Skip to content

Commit

Permalink
Merge pull request #82 from HongDam-org/feat/apn
Browse files Browse the repository at this point in the history
[FEAT] Firebase 연동 및 FCM Service 기본 Logic 구현
  • Loading branch information
ohksj77 authored Dec 23, 2023
2 parents b0b62f5 + 6d5752a commit 1bc638d
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build/
!**/src/test/**/build/
.env
backend/src/main/resources/application-oauth.yml

backend/src/main/resources/firebase/twtw_firebase_key.json
### STS ###
.apt_generated
.classpath
Expand Down
1 change: 1 addition & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter:1.2.5.RELEASE'
implementation 'org.springframework.cloud:spring-cloud-gcp-storage:1.2.5.RELEASE'
implementation 'com.google.firebase:firebase-admin:6.8.1'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.twtw.backend.config.firebase;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

@Configuration
public class FcmConfig {
private static final ClassPathResource FIREBASE_RESOURCE =
new ClassPathResource("backend/src/main/resources/firebase/twtw_firebase_key.json");

@Bean
public FirebaseApp firebaseApp() throws IOException {
FirebaseOptions options =
FirebaseOptions.builder()
.setCredentials(
GoogleCredentials.fromStream(FIREBASE_RESOURCE.getInputStream()))
.build();

return FirebaseApp.initializeApp(options);
}

@Bean
public FirebaseMessaging firebaseMessaging() throws IOException {
return FirebaseMessaging.getInstance(firebaseApp());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.member.service.AuthService;
import com.twtw.backend.domain.member.service.MemberService;
import com.twtw.backend.domain.notification.service.FcmService;
import com.twtw.backend.global.exception.EntityNotFoundException;

import org.springframework.stereotype.Service;
Expand All @@ -34,17 +35,21 @@ public class GroupService {
private final MemberService memberService;
private final GroupMapper groupMapper;

private final FcmService fcmService;

public GroupService(
GroupRepository groupRepository,
GroupMemberRepository groupMemberRepository,
AuthService authService,
MemberService memberService,
GroupMapper groupMapper) {
GroupMapper groupMapper,
FcmService fcmService) {
this.groupRepository = groupRepository;
this.groupMemberRepository = groupMemberRepository;
this.authService = authService;
this.memberService = memberService;
this.groupMapper = groupMapper;
this.fcmService = fcmService;
}

public GroupInfoResponse getGroupById(UUID groupId) {
Expand Down Expand Up @@ -116,6 +121,8 @@ public GroupInfoResponse inviteGroup(InviteGroupRequest inviteGroupRequest) {
memberService.getMembersByIds(inviteGroupRequest.getFriendMemberIds());
group.inviteAll(friends);

// invite push Alert

return groupMapper.toGroupInfo(group);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.twtw.backend.domain.notification.dto;

import com.google.firebase.messaging.Notification;

import jakarta.validation.constraints.NotBlank;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class NotificationRequest {
@NotBlank private String deviceToken;

private String title;

private String body;

public Notification toNotification() {
return new Notification(title, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.twtw.backend.domain.notification.mapper;

import org.springframework.stereotype.Component;

@Component
public class NotificationMapper {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.twtw.backend.domain.notification.service;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.twtw.backend.domain.notification.dto.NotificationRequest;

import org.springframework.stereotype.Service;

@Service
public class FcmService {
private final FirebaseMessaging firebaseMessaging;

public FcmService(FirebaseMessaging firebaseMessaging) {
this.firebaseMessaging = firebaseMessaging;
}

public void sendNotification(NotificationRequest request) throws FirebaseMessagingException {
Message message =
Message.builder()
.setToken(request.getDeviceToken())
.setNotification(request.toNotification())
.build();

firebaseMessaging.send(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ void searchCarPath() {
SearchPathOption.TRAFAST,
SearchPathFuel.DIESEL,
1);
// when

// when
// SearchCarPathResponse response = pathService.searchCarPath(request);

// then

// assertThat(response.getCode()).isEqualTo(0);
}

Expand Down

0 comments on commit 1bc638d

Please sign in to comment.