| | | 1 | | using FirebaseAdmin.Messaging; |
| | | 2 | | using KT.Modules.Alarm.Presentation.Contracts; |
| | | 3 | | using KT.Modules.Alarm.Presentation.Dto; |
| | | 4 | | using KT.Modules.Security.Presentation.Contracts; |
| | | 5 | | |
| | | 6 | | namespace KT.Modules.Alarm.Core.Application |
| | | 7 | | { |
| | 3 | 8 | | internal class NotificationService(IDeviceTokenService deviceTokenService, IAllConnectionsService allConnectionsServ |
| | | 9 | | { |
| | | 10 | | public async Task SendNotificationAsync(NotificationDto notificationDto) |
| | 3 | 11 | | { |
| | 3 | 12 | | var devicesId = new List<string>(); |
| | 3 | 13 | | var userAsociated = await allConnectionsService.GetAsync(notificationDto.UserId); |
| | 3 | 14 | | if (userAsociated.Count <= 0) |
| | 1 | 15 | | throw new InvalidOperationException("There are no contacts to alert"); |
| | 12 | 16 | | foreach (ConnectionDto user in userAsociated) |
| | 3 | 17 | | { |
| | 3 | 18 | | string deviceId = await deviceTokenService.GetAsync(user.UserId); |
| | 3 | 19 | | if (!string.IsNullOrEmpty(deviceId)) |
| | 2 | 20 | | devicesId.Add(deviceId); |
| | 3 | 21 | | } |
| | 2 | 22 | | if (devicesId.Count <= 0) |
| | 1 | 23 | | throw new InvalidOperationException("Users do not have a device token."); |
| | 1 | 24 | | await FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(CreateNotification(notificationDto, device |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | private MulticastMessage CreateNotification(NotificationDto notificationDto, List<string> devicesId) |
| | 1 | 28 | | { |
| | 1 | 29 | | var message = new MulticastMessage() |
| | 1 | 30 | | { |
| | 1 | 31 | | Tokens = devicesId, |
| | 1 | 32 | | Data = new Dictionary<string, string>() |
| | 1 | 33 | | { |
| | 1 | 34 | | { "title", notificationDto.Title }, |
| | 1 | 35 | | { "body", notificationDto.Message }, |
| | 1 | 36 | | { "payload", notificationDto.Payload } |
| | 1 | 37 | | } |
| | 1 | 38 | | }; |
| | 1 | 39 | | return message; |
| | 1 | 40 | | } |
| | | 41 | | } |
| | | 42 | | } |