-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat#22] FCM을 활용한 알림 시스템 구현 #23
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
81eed9a
[#22] feat: Add FCM service and config
FacerAin ffcd451
[#22] feat: Add upsertFcmToken
FacerAin b3454ea
[#22] feat: Update build workflow for firebase config
FacerAin 78e45d9
[#22] feat: Update build workflow for firebase config
FacerAin 662ecf5
[#22] feat: Update build workflow for firebase config
FacerAin bff3d06
Merge branch 'dev' into feat/fcm
FacerAin f80cd78
[#22] feat: Add patch fcmToken controller
FacerAin dc421fe
Merge remote-tracking branch 'origin/feat/fcm' into feat/fcm
FacerAin 912ce97
Merge remote-tracking branch 'origin/dev' into feat/fcm
FacerAin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. firebase 설정과 관련하여 application.yml과 통합을 고려하였는데, |
||
|
||
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spring security 관련하여☺️
access 정보로부터 member 정보 불러올 때 가독성과 유지보수를 높이기 위해 다음과 같이 custom annotation을 사용하였는데, annotation으로 인한 side-effect나 단점이 없을지 궁금합니다
참고자료: https://sol-devlog.tistory.com/3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드 중복도 줄이고 정말 좋은 방법이네요!
저도 만든 어노테이션 잘 활용해보겠습니다 :)