-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [chore] #56 add required dependency in build.gradle * [feat] #56 add FirebaseException and register in GlobalExceptionHandler * [feat] #56 create FirebaseConfig * [feat] #56 create FcmService * [feat] #56 create FcmMessageDto and FcmContent * [feat] #56 update yml * [feat] #56 add Async in fcm service * [feat] #56 add fcm exception * [feat] #56 create fcm notification logic * [fix] #56 move fcm related exception in business to firebase exception * [fix] #56 edit error code number
- Loading branch information
Showing
12 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
Submodule SERVER_YML
updated
from e66df2 to 153924
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
35 changes: 35 additions & 0 deletions
35
src/main/java/org/kkumulkkum/server/config/FirebaseConfig.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,35 @@ | ||
package org.kkumulkkum.server.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.kkumulkkum.server.exception.FirebaseException; | ||
import org.kkumulkkum.server.exception.code.FirebaseErrorCode; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@Component | ||
public class FirebaseConfig { | ||
|
||
@PostConstruct | ||
public void initialize() { | ||
try { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(new ClassPathResource("firebase/firebase_service_key.json").getInputStream())) | ||
.build(); | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
log.info("FirebaseApp initialized {}", FirebaseApp.getInstance().getName()); | ||
} | ||
} catch (IOException e) { | ||
log.error("FirebaseApp initialize failed : {}", e.getMessage()); | ||
throw new FirebaseException(FirebaseErrorCode.NOT_FOUND_FIREBASE_JSON); | ||
} | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
src/main/java/org/kkumulkkum/server/exception/FirebaseException.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,11 @@ | ||
package org.kkumulkkum.server.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.kkumulkkum.server.exception.code.FirebaseErrorCode; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class FirebaseException extends RuntimeException { | ||
private final FirebaseErrorCode errorCode; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/kkumulkkum/server/exception/code/FirebaseErrorCode.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,19 @@ | ||
package org.kkumulkkum.server.exception.code; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum FirebaseErrorCode implements DefaultErrorCode { | ||
// 400 BAD_REQUEST | ||
FCM_ERROR(HttpStatus.BAD_REQUEST,40070,"fcm 토큰 오류입니다."), | ||
// 404 NOT_FOUND | ||
NOT_FOUND_FIREBASE_JSON(HttpStatus.NOT_FOUND, 40470, "FIREBASE JSON을 찾을 수 없습니다."), | ||
; | ||
|
||
private HttpStatus httpStatus; | ||
private int code; | ||
private String message; | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/kkumulkkum/server/external/FcmService.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,51 @@ | ||
package org.kkumulkkum.server.external; | ||
|
||
import com.google.firebase.messaging.*; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.kkumulkkum.server.exception.BusinessException; | ||
import org.kkumulkkum.server.exception.FirebaseException; | ||
import org.kkumulkkum.server.exception.code.BusinessErrorCode; | ||
import org.kkumulkkum.server.exception.code.FirebaseErrorCode; | ||
import org.kkumulkkum.server.external.dto.FcmMessageDto; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class FcmService { | ||
|
||
@Async | ||
public void sendBulk( | ||
final List<String> fcmTokens, | ||
final FcmMessageDto fcmMessageDto | ||
){ | ||
MulticastMessage message = createBulkMessage(fcmTokens, fcmMessageDto); | ||
try { | ||
FirebaseMessaging.getInstance().sendMulticast(message); | ||
} catch (FirebaseMessagingException e){ | ||
throw new FirebaseException(FirebaseErrorCode.FCM_ERROR); | ||
} | ||
} | ||
|
||
private MulticastMessage createBulkMessage( | ||
final List<String> fcmTokens, | ||
final FcmMessageDto fcmMessageDto | ||
){ | ||
return MulticastMessage.builder() | ||
.addAllTokens(fcmTokens) | ||
.setNotification( | ||
Notification.builder() | ||
.setTitle(fcmMessageDto.title()) | ||
.setBody(fcmMessageDto.body()) | ||
.build() | ||
) | ||
.putData("screen", fcmMessageDto.screen()) | ||
.putData("promiseId", fcmMessageDto.promiseId().toString()) | ||
.build(); | ||
} | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
src/main/java/org/kkumulkkum/server/external/dto/FcmMessageDto.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,19 @@ | ||
package org.kkumulkkum.server.external.dto; | ||
|
||
import org.kkumulkkum.server.external.enums.FcmContent; | ||
|
||
public record FcmMessageDto( | ||
String title, | ||
String body, | ||
String screen, | ||
Long promiseId | ||
) { | ||
public static FcmMessageDto of(FcmContent fcmContent, Long promiseId) { | ||
return new FcmMessageDto( | ||
fcmContent.getTitle(), | ||
fcmContent.getBody(), | ||
fcmContent.getScreen(), | ||
promiseId | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/kkumulkkum/server/external/enums/FcmContent.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,18 @@ | ||
package org.kkumulkkum.server.external.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum FcmContent { | ||
|
||
FIRST_PREPARATION("⏰ 누군가 준비를 시작했어요 ⏰", "첫 번째 사람이 준비를 시작했어요\uD83D\uDE35\\n어플로 들어가 누군지 확인해 보세요!", "readyStatus"), | ||
FIRST_DEPARTURE("⏰ 누군가 이동을 시작했어요 ⏰", "첫 번째로 누군가 이동을 시작했어요\uD83D\uDE35\\n어플로 들어가 누군지 확인해 보세요!", "readyStatus"), | ||
FIRST_ARRIVAL("⏰ 누군가 도착 했어요 ⏰", "누군가 약속 장소에 도착했어요!\uD83D\uDE35\\n도착한 사람이 누구인지 확인해보세요!", "readyStatus"), | ||
; | ||
|
||
private final String title; | ||
private final String body; | ||
private final String screen; | ||
} |
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