public async Task SendToIds(List <NotifyMessage> notifyMessages, string title, string body, NotificationType notificationType, AppType appType, string sound, string additionalData = "") { using (var sender = new Sender(GetApiServerKey(appType))) { if (notifyMessages .Any(item => item.OsType == OsType.Android)) { var androidMessage = new FCM.Net.Message { RegistrationIds = notifyMessages .Where(item => item.OsType == OsType.Android) .Select(item => item.RegistrationId).ToList(), Data = new { title, body, notificationType, additionalData } }; var androidResult = await sender.SendAsync(androidMessage); if (androidResult.StatusCode != HttpStatusCode.OK || androidResult.MessageResponse.Success != 1) { var logger = LogManager.GetCurrentClassLogger(); logger.Error(androidResult); } } if (notifyMessages.Any(item => item.OsType == OsType.Ios)) { var iosMessage = new FCM.Net.Message { RegistrationIds = notifyMessages .Where(item => item.OsType == OsType.Ios) .Select(item => item.RegistrationId).ToList(), Data = new { title, body, notificationType, additionalData }, Notification = new Notification { Title = title, Body = body, Badge = "1", Sound = sound } }; var iosResult = await sender.SendAsync(iosMessage); if (iosResult.StatusCode != HttpStatusCode.OK || iosResult.MessageResponse.Success != 1) { var logger = LogManager.GetCurrentClassLogger(); logger.Error(iosResult); } } } }
public async Task SendToTopic(string title, string body, NotificationType notificationType, AppType appType, string sound) { using (var sender = new Sender(GetApiServerKey(appType))) { var message = new FCM.Net.Message { To = $"/topics/{GetTopicName(appType)}", Data = new { title, body, notificationType } }; var result = await sender.SendAsync(message); if (result.StatusCode != HttpStatusCode.OK) { var logger = LogManager.GetCurrentClassLogger(); logger.Error(result); } var messageIos = new FCM.Net.Message { To = $"/topics/{GetIosTopicName(appType)}", Data = new { title, body, notificationType }, Notification = new Notification { Title = title, Body = body, Badge = "1", Sound = sound } }; var resultIos = await sender.SendAsync(messageIos); if (resultIos.StatusCode != HttpStatusCode.OK) { var logger = LogManager.GetCurrentClassLogger(); logger.Error(result); } } }
public async Task <ResponseViewModel> SendNotification(NotificationInputModel notification) { using (var sender = new Sender(_setting.FireBaseSenderKey)) { var message = new FCM.Net.Message { RegistrationIds = notification.RegistrationIds, Notification = notification.Notification }; var result = await sender.SendAsync(message); Console.WriteLine($"Success: {result.MessageResponse.Success}"); //var json = "{\"notification\":{\"title\":\"json message\",\"body\":\"works like a charm!\"},\"to\":\"" + registrationId + "\"}"; //result = await sender.SendAsync(json); //Console.WriteLine($"Success: {result.MessageResponse.Success}"); } return(ResponseViewModel.Ok()); }