Skip to content

Commit

Permalink
test: #39 push notification test api 개방
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Feb 27, 2024
1 parent 1b07fc9 commit 32e088b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 32 deletions.
5 changes: 4 additions & 1 deletion fitapet-app-external-api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

## TEST API ##
## **/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kr.co.fitapet.api.apis.test;

import io.swagger.v3.oas.annotations.tags.Tag;
import kr.co.fitapet.api.common.security.authentication.CustomUserDetails;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "푸시 테스트 API", description = "push notification을 위한 개발자용 임시 API 입니다. 테스트 후 삭제 예정")
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/v2/test/members")
public class NotificationTestController {
private final NotificationTestService notificationTestService;

@RequestMapping("/push")
@PreAuthorize("isAuthenticated()")
public void push(@AuthenticationPrincipal CustomUserDetails user) {
notificationTestService.push(user.getUserId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package kr.co.fitapet.api.apis.test;

import kr.co.fitapet.domain.domains.device.domain.DeviceToken;
import kr.co.fitapet.domain.domains.device.service.DeviceTokenSearchService;
import kr.co.fitapet.infra.client.fcm.NotificationDataKey;
import kr.co.fitapet.infra.client.fcm.NotificationService;
import kr.co.fitapet.infra.client.fcm.request.NotificationRequest;
import kr.co.fitapet.infra.client.fcm.request.NotificationSingleRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class NotificationTestService {
private final DeviceTokenSearchService deviceTokenSearchService;
private final NotificationService notificationService;

public void push(Long userId) {
log.info("push to {}", userId);

List<DeviceToken> deviceTokens = deviceTokenSearchService.findDeviceTokensByMemberId(userId);
log.info("deviceTokens: {}", deviceTokens);

log.info("send push message");
deviceTokens.forEach(deviceToken -> {
NotificationSingleRequest request = NotificationSingleRequest.builder()
.token(deviceToken.getDeviceToken())
.title("푸시 테스트")
.content("푸시 테스트 메시지")
.build();

notificationService.sendMessage(request);
});

log.info("send push message with image");
deviceTokens.forEach(deviceToken -> {
NotificationSingleRequest request = NotificationSingleRequest.builder()
.title("이미지 테스트")
.content("푸시 이미지 테스트 메시지")
.imageUrl("https://pkcy.kr.object.ncloudstorage.com/profile/0bb25dde9020.jpeg")
.build();

notificationService.sendMessage(request);
});

log.info("send push message with data");
deviceTokens.forEach(deviceToken -> {
NotificationSingleRequest request = NotificationSingleRequest.builder()
.title("데이터 테스트")
.content("푸시 데이터 테스트 메시지")
.data(
Map.of(
NotificationDataKey.NOTICE_TYPE.getField(), "TEST_TYPE",
NotificationDataKey.TO_ID.getField(), userId.toString()
)
)
.build();

notificationService.sendMessage(request);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ public class FcmNotificationServiceImpl implements NotificationService {
private final FirebaseMessaging firebaseMessaging;

@Override
public void sendMessage(NotificationSingleRequest request) throws FirebaseMessagingException {
public void sendMessage(NotificationSingleRequest request) {
Message message = request.buildSendMessage().setApnsConfig(getApnsConfigToTopic(request)).build();

ApiFuture<String> response = firebaseMessaging.sendAsync(message);
log.info("Successfully sent message: " + response);
}

@Override
public void sendMessages(NotificationMulticastRequest request) throws FirebaseMessagingException {
public void sendMessages(NotificationMulticastRequest request) {
MulticastMessage messages = request.buildSendMessage().setApnsConfig(getApnsConfigToTopic(request)).build();

ApiFuture<BatchResponse> response = firebaseMessaging.sendEachForMulticastAsync(messages);
log.info("Successfully sent message: " + response);
}

@Override
public void sendMessagesToTopic(NotificationTopicRequest request) throws FirebaseMessagingException {
public void sendMessagesToTopic(NotificationTopicRequest request) {
Message message = request.buildSendMessage().setApnsConfig(getApnsConfigToTopic(request)).build();

ApiFuture<String> response = firebaseMessaging.sendAsync(message);
log.info("Successfully sent message: " + response);
}

@Override
public void subScribe(String topic, List<String> memberTokens) throws FirebaseMessagingException {
public void subScribe(String topic, List<String> memberTokens) {
firebaseMessaging.subscribeToTopicAsync(memberTokens, topic);
}

@Override
public void unSubScribe(String topic, List<String> memberTokens) throws FirebaseMessagingException {
public void unSubScribe(String topic, List<String> memberTokens) {
firebaseMessaging.unsubscribeFromTopicAsync(memberTokens, topic);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import java.util.List;

public interface NotificationService {
void sendMessage(NotificationSingleRequest request) throws FirebaseMessagingException;
void sendMessages(NotificationMulticastRequest request) throws FirebaseMessagingException;
void sendMessagesToTopic(NotificationTopicRequest request) throws FirebaseMessagingException;
void subScribe(String topic, List<String> memberTokens) throws FirebaseMessagingException;
void unSubScribe(String topic, List<String> memberTokens) throws FirebaseMessagingException;
void sendMessage(NotificationSingleRequest request);
void sendMessages(NotificationMulticastRequest request);
void sendMessagesToTopic(NotificationTopicRequest request);
void subScribe(String topic, List<String> memberTokens);
void unSubScribe(String topic, List<String> memberTokens);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ public void handleTokenEvent(NotificationSingleEvent event) {
log.info("handleTokensEvent: {}", event);
NotificationSingleRequest request = NotificationSingleRequest.fromEvent(event);

try {
notificationService.sendMessage(request);
} catch (FirebaseException e) {
log.error("Failed to send FCM message", e);
e.printStackTrace();
return;
}
notificationService.sendMessage(request);

log.info("Successfully sent FCM message");
}
Expand All @@ -57,13 +51,7 @@ public void handleTokensEvent(NotificationMulticastEvent event) {
log.info("handleTokensEvent: {}", event);
NotificationMulticastRequest request = NotificationMulticastRequest.fromEvent(event);

try {
notificationService.sendMessages(request);
} catch (FirebaseException e) {
log.error("Failed to send FCM message", e);
e.printStackTrace();
return;
}
notificationService.sendMessages(request);

log.info("Successfully sent FCM message");
}
Expand All @@ -83,13 +71,7 @@ public void handleTopicEvent(NotificationTopicEvent event) {
log.info("handleTopicEvent: {}", event);
NotificationTopicRequest request = NotificationTopicRequest.fromEvent(event);

try {
notificationService.sendMessagesToTopic(request);
} catch (FirebaseException e) {
log.error("Failed to send FCM message", e);
e.printStackTrace();
return;
}
notificationService.sendMessagesToTopic(request);

log.info("Successfully sent FCM message");
}
Expand Down

0 comments on commit 32e088b

Please sign in to comment.