Beispiel #1
0
        public async Task <int> Handle(SendNotificationToAll request, CancellationToken cancellationToken)
        {
            var targetFcmTokens = _context.NotificationRegistrationData
                                  .AsNoTracking()
                                  .Where(x => x.ChannelName == request.Channel)
                                  .Select(regDataResult => regDataResult.Token)
                                  .ToList();

            var response = 0;

            if (targetFcmTokens.Count > 0)
            {
                response = _firebaseService.SendAsync(request.From, request.Title, request.Message, targetFcmTokens);
            }

            var observerIds = _context.NotificationRegistrationData
                              .AsNoTracking()
                              .Where(x => x.ChannelName == request.Channel)
                              .Select(regDataResult => regDataResult.ObserverId.ToString())
                              .ToList();
            var notification = _mapper.Map <Entities.Notification>(new NewNotificationCommand
            {
                Channel    = request.Channel,
                Title      = request.Title,
                Message    = request.Message,
                From       = request.From,
                Recipients = observerIds
            });

            _context.Notifications.AddRange(notification);
            await _context.SaveChangesAsync(cancellationToken);

            return(response);
        }
        public async Task <dynamic> SendToAll([FromBody] NotificationForAllNewModel model)
        {
            var command = new SendNotificationToAll(model.Channel, model.From, model.Title, model.Message);

            var result = await _mediator.Send(command);

            return(Task.FromResult(result));
        }