From f822cc9fe9eba5203dc42feaada62bb948a513ee Mon Sep 17 00:00:00 2001 From: Palash Biswas Date: Tue, 29 Nov 2022 10:16:10 +0530 Subject: [PATCH] BMC: Set FCMTokent In Body --- .../Dto/FirebaseResponseModel.cs | 9 ++++---- .../FirebaseNotificationAppService.cs | 21 ++++++++++-------- .../Models/TokenAuth/FcmTokenResponseModel.cs | 22 +++++++++++++++++++ .../FirebaseNotificationController.cs | 13 +++++++---- 4 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/FcmTokenResponseModel.cs diff --git a/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FirebaseResponseModel.cs b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FirebaseResponseModel.cs index 04e40d0..3026ec9 100755 --- a/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FirebaseResponseModel.cs +++ b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FirebaseResponseModel.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using FirebaseAdmin.Messaging; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; @@ -9,9 +10,7 @@ namespace BCS.BMC.FirebaseCloudMessaging.Dto { public class FireBaseResponseModel { - [JsonProperty("isSuccess")] - public bool IsSuccess { get; set; } - [JsonProperty("message")] - public string Message { get; set; } + public Notification notification { get; set; } + public List FcmToken { get; set; } } } diff --git a/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/FirebaseNotificationAppService.cs b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/FirebaseNotificationAppService.cs index 710865a..29b3bfc 100644 --- a/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/FirebaseNotificationAppService.cs +++ b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/FirebaseNotificationAppService.cs @@ -3,7 +3,9 @@ using System.Threading.Tasks; using FirebaseAdmin; using Google.Apis.Auth.OAuth2; using System; - +using BCS.BMC.FirebaseCloudMessaging.Dto; +using System.Collections.Generic; +using Abp.Json; namespace BCS.BMC.FirebaseCloudMessaging { @@ -22,18 +24,19 @@ namespace BCS.BMC.FirebaseCloudMessaging } - public static async Task SendNotification(string fcmToken, Notification notification) + public static async Task SendNotification( FireBaseResponseModel notification) { try { - var message = new Message() - { - Notification = notification, - Token = fcmToken, - }; + + var message = new MulticastMessage() + { + Notification = notification.notification, + Tokens = notification.FcmToken, + }; - return await FirebaseMessaging.DefaultInstance.SendAsync(message) ; - + var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message); + return response; } catch(Exception ex) { diff --git a/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/FcmTokenResponseModel.cs b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/FcmTokenResponseModel.cs new file mode 100644 index 0000000..6a6b2f6 --- /dev/null +++ b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/FcmTokenResponseModel.cs @@ -0,0 +1,22 @@ +using FirebaseAdmin.Messaging; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BCS.BMC.Models.TokenAuth +{ + public class FcmTokenResponseModel + { + public result results { get; set; } + } + + public class results + { + public string message { get; set; } + + } +} + diff --git a/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/FirebaseNotificationController.cs b/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/FirebaseNotificationController.cs index 1d10cc0..5257e39 100644 --- a/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/FirebaseNotificationController.cs +++ b/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/FirebaseNotificationController.cs @@ -1,7 +1,11 @@ -using BCS.BMC.Controllers; +using Abp.Json; +using BCS.BMC.Controllers; using BCS.BMC.FirebaseCloudMessaging; +using BCS.BMC.FirebaseCloudMessaging.Dto; +using BCS.BMC.Models.TokenAuth; using FirebaseAdmin.Messaging; using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; using System.Collections.Generic; using System.Threading.Tasks; @@ -20,10 +24,11 @@ namespace BCS.BMC.Web.Controllers [Route("send")] [HttpPost] - public async Task Notification(string fcmToken, [FromBody] Notification notification) + public async Task Notification( [FromBody] FireBaseResponseModel notification) { - var result = await FirebaseNotificationAppService.SendNotification(fcmToken, notification); - return Ok(result); + var result = await FirebaseNotificationAppService.SendNotification(notification); + // FcmTokenResponseModel results = JsonConvert.DeserializeObject(result).ToJsonString(); + return Ok(result.Responses); } } }