Skip to content

Commit

Permalink
[FEAT]/#70-FCM을 이용한 푸시알림 생성과 디바이스 정보 저장 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
koojun99 committed Mar 12, 2024
1 parent 2f1def6 commit df19a1b
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example.tree.global.common.fcm.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)

public class FCMRequestDTO {

@Getter
public static class sendFCM {
private String title;
private String message;
private String receiverId;
}

@Getter
public static class saveDevice {
private String deviceUuid;
private String fcmToken;
private String userId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.example.tree.global.common.fcm.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class FCMDevice {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String deviceUuid;

private String fcmToken;

private String userId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.tree.global.common.fcm.repository;

import org.example.tree.global.common.fcm.entity.FCMDevice;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FCMDeviceRepository extends JpaRepository<FCMDevice, Long> {
FCMDevice findByDeviceUuid(String deviceUuid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.example.tree.global.common.fcm.service;

import lombok.RequiredArgsConstructor;
import org.example.tree.global.common.fcm.dto.FCMRequestDTO;
import org.example.tree.global.common.fcm.entity.FCMDevice;
import org.example.tree.global.common.fcm.repository.FCMDeviceRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class FCMDeviceService {

FCMDeviceRepository fcmDeviceRepository;

public Long saveDevice(FCMRequestDTO.saveDevice request) {
FCMDevice fcmDevice = FCMDevice.builder()
.deviceUuid(request.getDeviceUuid())
.fcmToken(request.getFcmToken())
.userId(request.getUserId())
.build();
return fcmDeviceRepository.save(fcmDevice).getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.example.tree.global.common.fcm.service;

import com.google.firebase.messaging.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.tree.global.common.fcm.entity.FCMDevice;
import org.example.tree.global.common.fcm.repository.FCMDeviceRepository;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class FCMService {

private final FCMDeviceRepository fcmDeviceRepository;
public static final String PUSH_CLICK = "push_click";

public String send(String deviceUuid, String title, String body) {
FCMDevice targetUser = fcmDeviceRepository.findByDeviceUuid(deviceUuid);

Message message = buildFcmMessage(title, body, targetUser.getFcmToken());
try {
String response = FirebaseMessaging.getInstance().send(message);
log.info("FCM-send-" + response);
} catch (FirebaseMessagingException e) {
log.info("FCM-except-" + e.getMessage());
return "알림 실패";
}
return "알림 전송";
}

private Message buildFcmMessage(String title, String body, String token) {
return Message.builder()
.setToken(token)
.setNotification(
Notification.builder()
.setTitle(title)
.setBody(body)
.build()
)
.setAndroidConfig(buildAndroidConfig(title, body))
.setApnsConfig(buildApnsConfig())
.build();
}

private AndroidConfig buildAndroidConfig(String title, String body) {
return AndroidConfig.builder()
.setNotification(
AndroidNotification.builder()
.setTitle(title)
.setBody(body)
.setClickAction(PUSH_CLICK)
.build()
)
.build();
}

private ApnsConfig buildApnsConfig() {
return ApnsConfig.builder()
.setAps(Aps.builder()
.setCategory(PUSH_CLICK)
.build())
.build();
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/example/tree/global/config/FCMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public void initialize() {
.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.build();

FirebaseApp.initializeApp(options);
log.info("Fcm Setting Completed");
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
log.info("Fcm Setting Completed");
}

} catch (final IOException e) {
throw new RuntimeException(e.getMessage());
Expand Down

0 comments on commit df19a1b

Please sign in to comment.