-
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.
- Loading branch information
1 parent
2f3a606
commit 62b60c6
Showing
5 changed files
with
96 additions
and
110 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
109 changes: 0 additions & 109 deletions
109
src/main/java/com/polzzak/global/infra/firebase/FirebaseCloudMessageService.java
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/main/java/com/polzzak/global/infra/firebase/dto/Message.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 com.polzzak.global.infra.firebase.dto; | ||
|
||
import java.util.Map; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Message { | ||
|
||
private Notification notification; | ||
private String to; | ||
private Map<String, String> data; | ||
|
||
public Message(Notification notification, String to, Map<String, String> data) { | ||
this.notification = notification; | ||
this.to = to; | ||
this.data = data; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/polzzak/global/infra/firebase/dto/Notification.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,17 @@ | ||
package com.polzzak.global.infra.firebase.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Notification { | ||
|
||
private String title; | ||
private String body; | ||
private String sound; | ||
|
||
public Notification(String title, String body, String sound) { | ||
this.title = title; | ||
this.body = body; | ||
this.sound = sound; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/polzzak/global/infra/firebase/service/FirebaseCloudMessageService.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,59 @@ | ||
package com.polzzak.global.infra.firebase.service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.polzzak.domain.pushtoken.model.PushToken; | ||
import com.polzzak.domain.pushtoken.service.PushTokenService; | ||
import com.polzzak.domain.user.entity.Member; | ||
import com.polzzak.global.infra.firebase.dto.Message; | ||
import com.polzzak.global.infra.firebase.dto.Notification; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
@Service | ||
@Log4j2 | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class FirebaseCloudMessageService { | ||
|
||
private final PushTokenService pushTokenService; | ||
private static final String FCM_URL = "https://fcm.googleapis.com/fcm/send"; | ||
private static final String SERVER_KEY = "AAAAXWWv8O0:APA91bEPp9GZzMUjTmhmk9n0J5PuX1LvAf-Kaa-vCffntaV85klO-" | ||
+ "gLb4QLf4f9ohrjdIu6L7MOZbaKUReN8CgljU4t1vWWS6BFESQhpZVPQzLhtswju1naFbmHyVNzrHvZWVpHvRNWo"; | ||
|
||
public void sendPushNotification(Member member, String title, String body, String link) { | ||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||
HttpPost httpPost = new HttpPost(FCM_URL); | ||
httpPost.setHeader("Content-Type", "application/json"); | ||
httpPost.setHeader("Authorization", "key=" + SERVER_KEY); | ||
|
||
List<String> registrationTokens = pushTokenService.getPushTokens(member).stream() | ||
.map(PushToken::getToken) | ||
.toList(); | ||
|
||
for (String token : registrationTokens) { | ||
Notification notification = new Notification(title, body, "default"); | ||
Map<String, String> data = Map.of("link", link == null ? "" : link, "title", title, "body", body); | ||
Message message = new Message(notification, token, data); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
String fcmMessage = objectMapper.writeValueAsString(message); | ||
httpPost.setEntity(new StringEntity(fcmMessage, "UTF-8")); | ||
httpClient.execute(httpPost); | ||
} | ||
} catch (Exception e) { | ||
// 예외 처리 | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |