Beispiel #1
0
        public static async Task SendFcmPushNotification(string token, Dictionary <string, string> data)
        {
            // Note that this third party FCM NuGet library uses the legacy HTTP protocol, but it's much simpler so that's fine with me
            using (var sender = new FCM.Net.Sender(FCM_SERVER_KEY))
            {
                var message = new FCM.Net.Message()
                {
                    To   = token,
                    Data = data
                };

                var resp = await sender.SendAsync(message);

                if (resp.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception(resp.ReasonPhrase);
                }
            }
        }
        public async Task SendAsync(IEnumerable <NewsNotification> notifications)
        {
            foreach (var notification in notifications)
            {
                var message = new FCM.Net.Message()
                {
                    To           = $"/topics/{notification.CategoryCode}",
                    Notification = new FCM.Net.Notification()
                    {
                        Title = "StealNews",
                        Body  = $"{notification.CategoryTitle} = {notification.CountNews}",//JsonSerializer.Serialize(notification),
                        Color = "blue"
                    }
                };

                var response = await _notificationService.SendAsync(message);

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    var warningMessage = $"FCM send {response.StatusCode} code. \nReason={response.ReasonPhrase} \nContent={response.Content}";
                    _logger.LogWarning(warningMessage);
                }
            }
        }