Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#106 [RESTRUCTURING] : 이벤트를 활용한 fcm push 알림 구현 #122

Merged
merged 8 commits into from
Apr 22, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.wakeUpTogetUp.togetUp.api.event;

import org.springframework.context.ApplicationEventPublisher;

public class EventPublisher {

private static ApplicationEventPublisher publisher;

public static void setPublisher(ApplicationEventPublisher publisher) {
EventPublisher.publisher = publisher;
}

public static void raise(Object event) {
if (publisher != null) {
publisher.publishEvent(event);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.wakeUpTogetUp.togetUp.api.notification;

import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent;
import com.wakeUpTogetUp.togetUp.infra.fcm.FcmService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;


@Component
@RequiredArgsConstructor
public class NotificationEventHandler {
private final FcmService fcmService;

@Async()
@EventListener(NotificationSendEvent.class)
public void handleSendPushEvent(NotificationSendEvent event) {
fcmService.sendMessageToTokens(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
@Service

@RequiredArgsConstructor
public class PushLogService {
public class NotificationLogService {

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.wakeUpTogetUp.togetUp.api.notification;

import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo;
import com.wakeUpTogetUp.togetUp.api.notification.vo.RoomMissionLogNotificationVo;
import com.wakeUpTogetUp.togetUp.api.event.EventPublisher;
import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent;
import com.wakeUpTogetUp.togetUp.api.notification.vo.RoomMissionLogNotificationEvent;
import com.wakeUpTogetUp.togetUp.api.room.RoomRepository;
import com.wakeUpTogetUp.togetUp.api.room.model.Room;
import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser;
Expand All @@ -11,10 +12,12 @@
import com.wakeUpTogetUp.togetUp.api.users.model.User;
import com.wakeUpTogetUp.togetUp.common.Status;
import com.wakeUpTogetUp.togetUp.exception.BaseException;
import com.wakeUpTogetUp.togetUp.infra.google.fcm.FcmService;
import com.wakeUpTogetUp.togetUp.infra.fcm.FcmService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -28,14 +31,13 @@ public class NotificationService {
private final FcmTokenRepository fcmTokenRepository;

public void sendNotificationToAllUsers(String title, String body) {
fcmService.sendMessageToTokens(
new NotificationSendVo(
fcmTokenRepository.findAllByUser_IdIn(userService.getAgreedNotiUsersIds()),
title,
body,
Map.of(DataKeyType.LINK.getKey(), DataValueType.HOME.toString())
)
);
EventPublisher.raise(new NotificationSendEvent(
fcmTokenRepository.findAllByUser_IdIn(userService.getAgreedNotiUsersIds()),
title,
body,
Map.of(DataKeyType.LINK.getKey(), DataValueType.HOME.toString())
));

}

public void sendNotificationToUsersInRoom(Integer alarmId, Integer missionCompleteUserId) {
Expand All @@ -51,13 +53,11 @@ public void sendNotificationToUsersInRoom(Integer alarmId, Integer missionComple
.collect(Collectors.toList());

if (userIdsInRoom.size() > 0) {
fcmService.sendMessageToTokens(
new RoomMissionLogNotificationVo(
user,
room,
fcmTokenRepository.findAllByUser_IdIn(userIdsInRoom)
)
);
EventPublisher.raise(new RoomMissionLogNotificationEvent(
user,
room,
fcmTokenRepository.findAllByUser_IdIn(userIdsInRoom)
));
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@
public class Notification {


@Builder
public Notification(String title, String content, Room room, FcmToken fcmToken) {
this.title = title;
this.content = content;
this.room = room;
this.fcmToken = fcmToken;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column( columnDefinition = "INT UNSIGNED")
@Column(columnDefinition = "INT UNSIGNED")
private Integer id;

private String title;
Expand All @@ -40,10 +32,10 @@ public Notification(String title, String content, Room room, FcmToken fcmToken)
private String createdAt = TimeFormatter.timestampFormat(new Timestamp(System.currentTimeMillis()));

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "roomId",columnDefinition = "INT UNSIGNED")
@JoinColumn(name = "roomId", columnDefinition = "INT UNSIGNED")
private Room room;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fcmTokenId",columnDefinition = "INT UNSIGNED")
@JoinColumn(name = "fcmTokenId", columnDefinition = "INT UNSIGNED")
private FcmToken fcmToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

@ToString
@Getter
public class NotificationSendVo {
public class NotificationSendEvent {
private final List<FcmToken> fcmTokens;
private final String title;
private final String body;
private final Map<String, String> dataMap;

public NotificationSendVo(List<FcmToken> fcmTokens, String title, String body, Map<String, String> dataMap) {
public NotificationSendEvent(List<FcmToken> fcmTokens, String title, String body, Map<String, String> dataMap) {
this.fcmTokens = fcmTokens;
this.title = title;
this.body = body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken;
import com.wakeUpTogetUp.togetUp.api.users.model.User;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoomMissionLogNotificationVo extends NotificationSendVo {
public class RoomMissionLogNotificationEvent extends NotificationSendEvent {
private static final String title = "%s님이 미션을 완료했어요!";
private static final String body = "%s 그룹";

public RoomMissionLogNotificationVo(User user, Room room, List<FcmToken> fcmTokens) {
public RoomMissionLogNotificationEvent(User user, Room room, List<FcmToken> fcmTokens) {
super(fcmTokens, String.format(title, user.getName()), String.format(body, room.getName()), createDataMap(room.getId()));

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.wakeUpTogetUp.togetUp.api.users;


import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenDeleteEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Component
@Transactional
@RequiredArgsConstructor
public class UserEventHandler {

private final UserService userService;

@EventListener(FcmTokenDeleteEvent.class)
public void handlePushTokenDeleteEvent(FcmTokenDeleteEvent event) {
userService.deleteFcmTokens(event.getFcmTokens());
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import com.wakeUpTogetUp.togetUp.api.users.vo.UserProgressResult;
import com.wakeUpTogetUp.togetUp.common.Status;
import com.wakeUpTogetUp.togetUp.exception.BaseException;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -68,6 +70,11 @@ private void saveFcmToken(User user, String fcmTokenValue) {
fcmTokenRepository.save(fcmToken);
}

@Transactional
public void deleteFcmTokens(List<String> fcmTokenValues) {
fcmTokenRepository.deleteByValueIn(fcmTokenValues);
}

public void updateAgreePush(Integer userId, boolean agreePush) {
User user = findExistingUser(userRepository, userId);
user.changeAgreePush(agreePush);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wakeUpTogetUp.togetUp.api.users.fcmToken;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@RequiredArgsConstructor
public class FcmTokenDeleteEvent {
private final List<String> fcmTokens;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
public interface FcmTokenRepository extends JpaRepository<FcmToken, Integer> {
List<FcmToken> findAllByUser_IdIn(List<Integer> userIds);

void deleteAllByValueIn(List<String> values);

void deleteByValueIn(List<String> fcmTokenValues);

boolean existsByValue(String fcmTokenValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class User {
@Column(name = "is_deleted")
private boolean isDeleted;

@JsonIgnore
@OneToMany(mappedBy = "user")
private List<FcmToken> fcmToken = new ArrayList<>();

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/wakeUpTogetUp/togetUp/config/EventsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.wakeUpTogetUp.togetUp.config;

import com.wakeUpTogetUp.togetUp.api.event.EventPublisher;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EventsConfig {
@Autowired
private ApplicationContext applicationContext;

@Bean
public InitializingBean eventsInitializer(ApplicationEventPublisher eventPublisher) {
return () -> EventPublisher.setPublisher(eventPublisher);
}
}
Loading
Loading