public Task SendAsync(IEnumerable <string> clientTokens, string title, string body, Uri imageUri = null)
        {
            var notification = new Notification {
                Body = body, Title = title, ImageUrl = imageUri?.AbsoluteUri
            };
            var message = new MulticastMessage {
                Tokens = clientTokens.ToList(), Notification = notification
            };

            return(_firebaseMessaging.SendMulticastAsync(message));
        }
Exemplo n.º 2
0
        public async Task <MessageSendResponse> SendMulticast(MessageSendRequest request)
        {
            var message = new MulticastMessage()
            {
                Notification = new Notification()
                {
                    Title = request.NotificationTitle,
                    Body  = request.NotificationBody,
                },
                Tokens = request.Tokens,
            };

            var response = await _firebaseService.SendMulticastAsync(message);

            var returnResponse = new MessageSendResponse()
            {
                Responses = response.Responses.Select(x => new ResponseItem
                {
                    IsSuccess = x.IsSuccess,
                    MessageId = x.MessageId,
                    Exception = x.IsSuccess ? null : new ExceptionItem
                    {
                        Message            = x.Exception.Message,
                        MessagingErrorCode = x.Exception.MessagingErrorCode.HasValue ? (int)x.Exception.MessagingErrorCode : 0
                    }
                }),
                FailureCount = response.FailureCount,
                SuccessCount = response.SuccessCount
            };

            return(returnResponse);
        }
        public async Task SendNotificationToUser(User user, string message, string subject = "eTutor", Dictionary <string, string> data = null)
        {
            var devices = await _deviceRepository.FindAll(u => u.UserId == user.Id);

            var deviceTokens = devices.Select(d => d.FcmToken).Distinct().ToArray();

            if (deviceTokens.Length > 0)
            {
                var multicastMessage = new MulticastMessage
                {
                    Tokens       = deviceTokens,
                    Notification = new Notification
                    {
                        Body  = message,
                        Title = subject
                    },
                    Data = data
                };

                await _firebaseMessaging.SendMulticastAsync(multicastMessage);
            }
        }
Exemplo n.º 4
0
        public async Task SendMulticastNotification(List <string> tokens, string notificationBody,
                                                    string notificationTitle)
        {
            if (_messaging != null)
            {
                var result = await _messaging.SendMulticastAsync(CreateMulticastMessage(tokens, notificationBody, notificationTitle));

                _logger.LogInformation($"Sent multicast notification.");
            }
            else
            {
                _logger.LogError("No FCM application credentials found.");
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> TriggerMessage([FromBody] TriggerMessageRequest request)
        {
            //var clientTokens = new List<string>();
            var registrationTokens = request.UserIds.ToList();

            var message = new MulticastMessage()
            {
                Tokens       = registrationTokens,
                Notification = new Notification()
                {
                    Title = request.Title,
                    Body  = request.Message
                }
            };
            var result = await messaging.SendMulticastAsync(message);

            //var response = FirebaseMessaging.GetMessaging(FirebaseApp.GetInstance("salesforce247app")).SendMulticastAsync(message).ConfigureAwait(true);
            app.Delete();
            return(Ok(true));
        }
Exemplo n.º 6
0
        // Methods
        public void Send(Notification notification, List <Registration> registrationList)
        {
            #region Contracts

            if (notification == null)
            {
                throw new ArgumentException(nameof(notification));
            }
            if (registrationList == null)
            {
                throw new ArgumentException(nameof(registrationList));
            }

            #endregion

            // Message
            var message = new FirebaseAdmin.Messaging.MulticastMessage()
            {
                Notification = new FirebaseAdmin.Messaging.Notification()
                {
                    Title = notification.Title,
                    Body  = notification.Text,
                },
                Tokens = registrationList.Select(registration => registration.Token).ToList()
            };

            // Formatter
            foreach (var firebaseFormatter in _firebaseFormatterList.Where(x => x.NotificationType == notification.Type))
            {
                // Configure
                firebaseFormatter.Configure(message);
            }

            // Send
            var response = _firebaseMessaging.SendMulticastAsync(message).GetAwaiter().GetResult();
            if (response == null)
            {
                throw new InvalidOperationException($"{nameof(response)}=null");
            }
        }
        public async Task Send <T>(T messageDetails, string uid) where T : NotificationDetails
        {
            if (_fcmTokenProvider == null)
            {
                throw new ApplicationException($"{nameof(_fcmTokenProvider)} is null, please call {nameof(WithTokenProvider)} first.");
            }

            var tokens = await _fcmTokenProvider.GetRegistrationTokens(uid);

            if (!tokens.Any())
            {
                _logger.LogInformation($"{messageDetails.GetType().Name} ({messageDetails.ToJson()}) message is failed. Tokens for {uid} not found");
                return;
            }
            var registrationTokens = tokens.Select(x => x.Token).ToArray();
            var message            = _messageFactory.Build(registrationTokens, messageDetails);

            _logger.LogInformation($"Sending {messageDetails.GetType().Name} ({messageDetails.ToJson()}) message to UID={uid}");
            var response = await _firebaseMessaging.SendMulticastAsync(message);

            await ProcessFailedTokensIfAny <T>(_fcmTokenProvider, response, tokens);
        }
Exemplo n.º 8
0
        public async Task <bool> SendBulkAsync(string[] registeredUserDeviceTokens, string title, string body)
        {
            BatchResponse result = await _firebaseMessaging.SendMulticastAsync(Notification(registeredUserDeviceTokens, title, body));

            return(result.SuccessCount > 0);
        }