From 91a8f80b321473158d7961ea314ba87821c7d6a5 Mon Sep 17 00:00:00 2001 From: Palash Biswas Date: Fri, 9 Dec 2022 11:11:10 +0530 Subject: [PATCH] BMC: Added FireStoreNotificationModel --- .../BCS.BMC.Application.csproj | 1 + .../Dto/FireStoreNotificationModel.cs | 31 ++++++ .../Dto/NotificationModel.cs | 29 +++--- .../BCS.BMC.Web.Mvc/BCS.BMC.Web.Mvc.csproj | 1 + .../Controllers/NotificationController.cs | 97 +++++++++++++------ 5 files changed, 114 insertions(+), 45 deletions(-) create mode 100644 BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FireStoreNotificationModel.cs diff --git a/BCS.BMC/src/BCS.BMC.Application/BCS.BMC.Application.csproj b/BCS.BMC/src/BCS.BMC.Application/BCS.BMC.Application.csproj index 0e8923e..0767a8c 100644 --- a/BCS.BMC/src/BCS.BMC.Application/BCS.BMC.Application.csproj +++ b/BCS.BMC/src/BCS.BMC.Application/BCS.BMC.Application.csproj @@ -20,6 +20,7 @@ + diff --git a/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FireStoreNotificationModel.cs b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FireStoreNotificationModel.cs new file mode 100644 index 0000000..c2721d9 --- /dev/null +++ b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FireStoreNotificationModel.cs @@ -0,0 +1,31 @@ +using Google.Cloud.Firestore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BCS.BMC.FirebaseCloudMessaging.Dto +{ + [FirestoreData] + public class FireStoreNotificationModel + { + [FirestoreProperty] + public string FcmToken { get; set; } + [FirestoreProperty] + public List Id { get; set; } + [FirestoreProperty] + public string Message { get; set; } + [FirestoreProperty] + public DateTime? MessageSentDateTime { get; set; } + [FirestoreProperty] + + public string SenderImageurl { get; set; } + [FirestoreProperty] + public string SenderName { get; set; } + [FirestoreProperty] + public bool Status { get; set; } + [FirestoreProperty] + public int UserId { get; set; } + } +} diff --git a/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/NotificationModel.cs b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/NotificationModel.cs index 4ae421f..d11bc15 100644 --- a/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/NotificationModel.cs +++ b/BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/NotificationModel.cs @@ -14,22 +14,25 @@ namespace BCS.BMC.FirebaseCloudMessaging.Dto public class NotificationModel { - - [JsonProperty("title")] - public List Title { get; set; } - [JsonProperty("body")] - public string Body { get; set; } - public int UserId { get; set; } + + //[JsonProperty("title")] + //public List Title { get; set; } + //[JsonProperty("body")] + public string FcmToken { get; set; } + public List Id { get; set; } + public string Message { get; set; } + public DateTime? MessageSentDateTime { get; set; } + public string SenderImageurl { get; set; } public string SenderName { get; set; } - public DateTime? MessageSentDateTime { get; set; } + public bool Status { get; set; } - public string Message { get; set; } + public int UserId { get; set; } } - [AutoMapFrom(typeof(NotificationModel))] - public class FirebaseNotification : NotificationModel - { - public string Id { get; set; } - } + //[AutoMapFrom(typeof(NotificationModel))] + //public class FirebaseNotification : NotificationModel + //{ + // public List Id { get; set; } + //} } diff --git a/BCS.BMC/src/BCS.BMC.Web.Mvc/BCS.BMC.Web.Mvc.csproj b/BCS.BMC/src/BCS.BMC.Web.Mvc/BCS.BMC.Web.Mvc.csproj index 32b54c1..aac9f67 100644 --- a/BCS.BMC/src/BCS.BMC.Web.Mvc/BCS.BMC.Web.Mvc.csproj +++ b/BCS.BMC/src/BCS.BMC.Web.Mvc/BCS.BMC.Web.Mvc.csproj @@ -33,6 +33,7 @@ + all diff --git a/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/NotificationController.cs b/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/NotificationController.cs index 5748b92..f014414 100644 --- a/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/NotificationController.cs +++ b/BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/NotificationController.cs @@ -11,10 +11,12 @@ using FirebaseAdmin.Messaging; using FireSharp.Config; using FireSharp.Interfaces; using FireSharp.Response; +using Google.Cloud.Firestore; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; namespace BCS.BMC.Web.Controllers @@ -23,60 +25,91 @@ namespace BCS.BMC.Web.Controllers [Route("api/[controller]/[action]")] public class NotificationController : BMCControllerBase { + private readonly IFirebaseNotificationAppService _notificationService; private readonly IRepository _firebaseCloudMessageDetails; - IFirebaseConfig config = new FirebaseConfig - { - AuthSecret = "n6DTPQEbpnLahrrk2EYu4bJ2Wd3jCk2N8kocazdI", - BasePath = "https://firechat-57601-default-rtdb.europe-west1.firebasedatabase.app/" - }; - IFirebaseClient client; + private readonly IRepository _firebaseToken; + //IFirebaseConfig config = new FirebaseConfig + //{ + // AuthSecret = "n6DTPQEbpnLahrrk2EYu4bJ2Wd3jCk2N8kocazdI", + // BasePath = "https://firechat-57601-default-rtdb.europe-west1.firebasedatabase.app/" + //}; + //IFirebaseClient client; public NotificationController(IFirebaseNotificationAppService notificationService, - IRepository firebaseCloudMessageDetails) + IRepository firebaseCloudMessageDetails, + IRepository firebaseToken) { _notificationService = notificationService; - _firebaseCloudMessageDetails = firebaseCloudMessageDetails; - - + _firebaseCloudMessageDetails = firebaseCloudMessageDetails; + _firebaseToken = firebaseToken; } [HttpPost] public async Task SendNotification([FromBody] FireBaseResponseModel notification) { - var result = await _notificationService.SendNotification(notification); + var result = await _notificationService.SendNotification(notification); + //To Do + //if () + //{ + // return Ok(result); + //} return Ok(result.Responses); } [HttpPost] public async Task GetNotifications([FromBody]NotificationModel notification) { - var model = ObjectMapper.Map(notification); + string path = AppDomain.CurrentDomain.BaseDirectory + @"firechat-57601-firebase-adminsdk-anscp-e04366c4d4.json"; + Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path); - client = new FireSharp.FirebaseClient(config); - var data = notification; - PushResponse response = client.Push("notification/", model); - model.Id = response.Result.name; - SetResponse setResponse = client.Set("notification/" + model.Id, model); - if (setResponse.StatusCode == System.Net.HttpStatusCode.OK) - { - ModelState.AddModelError(string.Empty, "Added Succesfully"); - } - else - { - ModelState.AddModelError(string.Empty, "Something went wrong!!"); - } try - { - var entity = notification.MapTo(); - await _firebaseCloudMessageDetails.InsertAndGetIdAsync(entity); - }catch(Exception ex) + { + FirestoreDb db = FirestoreDb.Create("firechat-57601"); + FireStoreNotificationModel data = new FireStoreNotificationModel(); + + data.FcmToken = notification.FcmToken; + data.Id = notification.Id; + data.Message = notification.Message; + //data.MessageSentDateTime = notification.MessageSentDateTime; + data.MessageSentDateTime = DateTime.UtcNow; + data.SenderImageurl = notification.SenderImageurl; + data.SenderName = notification.SenderName; + data.Status= notification.Status; + data.UserId = notification.UserId; + await db.Collection("BMC_Notification").Document().SetAsync(data); + var getTokenDetails = _firebaseToken.GetAllList().Where(x => x.UserId == notification.UserId).FirstOrDefault(); + List tokenList = new List(); + tokenList.Add("cXo7NDdXTFSA5ZBgDY3cz7:APA91bGXCLGrStsK7MHFeKXW2zGrfCtpC0yMj-7Ir1g4Q4iakIZ_By71xIKkIL_pMlw8don-YBgkDC9_jtWxNZjAtqT0cH_tpJSGr6j9Bx9iNIH9sfCYhqsuBPSUAL_nFkp8wkivsphx"); + + if (getTokenDetails != null) + { + var sendNotificationInfo = new FireBaseResponseModel(); + sendNotificationInfo.FcmToken = tokenList; + sendNotificationInfo.notification.Title = "Bwac Patrol Monitoring Alert"; + sendNotificationInfo.notification.Body = notification.Message; + await SendNotification(sendNotificationInfo); + } + + + var entity = new FirebaseCloudMessageDetails(); + entity.UserId = notification.UserId; + entity.SenderImageurl = notification.SenderImageurl; + entity.SenderName = notification.SenderName; + entity.MessageSentDateTime = notification.MessageSentDateTime; + entity.Status = notification.Status; + entity.Message = notification.Message; + + if(entity != null) + { + await _firebaseCloudMessageDetails.InsertAndGetIdAsync(entity); + } + } + catch(Exception ex) { } - - // var result = _notificationService.GetNotification(notification); return Ok(); } } -} +} \ No newline at end of file