|
2 | 2 |
|
3 | 3 | import com.google.firebase.messaging.FirebaseMessaging;
|
4 | 4 | import com.google.firebase.messaging.FirebaseMessagingException;
|
| 5 | +import com.rabbitmq.client.Channel; |
5 | 6 | import com.twtw.backend.domain.notification.dto.NotificationRequest;
|
6 | 7 | import com.twtw.backend.domain.notification.entity.Notification;
|
7 | 8 | import com.twtw.backend.domain.notification.repository.NotificationRepository;
|
8 | 9 |
|
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | + |
9 | 12 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
| 13 | +import org.springframework.amqp.support.AmqpHeaders; |
| 14 | +import org.springframework.messaging.handler.annotation.Header; |
10 | 15 | import org.springframework.stereotype.Component;
|
11 | 16 | import org.springframework.transaction.annotation.Transactional;
|
12 | 17 |
|
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Optional; |
13 | 20 | import java.util.UUID;
|
14 | 21 |
|
15 | 22 | @Component
|
| 23 | +@RequiredArgsConstructor |
16 | 24 | public class FcmConsumer {
|
17 | 25 | private final FirebaseMessaging firebaseMessaging;
|
18 | 26 | private final NotificationRepository notificationRepository;
|
19 | 27 |
|
20 |
| - public FcmConsumer( |
21 |
| - FirebaseMessaging firebaseMessaging, NotificationRepository notificationRepository) { |
22 |
| - this.firebaseMessaging = firebaseMessaging; |
23 |
| - this.notificationRepository = notificationRepository; |
24 |
| - } |
25 |
| - |
26 | 28 | @Transactional
|
27 | 29 | @RabbitListener(queues = "notification.queue")
|
28 |
| - public void sendNotification(final NotificationRequest request) |
29 |
| - throws FirebaseMessagingException { |
| 30 | + public void sendNotification( |
| 31 | + final NotificationRequest request, |
| 32 | + final Channel channel, |
| 33 | + @Header(AmqpHeaders.DELIVERY_TAG) final long tag) |
| 34 | + throws FirebaseMessagingException, IOException { |
30 | 35 | firebaseMessaging.send(request.toMessage());
|
31 | 36 |
|
32 |
| - notificationRepository |
33 |
| - .findById(UUID.fromString(request.getNotificationId())) |
34 |
| - .ifPresent(Notification::complete); |
| 37 | + final Optional<Notification> notification = |
| 38 | + notificationRepository.findById(UUID.fromString(request.getNotificationId())); |
| 39 | + |
| 40 | + if (notification.isPresent()) { |
| 41 | + notification.get().complete(); |
| 42 | + return; |
| 43 | + } |
| 44 | + channel.basicNack(tag, false, false); |
35 | 45 | }
|
36 | 46 | }
|
0 commit comments