Skip to content

Commit

Permalink
feat: fcm exception handling => 만료된 토큰의 device는 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
umi0410 committed Dec 12, 2021
1 parent 722de76 commit cff0b0c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ public class FcmPushManager implements PushManager{
@Value("${khumu.notification.rootLink}")
String NOTIFICATION_ROOT_LINK;

public void notify(Notification n, String deviceToken) throws PushException {
public void notify(Notification n, String deviceToken) throws ExpiredPushTokenException {
try {
System.out.println("Execute push notify to " + n.getRecipient() + "(" + deviceToken + ")");
String result = firebaseMessaging.send(
createMessage(n, deviceToken)
);
System.out.println(result);
} catch (FirebaseMessagingException e) {
if (e.getMessage().contains("Requested entity was not found.")) {
throw new ExpiredPushTokenException(deviceToken);
}

throw new PushException(e.getMessage());
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/khumu/alimi/external/push/PushManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
* 이건 DB의 Entity가 아니라 FCM Push를 의미.
*/
public interface PushManager {
void notify(Notification n, String deviceToken) throws PushException;
void notify(Notification n, String deviceToken) throws PushException, ExpiredPushTokenException;
// 모두에게 Notify
/// void notify(Notication n);
public static class PushException extends Exception{
class PushException extends RuntimeException{
public PushException(String message) {
super(message);
}
}

class ExpiredPushTokenException extends Exception{
public ExpiredPushTokenException(String token){
super("만료된 토큰입니다.: " + token);
}
}
}
10 changes: 3 additions & 7 deletions src/main/java/com/khumu/alimi/service/AnnouncementService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ public void notifyNewAnnouncementCrawled(NewAnnouncementCrawledDto event) {
try {
pushManager.notify(n, device.getDeviceToken());
log.info("푸시를 보냅니다. " + device.getUser());
} catch (PushManager.PushException e) {
if (e.getMessage().contains("Requested entity was not found.")) {
log.warn("더 이상 존재하지 않는 device tokne이므로 삭제합니다." + device.getDeviceToken());
pushDeviceRepository.delete(device);
} else{
e.printStackTrace();
}
} catch (PushManager.ExpiredPushTokenException e) {
log.warn("더 이상 존재하지 않는 device tokne이므로 삭제합니다." + device.getDeviceToken());
pushDeviceRepository.delete(device);
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/khumu/alimi/service/ArticleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ public void notifyNewHotArticle(ArticleResource article) throws PushManager.Push

Notification n = notificationRepository.save(tmp);

List<PushDevice> subscriptions = pushDeviceRepository.findAllByUser(author);
for (PushDevice subscription : subscriptions) {
pushManager.notify(n, subscription.getDeviceToken());
log.info("푸시를 보냅니다. " + subscription.getUser());
List<PushDevice> devices = pushDeviceRepository.findAllByUser(author);
for (PushDevice device : devices) {
try {
log.info("푸시를 보냅니다. " + n.getRecipient());
pushManager.notify(n, device.getDeviceToken());
} catch (PushManager.ExpiredPushTokenException e) {
log.warn("더 이상 존재하지 않는 device tokne이므로 삭제합니다." + device.getDeviceToken());
pushDeviceRepository.delete(device);
}
}
}
}
13 changes: 9 additions & 4 deletions src/main/java/com/khumu/alimi/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ public List<Notification> createNotificationsForNewComment(CommentDto commentDto

Notification n = notificationRepository.save(tmp);

List<PushDevice> subscriptions = pushDeviceRepository.findAllByUser(recipientId);
for (PushDevice subscription : subscriptions) {
pushManager.notify(n, subscription.getDeviceToken());
log.info("푸시를 보냅니다. " + subscription.getUser());
List<PushDevice> devices = pushDeviceRepository.findAllByUser(recipientId);
for (PushDevice device : devices) {
try {
log.info("푸시를 보냅니다. " + n.getRecipient());
pushManager.notify(n, device.getDeviceToken());
} catch (PushManager.ExpiredPushTokenException e) {
log.warn("더 이상 존재하지 않는 device tokne이므로 삭제합니다." + device.getDeviceToken());
pushDeviceRepository.delete(device);
}
}
results.add(n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ public List<Notification> createNotificationForHaksaScheduleStarts(HaksaSchedule
pushManager.notify(n, device.getDeviceToken());
log.info("푸시를 보냈습니다. " + device.getUser());
results.add(n);
} catch (PushManager.PushException e) {
if (e.getMessage().contains("Requested entity was not found.")) {
log.warn("더 이상 존재하지 않는 device tokne이므로 삭제합니다." + device.getDeviceToken());
pushDeviceRepository.delete(device);
} else{
e.printStackTrace();
}
} catch (PushManager.ExpiredPushTokenException e) {
log.warn("더 이상 존재하지 않는 device tokne이므로 삭제합니다." + device.getDeviceToken());
pushDeviceRepository.delete(device);
}

}
Expand Down

0 comments on commit cff0b0c

Please sign in to comment.