Skip to content

Commit

Permalink
feat: 푸시 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoon-jang committed Oct 26, 2023
1 parent 2f3a606 commit 62b60c6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.polzzak.domain.notification.service.NotificationService;
import com.polzzak.domain.user.entity.Member;
import com.polzzak.domain.user.service.UserService;
import com.polzzak.global.infra.firebase.FirebaseCloudMessageService;
import com.polzzak.global.infra.firebase.service.FirebaseCloudMessageService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
Expand Down

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/com/polzzak/global/infra/firebase/dto/Message.java
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;
}
}
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;
}
}
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();
}
}
}

0 comments on commit 62b60c6

Please sign in to comment.