-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from dnd-side-project/feat/fcm
[Feat#22] FCM을 활용한 알림 시스템 구현
- Loading branch information
Showing
11 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
timeet-firebase-adminsdk.json | ||
HELP.md | ||
.gradle | ||
build/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/org/dnd/timeet/common/security/annotation/ReqUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.dnd.timeet.common.security.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
|
||
@Target({ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") | ||
public @interface ReqUser { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.dnd.timeet.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
@Configuration | ||
public class FCMConfig { | ||
|
||
@Bean | ||
FirebaseMessaging firebaseMessaging() throws IOException { | ||
ClassPathResource resource = new ClassPathResource("timeet-firebase-adminsdk.json"); | ||
|
||
InputStream refreshToken = resource.getInputStream(); | ||
|
||
FirebaseApp firebaseApp = null; | ||
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(); | ||
|
||
if (firebaseAppList != null && !firebaseAppList.isEmpty()) { | ||
for (FirebaseApp app : firebaseAppList) { | ||
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { | ||
firebaseApp = app; | ||
} | ||
} | ||
} else { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(refreshToken)).build(); | ||
firebaseApp = FirebaseApp.initializeApp(options); | ||
} | ||
return FirebaseMessaging.getInstance(firebaseApp); | ||
} | ||
|
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/org/dnd/timeet/fcm/application/FCMNotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.dnd.timeet.fcm.application; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
import java.util.Collections; | ||
import lombok.RequiredArgsConstructor; | ||
import org.dnd.timeet.common.exception.InternalServerError; | ||
import org.dnd.timeet.common.exception.NotFoundError; | ||
import org.dnd.timeet.fcm.domain.FCMNotificationRequestDto; | ||
import org.dnd.timeet.member.domain.Member; | ||
import org.dnd.timeet.member.domain.MemberRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class FCMNotificationService { | ||
|
||
private final FirebaseMessaging firebaseMessaging; | ||
private final MemberRepository memberRepository; | ||
|
||
public void sendNotificationByToken(FCMNotificationRequestDto requestDto) { | ||
Member member = memberRepository.findById(requestDto.getTargetMemberId()) | ||
.orElseThrow(() -> new NotFoundError(NotFoundError.ErrorCode.RESOURCE_NOT_FOUND, | ||
Collections.singletonMap("MemberId", "Member not found"))); | ||
if (member.getFcmToken() == null) { | ||
throw new NotFoundError(NotFoundError.ErrorCode.RESOURCE_NOT_FOUND, | ||
Collections.singletonMap("fcmToken", "fcmToken not exist")); | ||
} | ||
|
||
Notification notification = Notification.builder() | ||
.setTitle(requestDto.getTitle()) | ||
.setBody(requestDto.getBody()) | ||
.build(); | ||
|
||
Message message = Message.builder() | ||
.setToken(member.getFcmToken()) | ||
.setNotification(notification) | ||
.build(); | ||
|
||
try { | ||
firebaseMessaging.send(message); | ||
} catch (Exception e) { | ||
throw new InternalServerError(InternalServerError.ErrorCode.INTERNAL_SERVER_ERROR, | ||
Collections.singletonMap("fcmSend", "Fail to send fcm")); | ||
} | ||
|
||
|
||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/dnd/timeet/fcm/domain/FCMNotificationRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.dnd.timeet.fcm.domain; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class FCMNotificationRequestDto { | ||
|
||
private Long targetMemberId; | ||
private String title; | ||
private String body; | ||
|
||
@Builder | ||
public FCMNotificationRequestDto(Long targetMemberId, String title, String body) { | ||
this.targetMemberId = targetMemberId; | ||
this.title = title; | ||
this.body = body; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/org/dnd/timeet/member/dto/RegisterFcmRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.dnd.timeet.member.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Schema(description = "fcmToken 등록 요청") | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class RegisterFcmRequest { | ||
|
||
@Schema(description = "fcmToken", nullable = false, example = "fcmToken") | ||
private String fcmToken; | ||
} |