Skip to content

Commit

Permalink
Merge pull request #162 from 4bujak-4bujak/feature/notification_update
Browse files Browse the repository at this point in the history
refactor: Notification Type 추가
  • Loading branch information
yurim0628 authored Jun 10, 2024
2 parents 818a556 + c361f62 commit afadd3a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class FCMConstants {

// Target URL
public static final String TARGET_ID_KEY = "targetId";
public static final String TARGET_TYPE_KEY = "targetType";

// Notification Title
public static final String COMMENT_TITLE = "내 글에 댓글";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void saveAndSendFCMNotificationForComment(SaveCommentEvent event) {
"Writer Email: [{}], Notification Content: [{}], Target URL: [{}]", email, content, targetId);

saveNotification(event.commenterImage(), COMMENT_TITLE, content, targetId, COMMUNITY, event.writer());
sendFCMNotification(email, createFCMMessage(email, COMMENT_TITLE, content, targetId));
sendFCMNotification(email, createFCMMessage(email, COMMENT_TITLE, content, targetId, COMMUNITY));
}

@TransactionalEventListener(phase = AFTER_COMMIT)
Expand All @@ -65,7 +65,7 @@ public void saveAndSendFCMNotificationForMeetingRoomInvitation(ReserveMeetingRoo
log.info("Notification Target Participant Email: [{}]", email);

saveNotification(CHECK_IMAGE, MEETING_ROOM_INVITATION_TITLE, content, targetId, RESERVATION, participant);
sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_INVITATION_TITLE, content, targetId));
sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_INVITATION_TITLE, content, targetId, RESERVATION));
}
}

Expand All @@ -88,7 +88,7 @@ public void saveAndSendFCMNotificationForRechargingRoomCancellation(ReserveMeeti
log.info("Notification Content: [{}], Notification Target Canceler Email: [{}]", content, email);

saveNotification(CHECK_IMAGE, RECHARGING_ROOM_CANCELLATION_TITLE, content, targetId, RESERVATION, canceler);
sendFCMNotificationAsync(email, createFCMMessage(email, RECHARGING_ROOM_CANCELLATION_TITLE, content, targetId));
sendFCMNotificationAsync(email, createFCMMessage(email, RECHARGING_ROOM_CANCELLATION_TITLE, content, targetId, RESERVATION));
}
}

Expand All @@ -104,7 +104,7 @@ public void saveAndSendFCMNotificationForMeetingRoomEntry(FindMeetingRoomEntryNo
log.info("Notification Target Member Email: [{}]", email);

saveNotification(WARING_IMAGE, MEETING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION, member);
sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_RESERVATION_TITLE, content, targetId));
sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION));
}
}

Expand All @@ -117,16 +117,16 @@ public void saveAndSendFCMNotificationForRechargingRoomEntry(FindRechargingRoomE
"Member Email: [{}], Notification Content: [{}], Target ID: [{}]", email, content, targetId);

saveNotification(WARING_IMAGE, RECHARGING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION, event.member());
sendFCMNotification(email, createFCMMessage(email, RECHARGING_ROOM_RESERVATION_TITLE, content, targetId));
sendFCMNotification(email, createFCMMessage(email, RECHARGING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION));
}

private void saveNotification(String image, String title, String content, Long targetId, NotificationType type, Member member) {
log.info("Start Saving Notification Before Sending FCM Notification.");
notificationService.saveNotification(image, title, content, targetId, type, member);
}

private Message createFCMMessage(String email, String title, String content, Long targetId) {
return fcmNotificationService.createFCMMessage(email, title, content, targetId);
private Message createFCMMessage(String email, String title, String content, Long targetId, NotificationType targetType) {
return fcmNotificationService.createFCMMessage(email, title, content, targetId, targetType);
}

private void sendFCMNotification(String email, Message message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.sabujak.fcm.service;

import com.example.sabujak.fcm.exception.FCMException;
import com.example.sabujak.notification.entity.NotificationType;
import com.google.api.core.ApiFuture;
import com.google.firebase.messaging.*;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -30,11 +31,11 @@ public class FCMNotificationService {
private final Executor callBackTaskExecutor;
private final ObjectProvider<FCMNotificationService> provider;

public Message createFCMMessage(String email, String title, String content, Long targetId) {
public Message createFCMMessage(String email, String title, String content, Long targetId, NotificationType targetType) {
return Message.builder()
.setToken(fcmTokenService.getFCMToken(email))
.setNotification(createNotification(title, content))
.putAllData(createData(targetId))
.putAllData(createData(targetId, targetType))
.build();
}

Expand All @@ -46,8 +47,11 @@ private Notification createNotification(String title, String body) {
.build();
}

private Map<String, String> createData(Long targetId) {
return Map.of(TARGET_ID_KEY, targetId.toString());
private Map<String, String> createData(Long targetId, NotificationType targetType) {
return Map.of(
TARGET_ID_KEY, targetId.toString(),
TARGET_TYPE_KEY, targetType.toString()
);
}

public void sendFCMNotificationAsync(String email, Message message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.sabujak.notification.dto;

import com.example.sabujak.notification.entity.Notification;
import com.example.sabujak.notification.entity.NotificationType;

import java.time.LocalDateTime;

Expand All @@ -10,6 +11,7 @@ public record NotificationResponse(
String title,
String content,
Long targetId,
NotificationType targetType,
LocalDateTime date

) {
Expand All @@ -20,6 +22,7 @@ public static NotificationResponse of(Notification notification) {
notification.getTitle(),
notification.getContent(),
notification.getTargetId(),
notification.getType(),
notification.getCreatedDate()
);
}
Expand Down

0 comments on commit afadd3a

Please sign in to comment.