Browse Source

BMC: Added FireStoreNotificationModel

feature/ModificationForSentNotificationAPI
Palash Biswas 2 years ago
parent
commit
91a8f80b32
5 changed files with 114 additions and 45 deletions
  1. +1
    -0
      BCS.BMC/src/BCS.BMC.Application/BCS.BMC.Application.csproj
  2. +31
    -0
      BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FireStoreNotificationModel.cs
  3. +16
    -13
      BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/NotificationModel.cs
  4. +1
    -0
      BCS.BMC/src/BCS.BMC.Web.Mvc/BCS.BMC.Web.Mvc.csproj
  5. +65
    -32
      BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/NotificationController.cs

+ 1
- 0
BCS.BMC/src/BCS.BMC.Application/BCS.BMC.Application.csproj View File

@ -20,6 +20,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="FirebaseAdmin" Version="2.3.0" /> <PackageReference Include="FirebaseAdmin" Version="2.3.0" />
<PackageReference Include="FireSharp" Version="2.0.4" /> <PackageReference Include="FireSharp" Version="2.0.4" />
<PackageReference Include="Google.Cloud.Firestore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.9" /> <PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" /> <PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
</ItemGroup> </ItemGroup>

+ 31
- 0
BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/FireStoreNotificationModel.cs View File

@ -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<string> 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; }
}
}

+ 16
- 13
BCS.BMC/src/BCS.BMC.Application/FirebaseCloudMessaging/Dto/NotificationModel.cs View File

@ -14,22 +14,25 @@ namespace BCS.BMC.FirebaseCloudMessaging.Dto
public class NotificationModel public class NotificationModel
{ {
[JsonProperty("title")]
public List<string> Title { get; set; }
[JsonProperty("body")]
public string Body { get; set; }
public int UserId { get; set; }
//[JsonProperty("title")]
//public List<string> Title { get; set; }
//[JsonProperty("body")]
public string FcmToken { get; set; }
public List<string> Id { get; set; }
public string Message { get; set; }
public DateTime? MessageSentDateTime { get; set; }
public string SenderImageurl { get; set; } public string SenderImageurl { get; set; }
public string SenderName { get; set; } public string SenderName { get; set; }
public DateTime? MessageSentDateTime { get; set; }
public bool Status { 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<string> Id { get; set; }
//}
} }

+ 1
- 0
BCS.BMC/src/BCS.BMC.Web.Mvc/BCS.BMC.Web.Mvc.csproj View File

@ -33,6 +33,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" /> <PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
<PackageReference Include="FireSharp" Version="2.0.4" /> <PackageReference Include="FireSharp" Version="2.0.4" />
<PackageReference Include="Google.Cloud.Firestore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.4" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>


+ 65
- 32
BCS.BMC/src/BCS.BMC.Web.Mvc/Controllers/NotificationController.cs View File

@ -11,10 +11,12 @@ using FirebaseAdmin.Messaging;
using FireSharp.Config; using FireSharp.Config;
using FireSharp.Interfaces; using FireSharp.Interfaces;
using FireSharp.Response; using FireSharp.Response;
using Google.Cloud.Firestore;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace BCS.BMC.Web.Controllers namespace BCS.BMC.Web.Controllers
@ -23,60 +25,91 @@ namespace BCS.BMC.Web.Controllers
[Route("api/[controller]/[action]")] [Route("api/[controller]/[action]")]
public class NotificationController : BMCControllerBase public class NotificationController : BMCControllerBase
{ {
private readonly IFirebaseNotificationAppService _notificationService; private readonly IFirebaseNotificationAppService _notificationService;
private readonly IRepository<FirebaseCloudMessageDetails> _firebaseCloudMessageDetails; private readonly IRepository<FirebaseCloudMessageDetails> _firebaseCloudMessageDetails;
IFirebaseConfig config = new FirebaseConfig
{
AuthSecret = "n6DTPQEbpnLahrrk2EYu4bJ2Wd3jCk2N8kocazdI",
BasePath = "https://firechat-57601-default-rtdb.europe-west1.firebasedatabase.app/"
};
IFirebaseClient client;
private readonly IRepository<FirebaseToken, int> _firebaseToken;
//IFirebaseConfig config = new FirebaseConfig
//{
// AuthSecret = "n6DTPQEbpnLahrrk2EYu4bJ2Wd3jCk2N8kocazdI",
// BasePath = "https://firechat-57601-default-rtdb.europe-west1.firebasedatabase.app/"
//};
//IFirebaseClient client;
public NotificationController(IFirebaseNotificationAppService notificationService, public NotificationController(IFirebaseNotificationAppService notificationService,
IRepository<FirebaseCloudMessageDetails> firebaseCloudMessageDetails)
IRepository<FirebaseCloudMessageDetails> firebaseCloudMessageDetails,
IRepository<FirebaseToken, int> firebaseToken)
{ {
_notificationService = notificationService; _notificationService = notificationService;
_firebaseCloudMessageDetails = firebaseCloudMessageDetails;
_firebaseCloudMessageDetails = firebaseCloudMessageDetails;
_firebaseToken = firebaseToken;
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> SendNotification([FromBody] FireBaseResponseModel notification) public async Task<IActionResult> 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); return Ok(result.Responses);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> GetNotifications([FromBody]NotificationModel notification) public async Task<IActionResult> GetNotifications([FromBody]NotificationModel notification)
{ {
var model = ObjectMapper.Map<FirebaseNotification>(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 try
{
var entity = notification.MapTo<FirebaseCloudMessageDetails>();
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<string> tokenList = new List<string>();
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(); return Ok();
} }
} }
}
}

Loading…
Cancel
Save