-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] Fcm 푸시 알림 서비스 구현
- Loading branch information
Showing
13 changed files
with
222 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ out/ | |
.DS_Store | ||
../../.DS_Store | ||
*.DS_Store | ||
/src/main/resources/fire-base.json |
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
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
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
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
41 changes: 41 additions & 0 deletions
41
DontBeServer/src/main/java/com/dontbe/www/DontBeServer/external/fcm/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,41 @@ | ||
package com.dontbe.www.DontBeServer.external.fcm.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public class FcmMessageDto { | ||
private boolean validateOnly; | ||
private Message message; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public static class Message { | ||
@JsonProperty("notification") | ||
private NotificationDetails notificationDetails; | ||
private String token; | ||
private Data data; | ||
} | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public static class NotificationDetails { | ||
private String title; | ||
private String body; | ||
} | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public static class Data { | ||
private String name; | ||
private String description; | ||
private Long relateContentId; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
DontBeServer/src/main/java/com/dontbe/www/DontBeServer/external/fcm/service/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,53 @@ | ||
package com.dontbe.www.DontBeServer.external.fcm.service; | ||
|
||
import com.dontbe.www.DontBeServer.common.exception.BadRequestException; | ||
import com.dontbe.www.DontBeServer.external.fcm.dto.FcmMessageDto; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FcmService { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@PostConstruct | ||
public void initialize() throws IOException { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(new ClassPathResource("fire-base.json").getInputStream())) | ||
.build(); | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
} | ||
} | ||
|
||
public void sendMessage(FcmMessageDto fcmMessageDto) { | ||
Message message = Message.builder() | ||
.setToken(fcmMessageDto.getMessage().getToken()) | ||
.setNotification(com.google.firebase.messaging.Notification.builder() | ||
.setTitle(fcmMessageDto.getMessage().getNotificationDetails().getTitle()) | ||
.setBody(fcmMessageDto.getMessage().getNotificationDetails().getBody()) | ||
.build() | ||
) | ||
.putAllData(objectMapper.convertValue(fcmMessageDto.getMessage().getData(), Map.class)) | ||
.build(); | ||
|
||
try{ | ||
FirebaseMessaging.getInstance().send(message); | ||
} catch (FirebaseMessagingException e) { | ||
throw new BadRequestException(e.getMessage()); | ||
} | ||
} | ||
} |