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

[BE] FCM 알림 전송 Topic 구독 방식으로 변경 #771

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.happy.friendogly.notification.service;
package com.happy.friendogly.chatroom.service;

import static com.happy.friendogly.notification.domain.NotificationType.CHAT;

import com.happy.friendogly.chatsocket.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import com.happy.friendogly.deveicetoken.repository.DeviceTokenRepository;
import com.happy.friendogly.notification.service.NotificationService;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.happy.friendogly.exception.FriendoglyException;
import com.happy.friendogly.member.domain.Member;
import com.happy.friendogly.member.repository.MemberRepository;
import com.happy.friendogly.notification.service.ChatNotificationService;
import com.happy.friendogly.chatroom.service.ChatNotificationService;
import java.time.LocalDateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.happy.friendogly.exception.FriendoglyException;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -60,4 +61,8 @@ private InputStream getFirebaseCredentialsInputStream() {
}
}

@Bean
public FirebaseMessaging firebaseMessaging(){
return FirebaseMessaging.getInstance(firebaseApp());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.happy.friendogly.notification.controller;
package com.happy.friendogly.deveicetoken.controller;


import com.happy.friendogly.auth.Auth;
import com.happy.friendogly.common.ApiResponse;
import com.happy.friendogly.notification.dto.request.UpdateDeviceTokenRequest;
import com.happy.friendogly.notification.dto.response.UpdateDeviceTokenResponse;
import com.happy.friendogly.notification.service.DeviceTokenCommandService;
import com.happy.friendogly.deveicetoken.dto.request.UpdateDeviceTokenRequest;
import com.happy.friendogly.deveicetoken.dto.response.UpdateDeviceTokenResponse;
import com.happy.friendogly.deveicetoken.service.DeviceTokenCommandService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.happy.friendogly.notification.domain;
package com.happy.friendogly.deveicetoken.domain;

import com.happy.friendogly.member.domain.Member;
import jakarta.persistence.Column;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.happy.friendogly.notification.dto.request;
package com.happy.friendogly.deveicetoken.dto.request;

import jakarta.validation.constraints.NotBlank;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.happy.friendogly.notification.dto.response;
package com.happy.friendogly.deveicetoken.dto.response;

public record UpdateDeviceTokenResponse(String deviceToken) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.happy.friendogly.notification.repository;
package com.happy.friendogly.deveicetoken.repository;

import com.happy.friendogly.deveicetoken.domain.DeviceToken;
import com.happy.friendogly.exception.FriendoglyException;
import com.happy.friendogly.notification.domain.DeviceToken;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -26,4 +26,6 @@ List<String> findAllByChatRoomIdWithoutMine(
@Param("chatRoomId") Long chatRoomId, @Param("myMemberId") Long myMemberId);

void deleteByMemberId(Long memberId);

List<DeviceToken> findByMemberIdIn(List<Long> memberIds);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.happy.friendogly.notification.service;
package com.happy.friendogly.deveicetoken.service;

import com.happy.friendogly.deveicetoken.domain.DeviceToken;
import com.happy.friendogly.deveicetoken.dto.request.UpdateDeviceTokenRequest;
import com.happy.friendogly.deveicetoken.dto.response.UpdateDeviceTokenResponse;
import com.happy.friendogly.deveicetoken.repository.DeviceTokenRepository;
import com.happy.friendogly.member.repository.MemberRepository;
import com.happy.friendogly.notification.domain.DeviceToken;
import com.happy.friendogly.notification.dto.request.UpdateDeviceTokenRequest;
import com.happy.friendogly.notification.dto.response.UpdateDeviceTokenResponse;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.happy.friendogly.footprint.repository.FootprintRepository;
import com.happy.friendogly.member.domain.Member;
import com.happy.friendogly.member.repository.MemberRepository;
import com.happy.friendogly.notification.service.FootprintNotificationService;
import com.happy.friendogly.pet.domain.Pet;
import com.happy.friendogly.pet.repository.PetRepository;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.happy.friendogly.notification.service;
package com.happy.friendogly.footprint.service;

import com.happy.friendogly.deveicetoken.domain.DeviceToken;
import com.happy.friendogly.footprint.domain.Footprint;
import com.happy.friendogly.footprint.domain.WalkStatus;
import com.happy.friendogly.notification.domain.DeviceToken;
import com.happy.friendogly.notification.domain.NotificationType;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import com.happy.friendogly.deveicetoken.repository.DeviceTokenRepository;
import com.happy.friendogly.notification.service.NotificationService;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
Expand All @@ -13,7 +14,7 @@
public class FootprintNotificationService {

private static final String DEFAULT_TITLE = "반갑개";

private final NotificationService notificationService;
private final DeviceTokenRepository deviceTokenRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.happy.friendogly.member.dto.response.SaveMemberResponse;
import com.happy.friendogly.member.dto.response.UpdateMemberResponse;
import com.happy.friendogly.member.repository.MemberRepository;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import com.happy.friendogly.deveicetoken.repository.DeviceTokenRepository;
import com.happy.friendogly.pet.repository.PetRepository;
import com.happy.friendogly.utils.UuidGenerator;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ public void sendNotification(
) {

}

@Override
public void sendNotificationToTopic(String title, String content, NotificationType notificationType, String topic) {

}

@Override
public void subscribeTopic(List<String> deviceToken, String topic) {

}

@Override
public void unsubscribeTopic(List<String> deviceToken, String topic) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.MulticastMessage;
import com.happy.friendogly.exception.FriendoglyException;
import com.happy.friendogly.notification.domain.NotificationType;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -22,8 +21,8 @@ public class FcmNotificationService implements NotificationService {

private final FirebaseMessaging firebaseMessaging;

public FcmNotificationService(@Autowired FirebaseApp firebaseApp) {
this.firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp);
public FcmNotificationService(FirebaseMessaging firebaseMessaging) {
this.firebaseMessaging = firebaseMessaging;
}

@Override
Expand Down Expand Up @@ -54,9 +53,46 @@ public void sendNotification(
try {
firebaseMessaging.sendEachForMulticast(message);
} catch (FirebaseMessagingException e) {
throw new FriendoglyException("FCM을 통해 사용자에게 알림을 보내는 과정에서 에러가 발생했습니다.",
INTERNAL_SERVER_ERROR);
throw new FriendoglyException("FCM을 통해 사용자에게 알림을 보내는 과정에서 에러가 발생했습니다.", INTERNAL_SERVER_ERROR);
}
}
}

@Override
public void sendNotificationToTopic(
String title,
String content,
NotificationType notificationType,
String topic
) {
Message message = Message.builder()
.putData("body", content)
.putData("type", notificationType.toString())
.putData("title", title)
.setTopic(topic)
.build();
try {
firebaseMessaging.send(message);
} catch (FirebaseMessagingException e) {
throw new FriendoglyException("FCM을 통해 Topic으로 알림을 보내는 과정에서 에러가 발생했습니다.", INTERNAL_SERVER_ERROR);
}
}

@Override
public void subscribeTopic(List<String> deviceToken, String topic) {
try {
firebaseMessaging.subscribeToTopic(deviceToken, topic);
} catch (FirebaseMessagingException e) {
throw new FriendoglyException("FCM을 통해 Topic을 구독하는 과정에서 에러가 발생했습니다.", INTERNAL_SERVER_ERROR);
}
}

@Override
public void unsubscribeTopic(List<String> deviceToken, String topic) {
try {
firebaseMessaging.unsubscribeFromTopic(deviceToken, topic);
} catch (FirebaseMessagingException e) {
throw new FriendoglyException("FCM을 통해 Topic을 구독해제하는 과정에서 에러가 발생했습니다.", INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ void sendNotification(
NotificationType notificationType,
List<String> receiverTokens
);

void sendNotificationToTopic(
String title,
String content,
NotificationType notificationType,
String topic
);

void subscribeTopic(List<String> deviceToken, String topic);

void unsubscribeTopic(List<String> deviceToken, String topic);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.happy.friendogly.exception.FriendoglyException;
import com.happy.friendogly.member.domain.Member;
import com.happy.friendogly.member.repository.MemberRepository;
import com.happy.friendogly.notification.service.PlaygroundNotificationService;
import com.happy.friendogly.playground.domain.Location;
import com.happy.friendogly.playground.domain.Playground;
import com.happy.friendogly.playground.domain.PlaygroundMember;
Expand Down Expand Up @@ -58,7 +57,8 @@ public SavePlaygroundResponse save(SavePlaygroundRequest request, Long memberId)
Playground savedPlayground = playgroundRepository.save(
new Playground(new Location(request.latitude(), request.longitude()))
);
playgroundMemberRepository.save(new PlaygroundMember(savedPlayground, member));
PlaygroundMember playgroundMember = playgroundMemberRepository.save(new PlaygroundMember(savedPlayground, member));
playgroundNotificationService.subscribePlaygroundTopic(playgroundMember);

return new SavePlaygroundResponse(savedPlayground);
}
Expand Down Expand Up @@ -98,16 +98,15 @@ private void validateOverlapPlayground(double latitude, double longitude) {
public SaveJoinPlaygroundMemberResponse joinPlayground(Long memberId, Long playgroundId) {
Playground playground = playgroundRepository.getById(playgroundId);
Member member = memberRepository.getById(memberId);
List<PlaygroundMember> existingPlaygroundMembers = playgroundMemberRepository
.findAllByPlaygroundId(playgroundId);

validateExistParticipatingPlayground(member);

PlaygroundMember playgroundMember = playgroundMemberRepository.save(
new PlaygroundMember(playground, member)
);

playgroundNotificationService.sendJoinNotification(member.getName().getValue(), existingPlaygroundMembers);
playgroundNotificationService.sendJoinNotification(member.getName().getValue(), playground);
playgroundNotificationService.subscribePlaygroundTopic(playgroundMember);

return new SaveJoinPlaygroundMemberResponse(playgroundMember);
}
Expand All @@ -117,6 +116,7 @@ public void leavePlayground(Long memberId) {
.ifPresent(playgroundMember -> {
playgroundMemberRepository.delete(playgroundMember);
deletePlaygroundConditional(playgroundMember.getPlayground());
playgroundNotificationService.unsubscribePlaygroundTopic(playgroundMember);
});
}

Expand Down Expand Up @@ -164,6 +164,7 @@ public void deleteJoinMemberIntervalTime() {
).toList();

playgroundMemberRepository.deleteAll(deletePlaygroundMembers);
playgroundNotificationService.unsubscribeAllMemberForPlayground(deletePlaygroundMembers);

List<Long> deletePlaygroundCandidate = deletePlaygroundMembers.stream()
.map(playgroundMember -> playgroundMember.getPlayground().getId())
Expand Down
Loading