-
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.
[FEAT]/#70-FCM을 이용한 푸시알림 생성과 디바이스 정보 저장 로직 구현
- Loading branch information
Showing
6 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/org/example/tree/global/common/fcm/dto/FCMRequestDTO.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,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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/example/tree/global/common/fcm/entity/FCMDevice.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,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; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/org/example/tree/global/common/fcm/repository/FCMDeviceRepository.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,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); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/example/tree/global/common/fcm/service/FCMDeviceService.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,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(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/org/example/tree/global/common/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,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(); | ||
} | ||
} |
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