Skip to content

Commit

Permalink
[FEAT] deepLink add to fcm and notification
Browse files Browse the repository at this point in the history
  • Loading branch information
nogamsung committed Feb 14, 2024
1 parent e42da1d commit 9d1098e
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 269 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gabojait.gabojaitspring.api.dto.notification.response;

import com.gabojait.gabojaitspring.common.constant.deepLink.DeepLink;
import com.gabojait.gabojaitspring.domain.notification.DeepLinkType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
Expand All @@ -17,8 +17,8 @@ public class DeepLinkResponse {
@ApiModelProperty(position = 2, required = true, value = "설명")
private String description;

public DeepLinkResponse(DeepLink deepLink) {
this.url = deepLink.getUrl();
this.description = deepLink.getDescription();
public DeepLinkResponse(DeepLinkType deepLinkType) {
this.description = deepLinkType.getDescription();
this.url = deepLinkType.getUrl();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import com.gabojait.gabojaitspring.domain.notification.Notification;
import com.gabojait.gabojaitspring.domain.notification.NotificationType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
Expand All @@ -18,19 +17,16 @@ public class NotificationPageResponse {
@ApiModelProperty(position = 1, required = true, value = "알림 식별자")
private Long notificationId;

@ApiModelProperty(position = 2, required = true, value = "알림 타입")
private NotificationType notificationType;

@ApiModelProperty(position = 3, required = true, value = "알림 제목")
@ApiModelProperty(position = 2, required = true, value = "알림 제목")
private String title;

@ApiModelProperty(position = 4, required = true, value = "알림 내용")
@ApiModelProperty(position = 3, required = true, value = "알림 내용")
private String body;

@ApiModelProperty(position = 5, required = true, value = "읽음 여부")
@ApiModelProperty(position = 4, required = true, value = "읽음 여부")
private Boolean isRead;

@ApiModelProperty(position = 6, required = true, value = "딥링크")
@ApiModelProperty(position = 5, required = true, value = "딥링크")
private DeepLinkResponse deepLink;

@ApiModelProperty(position = 6, required = true, value = "생성일")
Expand All @@ -43,10 +39,10 @@ public class NotificationPageResponse {

public NotificationPageResponse(Notification notification) {
this.notificationId = notification.getId();
this.notificationType = notification.getNotificationType();
this.title = notification.getTitle();
this.body = notification.getBody();
this.isRead = notification.getIsRead();
this.deepLink = new DeepLinkResponse(notification.getDeepLinkType());
this.createdAt = notification.getCreatedAt();
this.updatedAt = notification.getUpdatedAt();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.gabojait.gabojaitspring.api.service.notification;

import com.gabojait.gabojaitspring.common.response.PageData;
import com.gabojait.gabojaitspring.api.dto.notification.response.NotificationPageResponse;
import com.gabojait.gabojaitspring.common.response.PageData;
import com.gabojait.gabojaitspring.domain.notification.DeepLinkType;
import com.gabojait.gabojaitspring.domain.notification.Notification;
import com.gabojait.gabojaitspring.domain.notification.NotificationType;
import com.gabojait.gabojaitspring.domain.offer.Offer;
import com.gabojait.gabojaitspring.domain.team.Team;
import com.gabojait.gabojaitspring.domain.team.TeamMember;
Expand Down Expand Up @@ -82,22 +82,22 @@ public void sendTeamMemberJoin(Offer offer) {
String userBody = offer.getPosition().getText() + "로서 역량을 펼쳐보세요!";
String teamTitle = "새로운 " + offer.getPosition().getText() + " 합류";
String teamBody = user.getNickname() + "님이 " + offer.getPosition().getText() + "로 팀에 합류하였어요.";
NotificationType notificationType = NotificationType.NEW_TEAM_MEMBER;
DeepLinkType deepLinkType = DeepLinkType.TEAM_PAGE;

createAndSaveNotification(user, notificationType, userTitle, userBody);
createAndSaveNotification(user, userTitle, userBody, deepLinkType);
if (!userFcms.isEmpty()) {
Map<String, String> data = createFcmData(userTitle, userBody, notificationType);
Map<String, String> data = createFcmData(userTitle, userBody, deepLinkType.getUrl());

MulticastMessage message = createMulticast(userFcms, data);
sendMulticast(message);
}

teamMemberRepository.findAllExceptUserFetchUser(team.getId(), user.getId())
.forEach(teamMember ->
createAndSaveNotification(teamMember.getUser(), notificationType, teamTitle, teamBody)
createAndSaveNotification(teamMember.getUser(), teamTitle, teamBody, deepLinkType)
);
if (!teamFcms.isEmpty()) {
Map<String, String> data = createFcmData(teamTitle, teamBody, notificationType);
Map<String, String> data = createFcmData(teamTitle, teamBody, deepLinkType.getUrl());

MulticastMessage message = createMulticast(teamFcms, data);
sendMulticast(message);
Expand All @@ -116,24 +116,25 @@ public void sendTeamMemberFired(User user, Team team) {

String userTitle = team.getProjectName() + "팀에서 추방";
String userBody = team.getProjectName() + "팀에서 추방 되었어요. 아쉽지만 새로운 팀을 찾아보세요.";
DeepLinkType userDeepLinkType = DeepLinkType.HOME_PAGE;
String teamTitle = user.getNickname() + "님 팀에서 추방";
String teamBody = user.getNickname() + "님이 팀장에 의해 추방되었어요.";
NotificationType notificationType = NotificationType.FIRED_TEAM_MEMBER;
DeepLinkType teamDeepLinkType = DeepLinkType.TEAM_PAGE;

createAndSaveNotification(user, notificationType, userTitle, userBody);
createAndSaveNotification(user, userTitle, userBody, userDeepLinkType);
if (!userFcms.isEmpty()) {
Map<String, String> data = createFcmData(userTitle, userBody, notificationType);
Map<String, String> data = createFcmData(userTitle, userBody, userDeepLinkType.getUrl());

MulticastMessage message = createMulticast(userFcms, data);
sendMulticast(message);
}

teamMemberRepository.findAllExceptUserFetchUser(team.getId(), user.getId())
.forEach(teamMember ->
createAndSaveNotification(teamMember.getUser(), notificationType, teamTitle, teamBody)
createAndSaveNotification(teamMember.getUser(), teamTitle, teamBody, teamDeepLinkType)
);
if (!teamFcms.isEmpty()) {
Map<String, String> data = createFcmData(teamTitle, teamBody, notificationType);
Map<String, String> data = createFcmData(teamTitle, teamBody, teamDeepLinkType.getUrl());

MulticastMessage message = createMulticast(teamFcms, data);
sendMulticast(message);
Expand All @@ -151,13 +152,13 @@ public void sendTeamMemberQuit(User user, Team team) {

String title = user.getNickname() + "님 팀 탈퇴";
String body = user.getNickname() + "님이 팀에서 탈퇴하였습니다.";
NotificationType notificationType = NotificationType.QUIT_TEAM_MEMBER;
DeepLinkType deepLinkType = DeepLinkType.TEAM_PAGE;
teamMemberRepository.findAllExceptUserFetchUser(team.getId(), user.getId())
.forEach(teamMember ->
createAndSaveNotification(teamMember.getUser(), notificationType, title, body)
createAndSaveNotification(teamMember.getUser(), title, body, deepLinkType)
);
if (!fcms.isEmpty()) {
Map<String, String> data = createFcmData(title, body, notificationType);
Map<String, String> data = createFcmData(title, body, deepLinkType.getUrl());

MulticastMessage message = createMulticast(fcms, data);
sendMulticast(message);
Expand All @@ -173,14 +174,14 @@ public void sendTeamMemberQuit(User user, Team team) {
public void sendTeamIncomplete(Team team) {
List<String> fcms = fcmRepository.findAllTeam(team.getId());
String title = team.getProjectName() + "팀 해산";
String boy = "아쉽지만 팀장에 의해 " + team.getProjectName() + "팀이 해산 되었어요.";
NotificationType notificationType = NotificationType.TEAM_INCOMPLETE;
String body = "아쉽지만 팀장에 의해 " + team.getProjectName() + "팀이 해산 되었어요.";
DeepLinkType deepLinkType = DeepLinkType.TEAM_PAGE;
teamMemberRepository.findAllFetchUser(team.getId())
.forEach(teamMember ->
createAndSaveNotification(teamMember.getUser(), notificationType, title, boy)
createAndSaveNotification(teamMember.getUser(), title, body, deepLinkType)
);
if (!fcms.isEmpty()) {
Map<String, String> data = createFcmData(title, boy, notificationType);
Map<String, String> data = createFcmData(title, body, deepLinkType.getUrl());

MulticastMessage message = createMulticast(fcms, data);
sendMulticast(message);
Expand All @@ -196,13 +197,13 @@ public void sendTeamComplete(Team team) {
List<String> fcms = fcmRepository.findAllTeam(team.getId());
String title = team.getProjectName() + " 프로젝트 완료";
String body = "수고하셨어요! 프로젝트를 완료했어요. 팀원 리뷰를 작성해보세요!";
NotificationType notificationType = NotificationType.TEAM_COMPLETE;
DeepLinkType deepLinkType = DeepLinkType.REVIEW_PAGE;
teamMemberRepository.findAllFetchUser(team.getId())
.forEach(teamMember ->
createAndSaveNotification(teamMember.getUser(), notificationType, title, body)
createAndSaveNotification(teamMember.getUser(), title, body, deepLinkType)
);
if (!fcms.isEmpty()) {
Map<String, String> data = createFcmData(title, body, notificationType);
Map<String, String> data = createFcmData(title, body, deepLinkType.getUrl());

MulticastMessage message = createMulticast(fcms, data);
sendMulticast(message);
Expand All @@ -218,14 +219,14 @@ public void sendTeamProfileUpdated(Team team) {
List<String> fcms = fcmRepository.findAllTeam(team.getId());
String title = team.getProjectName() + "팀 프로필 수정";
String body = team.getProjectName() + "팀 프로필이 팀장에 의해 수정되었어요.";
NotificationType notificationType = NotificationType.TEAM_PROFILE_UPDATED;
DeepLinkType deepLinkType = DeepLinkType.TEAM_PAGE;

teamMemberRepository.findAllFetchUser(team.getId())
.forEach(teamMember ->
createAndSaveNotification(teamMember.getUser(), notificationType, title, body)
createAndSaveNotification(teamMember.getUser(), title, body, deepLinkType)
);
if (!fcms.isEmpty()) {
Map<String, String> data = createFcmData(title, body, notificationType);
Map<String, String> data = createFcmData(title, body, deepLinkType.getUrl());

MulticastMessage message = createMulticast(fcms, data);
sendMulticast(message);
Expand All @@ -241,10 +242,10 @@ public void sendOfferByTeam(Offer offer) {
List<String> fcms = fcmRepository.findAllUser(offer.getUser().getId());
String title = offer.getPosition().toString() + " 스카웃 제의";
String body = offer.getTeam().getProjectName() + "팀에서 " + offer.getPosition().getText() + " 스카웃 제의가 왔어요!";
NotificationType notificationType = NotificationType.TEAM_OFFER;
createAndSaveNotification(offer.getUser(), notificationType, title, body);
DeepLinkType deepLinkType = DeepLinkType.USER_OFFER_RECEIVE_PAGE;
createAndSaveNotification(offer.getUser(), title, body, deepLinkType);
if (!fcms.isEmpty()) {
Map<String, String> data = createFcmData(title, body, notificationType);
Map<String, String> data = createFcmData(title, body, deepLinkType.getUrl());

MulticastMessage message = createMulticast(fcms, data);
sendMulticast(message);
Expand All @@ -260,26 +261,31 @@ public void sendOfferByUser(Offer offer) {
Optional<TeamMember> teamLeader = teamMemberRepository.findLeaderFetchUser(offer.getTeam().getId());
String title = offer.getPosition().getText() + " 지원";
String body = offer.getPosition().getText() + " " + offer.getUser().getNickname() + "님이 지원을 했습니다.";
NotificationType notificationType = NotificationType.USER_OFFER;
DeepLinkType deepLinkType = DeepLinkType.TEAM_OFFER_RECEIVE_PAGE;
if (teamLeader.isPresent()) {
createAndSaveNotification(teamLeader.get().getUser(), notificationType, title, body);
createAndSaveNotification(teamLeader.get().getUser(), title, body, deepLinkType);

List<String> fcms = fcmRepository.findAllUser(teamLeader.get().getUser().getId());
if (!fcms.isEmpty()) {
Map<String, String> data = createFcmData(title, body, notificationType);
Map<String, String> data = createFcmData(title, body, deepLinkType.getUrl());

MulticastMessage message = createMulticast(fcms, data);
sendMulticast(message);
}
}
}


private Map<String, String> createFcmData(String title, String body, NotificationType notificationType) {
/**
* FCM 데이터 생성
* @param title 제목
* @param body 내용
* @param deepLinkUrl 딥링크 URL
*/
private Map<String, String> createFcmData(String title, String body, String deepLinkUrl) {
return Map.of(
"title", title,
"body", body,
"type", notificationType.name(),
"deepLink", deepLinkUrl,
"time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
);
}
Expand Down Expand Up @@ -324,15 +330,15 @@ public void sendMulticast(MulticastMessage message) {
/**
* 알림 생성 및 저장
* @param user 회원
* @param notificationType 알림 종류
* @param title 제목
* @param body 내용
* @param deepLinkType 딥링크 종류
*/
@Transactional
public void createAndSaveNotification(User user, NotificationType notificationType, String title, String body) {
public void createAndSaveNotification(User user, String title, String body, DeepLinkType deepLinkType) {
Notification notification = Notification.builder()
.user(user)
.notificationType(notificationType)
.deepLinkType(deepLinkType)
.title(title)
.body(body)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.gabojait.gabojaitspring.common.constant.deepLink;
package com.gabojait.gabojaitspring.domain.notification;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@AllArgsConstructor
public enum DeepLink {
@RequiredArgsConstructor
public enum DeepLinkType {

HOME_PAGE("gabojait://home", "홈페이지"),
TEAM_PAGE("gabojait://team", "팀 페이지"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ public class Notification extends BasePermanentEntity {
@JoinColumn(name = "user_id")
private User user;

@Column(nullable = false, length = 25)
@Enumerated(EnumType.STRING)
private NotificationType notificationType;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String body;
@Column(nullable = false)
private Boolean isRead;
@Column(nullable = false, length = 31)
@Enumerated(EnumType.STRING)
private DeepLinkType deepLinkType;

@Builder
private Notification(User user, NotificationType notificationType, String title, String body) {
private Notification(User user, String title, String body, DeepLinkType deepLinkType) {
this.user = user;
this.notificationType = notificationType;
this.title = title;
this.body = body;
this.isRead = false;
this.deepLinkType = deepLinkType;
this.isDeleted = false;
}

Expand All @@ -54,14 +54,14 @@ public boolean equals(Object object) {
Notification that = (Notification) object;
return Objects.equals(id, that.id)
&& Objects.equals(user, that.user)
&& notificationType == that.notificationType
&& deepLinkType == that.deepLinkType
&& Objects.equals(title, that.title)
&& Objects.equals(body, that.body)
&& Objects.equals(isRead, that.isRead);
}

@Override
public int hashCode() {
return Objects.hash(id, user, notificationType, title, body, isRead);
return Objects.hash(id, user, deepLinkType, title, body, isRead);
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ CREATE TABLE notification
notification_id BIGINT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
body VARCHAR(255) NOT NULL,
notification_type VARCHAR(25) NOT NULL,
deep_link_type VARCHAR(31) NOT NULL,
is_read BIT NOT NULL,
created_at DATETIME(6) NOT NULL,
updated_at DATETIME(6) NOT NULL,
Expand Down
Loading

0 comments on commit 9d1098e

Please sign in to comment.