-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiverNotificationQueue.java
35 lines (31 loc) · 1.51 KB
/
ReceiverNotificationQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@Component
@RequiredArgsConstructor
public class ReceiverNotificationQueue {
private final NotificationTemplateRepository notificationTemplateRepository;
private final FirebaseMessaging firebaseMessaging;
private final TokenRepository tokenRepository;
@RabbitListener(queues = "notification-queue")
public void handleMessageFromQueue(String notificationRequet) {
ObjectMapper objectMapper = new ObjectMapper();
try {
sendNotificationByTokenAndRabit(objectMapper.readValue(notificationRequet, NotificationRequet.class));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public String sendNotificationByTokenAndRabit(NotificationRequet notificationRequet) {
NotificationTemplate notificationTemplate = notificationTemplateRepository.findByType(notificationRequet.getType());
Token token = tokenRepository.findByUserId(notificationRequet.getUserId());
Map<String, String> data = new HashMap<>();
data.put("message", notificationTemplate.getBody());
data.put("title", notificationTemplate.getTitle());
data.put("image", notificationTemplate.getImage());
data.put("url", notificationTemplate.getUrl());
Message message = Message.builder().setToken(token.getToken()).putAllData(data).build();
try {
firebaseMessaging.send(message);
return "success";
} catch (Exception e) {
throw new RuntimeException();
}
}