-
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.
Browse files
Browse the repository at this point in the history
* feat: 팔로우 신청 및 수락용 event 추가 * feat: push 알림 이벤트용 리스너 추가 * refactor: 푸시 알림 이벤트를 발행하도록 수정
- Loading branch information
Showing
4 changed files
with
176 additions
and
75 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
voicepocket/src/main/java/com/vp/voicepocket/domain/friend/event/FriendAcceptPushEvent.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,11 @@ | ||
package com.vp.voicepocket.domain.friend.event; | ||
|
||
import com.vp.voicepocket.domain.friend.entity.Friend; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class FriendAcceptPushEvent { | ||
private Friend friend; | ||
} |
11 changes: 11 additions & 0 deletions
11
voicepocket/src/main/java/com/vp/voicepocket/domain/friend/event/FriendRequestPushEvent.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,11 @@ | ||
package com.vp.voicepocket.domain.friend.event; | ||
|
||
import com.vp.voicepocket.domain.friend.entity.Friend; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class FriendRequestPushEvent { | ||
private Friend friend; | ||
} |
98 changes: 98 additions & 0 deletions
98
voicepocket/src/main/java/com/vp/voicepocket/domain/friend/event/PushEventListener.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,98 @@ | ||
package com.vp.voicepocket.domain.friend.event; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
import com.vp.voicepocket.domain.firebase.entity.FCMUserToken; | ||
import com.vp.voicepocket.domain.firebase.exception.CFCMTokenNotFoundException; | ||
import com.vp.voicepocket.domain.firebase.repository.FCMRepository; | ||
import com.vp.voicepocket.domain.user.entity.User; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.event.TransactionPhase; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
import java.util.HashMap; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class PushEventListener { | ||
private final FirebaseMessaging firebaseMessaging; | ||
private final FCMRepository fcmRepository; | ||
|
||
@Async | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
public void sendFriendRequestPushMessage(FriendRequestPushEvent friendRequestPushEvent) { | ||
User toUser = friendRequestPushEvent.getFriend().getRequest_to(); | ||
User fromUser = friendRequestPushEvent.getFriend().getRequest_from(); | ||
|
||
FCMUserToken fcmEntity = fcmRepository.findByUserId(toUser) | ||
.orElseThrow(CFCMTokenNotFoundException::new); | ||
String fcmToken = fcmEntity.getFireBaseToken(); | ||
|
||
try { | ||
Message message = getFriendRequestPushMessage(fromUser.getName(), fcmToken); | ||
firebaseMessaging.send(message); | ||
} catch (FirebaseMessagingException e) { | ||
log.error("PUSH NOTIFICATION ERROR: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
@Async | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
public void sendFriendAcceptPushMessage(FriendAcceptPushEvent friendAcceptPushEvent) { | ||
User toUser = friendAcceptPushEvent.getFriend().getRequest_to(); | ||
User fromUser = friendAcceptPushEvent.getFriend().getRequest_from(); | ||
|
||
FCMUserToken fcmEntity = fcmRepository.findByUserId(fromUser) | ||
.orElseThrow(CFCMTokenNotFoundException::new); | ||
String fcmToken = fcmEntity.getFireBaseToken(); | ||
|
||
try { | ||
Message message = getFriendAcceptPushMessage(toUser.getName(), fcmToken); | ||
firebaseMessaging.send(message); | ||
} catch (FirebaseMessagingException e) { | ||
log.error("PUSH NOTIFICATION ERROR: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
private Message getFriendRequestPushMessage(String fromUserName, String fcmToken) { | ||
Notification notification = Notification.builder() | ||
.setTitle("Friend Request") | ||
.setBody(fromUserName + " request Friend to you!") | ||
.build(); | ||
|
||
HashMap<String, String> data = new HashMap<>(); | ||
data.put("ID", "1"); | ||
|
||
return Message.builder() | ||
.setToken(fcmToken) | ||
.setNotification(notification) | ||
.putAllData(data) | ||
.build(); | ||
} | ||
|
||
private Message getFriendAcceptPushMessage(String toUserName, String fcmToken) { | ||
Notification notification = Notification.builder() | ||
.setTitle("Friend Accept") | ||
.setBody(toUserName + " Accept your Friend Request!") | ||
.build(); | ||
|
||
HashMap<String, String> data = new HashMap<>(); | ||
data.put("ID", "2"); | ||
|
||
return Message.builder() | ||
.setToken(fcmToken) | ||
.setNotification(notification) | ||
.putAllData(data) | ||
.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