BMC: Individual SendNotification Functionality
This commit is contained in:
parent
a8dea50c2c
commit
a8a988f7cf
@ -8,9 +8,9 @@ namespace BCS.BMC.FirebaseCloudMessaging.Dto
|
|||||||
{
|
{
|
||||||
public class BmcMessageStatusInput
|
public class BmcMessageStatusInput
|
||||||
{
|
{
|
||||||
public List<string> UserId { get; set; }
|
public string UserId { get; set; }
|
||||||
public long? ScheduleGenerationId { get; set; }
|
public long? ScheduleGenerationId { get; set; }
|
||||||
public List<long> BmcIDs { get; set; }
|
public long? BmcID { get; set; }
|
||||||
public string Status { get; set; }
|
public string Status { get; set; }
|
||||||
public string CompanyUrl { get; set; }
|
public string CompanyUrl { get; set; }
|
||||||
public bool IsSuccess { get; set; }
|
public bool IsSuccess { get; set; }
|
||||||
|
@ -30,6 +30,6 @@ namespace BCS.BMC.FirebaseCloudMessaging.Dto
|
|||||||
[FirestoreProperty]
|
[FirestoreProperty]
|
||||||
public int CompanyId { get; set; }
|
public int CompanyId { get; set; }
|
||||||
[FirestoreProperty]
|
[FirestoreProperty]
|
||||||
public string ScheduleGenerationId { get; set; }
|
public long? ScheduleGenerationId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BCS.BMC.FirebaseCloudMessaging.Dto
|
||||||
|
{
|
||||||
|
public class FireStoreNotificationModelDto
|
||||||
|
{
|
||||||
|
public string FcmToken { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
public DateTime? MessageSentDateTime { get; set; }
|
||||||
|
|
||||||
|
public string SenderImageurl { get; set; }
|
||||||
|
public string SenderName { get; set; }
|
||||||
|
public bool Status { get; set; }
|
||||||
|
public string UserId { get; set; }
|
||||||
|
|
||||||
|
public int CompanyId { get; set; }
|
||||||
|
|
||||||
|
public long? ScheduleGenerationId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,7 +11,7 @@ namespace BCS.BMC.FirebaseCloudMessaging.Dto
|
|||||||
public class FireBaseResponseModel
|
public class FireBaseResponseModel
|
||||||
{
|
{
|
||||||
public Notification notification { get; set; }
|
public Notification notification { get; set; }
|
||||||
public List<string> FcmToken { get; set; }
|
public string FcmToken { get; set; }
|
||||||
|
|
||||||
public BmcMessageStatusInput getResponseData { get; set; }
|
public BmcMessageStatusInput getResponseData { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -22,15 +22,9 @@ namespace BCS.BMC.FirebaseCloudMessaging.Dto
|
|||||||
public string SenderName { get; set; }
|
public string SenderName { get; set; }
|
||||||
|
|
||||||
public bool Status { get; set; }
|
public bool Status { get; set; }
|
||||||
public List<string> UserId { get; set; }
|
public string UserId { get; set; }
|
||||||
public List<long> BmcIDs { get; set; }
|
public long? BmcID { get; set; }
|
||||||
public string CompanyUrl { get; set; }
|
public string CompanyUrl { get; set; }
|
||||||
public long? ScheduleGenerationId { get; set; }
|
public long? ScheduleGenerationId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
//[AutoMapFrom(typeof(NotificationModel))]
|
|
||||||
//public class FirebaseNotification : NotificationModel
|
|
||||||
//{
|
|
||||||
// public List<string> Id { get; set; }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
@ -29,10 +29,12 @@ namespace BCS.BMC.FirebaseCloudMessaging
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
List<string> token = new List<string>();
|
||||||
|
token.Add(notification.FcmToken);
|
||||||
var message = new MulticastMessage()
|
var message = new MulticastMessage()
|
||||||
{
|
{
|
||||||
Notification = notification.notification,
|
Notification = notification.notification,
|
||||||
Tokens = notification.FcmToken,
|
Tokens = token,
|
||||||
};
|
};
|
||||||
|
|
||||||
var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message);
|
var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message);
|
||||||
|
@ -20,6 +20,10 @@ using System.Linq;
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Abp.EntityFrameworkCore.Repositories;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using Abp.Domain.Uow;
|
||||||
|
using Google.Type;
|
||||||
|
|
||||||
namespace BCS.BMC.Web.Controllers
|
namespace BCS.BMC.Web.Controllers
|
||||||
{
|
{
|
||||||
@ -32,6 +36,7 @@ namespace BCS.BMC.Web.Controllers
|
|||||||
private readonly IRepository<FirebaseCloudMessageDetails> _firebaseCloudMessageDetails;
|
private readonly IRepository<FirebaseCloudMessageDetails> _firebaseCloudMessageDetails;
|
||||||
private readonly IRepository<FirebaseToken, int> _firebaseToken;
|
private readonly IRepository<FirebaseToken, int> _firebaseToken;
|
||||||
private readonly IRepository<CompanyMaster, int> _companyMasterService;
|
private readonly IRepository<CompanyMaster, int> _companyMasterService;
|
||||||
|
private readonly IUnitOfWorkManager _unitOfWorkManager;
|
||||||
//IFirebaseConfig config = new FirebaseConfig
|
//IFirebaseConfig config = new FirebaseConfig
|
||||||
//{
|
//{
|
||||||
// AuthSecret = "n6DTPQEbpnLahrrk2EYu4bJ2Wd3jCk2N8kocazdI",
|
// AuthSecret = "n6DTPQEbpnLahrrk2EYu4bJ2Wd3jCk2N8kocazdI",
|
||||||
@ -41,114 +46,150 @@ namespace BCS.BMC.Web.Controllers
|
|||||||
public NotificationController(IFirebaseNotificationAppService notificationService,
|
public NotificationController(IFirebaseNotificationAppService notificationService,
|
||||||
IRepository<FirebaseCloudMessageDetails> firebaseCloudMessageDetails,
|
IRepository<FirebaseCloudMessageDetails> firebaseCloudMessageDetails,
|
||||||
IRepository<FirebaseToken, int> firebaseToken,
|
IRepository<FirebaseToken, int> firebaseToken,
|
||||||
IRepository<CompanyMaster, int> companyMasterService)
|
IRepository<CompanyMaster, int> companyMasterService,
|
||||||
|
IUnitOfWorkManager unitOfWorkManager)
|
||||||
{
|
{
|
||||||
_notificationService = notificationService;
|
_notificationService = notificationService;
|
||||||
_firebaseCloudMessageDetails = firebaseCloudMessageDetails;
|
_firebaseCloudMessageDetails = firebaseCloudMessageDetails;
|
||||||
_firebaseToken = firebaseToken;
|
_firebaseToken = firebaseToken;
|
||||||
_companyMasterService = companyMasterService;
|
_companyMasterService = companyMasterService;
|
||||||
|
_unitOfWorkManager = unitOfWorkManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> SendNotification([FromBody] FireBaseResponseModel notification)
|
public async Task<IActionResult> SendNotification([FromBody] List<FireBaseResponseModel> notifications)
|
||||||
{
|
{
|
||||||
var result = await _notificationService.SendNotification(notification);
|
List<BmcMessageStatusInput> bmcMessageStatusInputs = new List<BmcMessageStatusInput>();
|
||||||
|
foreach (var notification in notifications)
|
||||||
|
|
||||||
if (result.SuccessCount > 0)
|
|
||||||
{
|
{
|
||||||
if (notification.getResponseData != null)
|
var result = await _notificationService.SendNotification(notification);
|
||||||
|
|
||||||
|
if (result.SuccessCount > 0)
|
||||||
{
|
{
|
||||||
notification.getResponseData.IsSuccess = true;
|
if (notification.getResponseData != null)
|
||||||
notification.getResponseData.Status = "Delivered";
|
{
|
||||||
|
notification.getResponseData.IsSuccess = true;
|
||||||
|
notification.getResponseData.Status = "Delivered";
|
||||||
|
}
|
||||||
|
|
||||||
|
// return Ok(result);
|
||||||
}
|
}
|
||||||
await UpdateBmcMessageStatus(notification.getResponseData);
|
if (result.FailureCount > 0)
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
if (result.FailureCount > 0)
|
|
||||||
{
|
|
||||||
if (notification.getResponseData != null)
|
|
||||||
{
|
{
|
||||||
notification.getResponseData.IsSuccess = false;
|
if (notification.getResponseData != null)
|
||||||
notification.getResponseData.Status = result.Responses[0].Exception.Message;
|
{
|
||||||
|
notification.getResponseData.IsSuccess = false;
|
||||||
|
notification.getResponseData.Status = result.Responses[0].Exception.Message;
|
||||||
|
}
|
||||||
|
//await UpdateBmcMessageStatus(notification.getResponseData);
|
||||||
|
return BadRequest(result.Responses);
|
||||||
}
|
}
|
||||||
await UpdateBmcMessageStatus(notification.getResponseData);
|
bmcMessageStatusInputs.Add(notification.getResponseData);
|
||||||
return BadRequest(result.Responses);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(result.Responses);
|
await UpdateBmcMessageStatus(bmcMessageStatusInputs);
|
||||||
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> GetNotifications([FromBody] NotificationModel notification)
|
public async Task<IActionResult> GetNotifications([FromBody] List<NotificationModel> notifications)
|
||||||
{
|
{
|
||||||
string path = AppDomain.CurrentDomain.BaseDirectory + @"firechat-57601-firebase-adminsdk-anscp-e04366c4d4.json";
|
string path = AppDomain.CurrentDomain.BaseDirectory + @"firechat-57601-firebase-adminsdk-anscp-e04366c4d4.json";
|
||||||
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
|
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
|
||||||
var companyDetails = _companyMasterService.GetAllList().Where(x => x.Url.Trim() == notification.CompanyUrl.Trim()).FirstOrDefault();
|
var companyDetails = _companyMasterService.GetAllList().Where(x => x.Url.Trim() == notifications.FirstOrDefault().CompanyUrl.Trim()).FirstOrDefault();
|
||||||
|
var getTokenDetails = _firebaseToken.GetAllList();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
List<FireStoreNotificationModelDto> notificationModelsList = new List<FireStoreNotificationModelDto>();
|
||||||
FirestoreDb db = FirestoreDb.Create("firechat-57601");
|
FirestoreDb db = FirestoreDb.Create("firechat-57601");
|
||||||
FireStoreNotificationModel data = new FireStoreNotificationModel();
|
var sendNotificationInfoList = new List<FireBaseResponseModel>();
|
||||||
|
var cloudMessageDetailsEntityList = new List<FirebaseCloudMessageDetails>();
|
||||||
data.FcmToken = notification.FcmToken;
|
foreach (var notification in notifications)
|
||||||
// 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;
|
|
||||||
data.CompanyId = companyDetails.Id;
|
|
||||||
data.ScheduleGenerationId = notification.ScheduleGenerationId.ToString();
|
|
||||||
await db.Collection("BMC_Notification").Document().SetAsync(data);
|
|
||||||
|
|
||||||
var responseData = new BmcMessageStatusInput();
|
|
||||||
{
|
{
|
||||||
responseData.UserId = notification.UserId;
|
FireStoreNotificationModelDto data = new FireStoreNotificationModelDto();
|
||||||
responseData.ScheduleGenerationId = notification.ScheduleGenerationId;
|
var sendNotificationInfo = new FireBaseResponseModel();
|
||||||
responseData.CompanyUrl = notification.CompanyUrl;
|
data.FcmToken = notification.FcmToken;
|
||||||
responseData.BmcIDs = notification.BmcIDs;
|
// data.Id = notification.Id;
|
||||||
}
|
data.Message = notification.Message;
|
||||||
|
data.MessageSentDateTime = notification.MessageSentDateTime;
|
||||||
int userId = 0;
|
//data.MessageSentDateTime = DateTime.UtcNow;
|
||||||
List<string> tokenList = new List<string>();
|
data.SenderImageurl = notification.SenderImageurl;
|
||||||
foreach (var item in notification.UserId)
|
data.SenderName = notification.SenderName;
|
||||||
{
|
data.Status = notification.Status;
|
||||||
userId = Convert.ToInt32(item);
|
data.UserId = notification.UserId;
|
||||||
var getTokenDetails = _firebaseToken.GetAllList().Where(x => x.UserId == userId).FirstOrDefault();
|
data.CompanyId = companyDetails.Id;
|
||||||
tokenList.Add(getTokenDetails.FcmToken.ToString());
|
data.ScheduleGenerationId = Int64.Parse(notification.ScheduleGenerationId.ToString());
|
||||||
|
//await db.Collection("BMC_Notification").Document().SetAsync(data);
|
||||||
|
notificationModelsList.Add(data);
|
||||||
|
|
||||||
var entity = new FirebaseCloudMessageDetails();
|
var entity = new FirebaseCloudMessageDetails();
|
||||||
entity.UserId = userId;
|
entity.UserId = Int32.Parse(notification.UserId);
|
||||||
entity.SenderImageurl = notification.SenderImageurl;
|
entity.SenderImageurl = notification.SenderImageurl;
|
||||||
entity.SenderName = notification.SenderName;
|
entity.SenderName = notification.SenderName;
|
||||||
entity.MessageSentDateTime = notification.MessageSentDateTime;
|
entity.MessageSentDateTime = notification.MessageSentDateTime;
|
||||||
entity.Status = notification.Status;
|
entity.Status = notification.Status;
|
||||||
entity.Message = notification.Message;
|
entity.Message = notification.Message;
|
||||||
|
cloudMessageDetailsEntityList.Add(entity);
|
||||||
|
|
||||||
if (entity != null)
|
|
||||||
|
Notification noti = new Notification
|
||||||
{
|
{
|
||||||
await _firebaseCloudMessageDetails.InsertAndGetIdAsync(entity);
|
Title = notification.SenderName.ToJsonString(),
|
||||||
|
Body = notification.Message.ToJsonString(),
|
||||||
|
//ImageUrl = null
|
||||||
|
};
|
||||||
|
var responseData = new BmcMessageStatusInput();
|
||||||
|
{
|
||||||
|
responseData.UserId = notification.UserId;
|
||||||
|
responseData.ScheduleGenerationId = notification.ScheduleGenerationId;
|
||||||
|
responseData.CompanyUrl = notification.CompanyUrl;
|
||||||
|
responseData.BmcID = notification.BmcID;
|
||||||
}
|
}
|
||||||
|
var uID = Convert.ToInt32(notification.UserId);
|
||||||
|
var tokenDetails = getTokenDetails.FirstOrDefault(x => x.UserId == uID);
|
||||||
|
|
||||||
|
sendNotificationInfo.notification = noti;
|
||||||
|
sendNotificationInfo.getResponseData = responseData;
|
||||||
|
sendNotificationInfo.FcmToken = tokenDetails.FcmToken;
|
||||||
|
|
||||||
|
sendNotificationInfoList.Add(sendNotificationInfo);
|
||||||
}
|
}
|
||||||
var title = notification.SenderName.ToJsonString();
|
var getAllFireStoreData = from table in notificationModelsList
|
||||||
var body = notification.Message.ToJsonString();
|
group table by table.Message
|
||||||
var sendNotificationInfo = new FireBaseResponseModel();
|
into grp
|
||||||
|
select new FireStoreNotificationModel
|
||||||
|
{
|
||||||
|
FcmToken = grp.FirstOrDefault().FcmToken,
|
||||||
|
Message = grp.FirstOrDefault().Message,
|
||||||
|
MessageSentDateTime = grp.FirstOrDefault().MessageSentDateTime,
|
||||||
|
//data.MessageSentDateTime = DateTime.UtcNow;
|
||||||
|
SenderImageurl = grp.FirstOrDefault().SenderImageurl,
|
||||||
|
SenderName = grp.FirstOrDefault().SenderName,
|
||||||
|
Status = grp.FirstOrDefault().Status,
|
||||||
|
UserId = grp.Select(i => i.UserId).ToList(),
|
||||||
|
CompanyId = grp.FirstOrDefault().CompanyId,
|
||||||
|
ScheduleGenerationId = grp.FirstOrDefault().ScheduleGenerationId,
|
||||||
|
};
|
||||||
|
using (var uow = _unitOfWorkManager.Begin())
|
||||||
{
|
{
|
||||||
sendNotificationInfo.FcmToken = tokenList;
|
foreach (var fireStoreData in getAllFireStoreData)
|
||||||
|
{
|
||||||
|
await db.Collection("BMC_Notification").Document().SetAsync(fireStoreData);
|
||||||
|
}
|
||||||
|
await uow.CompleteAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
Notification notifications = new Notification
|
using (var uow = _unitOfWorkManager.Begin())
|
||||||
{
|
{
|
||||||
Title = title,
|
foreach (var entity in cloudMessageDetailsEntityList)
|
||||||
Body = body,
|
{
|
||||||
//ImageUrl = null
|
await _firebaseCloudMessageDetails.InsertAsync(entity);
|
||||||
};
|
}
|
||||||
sendNotificationInfo.notification = notifications;
|
await uow.CompleteAsync();
|
||||||
sendNotificationInfo.getResponseData = responseData;
|
}
|
||||||
await SendNotification(sendNotificationInfo);
|
await SendNotification(sendNotificationInfoList);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -156,12 +197,17 @@ namespace BCS.BMC.Web.Controllers
|
|||||||
}
|
}
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
[HttpPost]
|
|
||||||
public async Task<IActionResult> UpdateBmcMessageStatus([FromBody] BmcMessageStatusInput bmcMessageStatusInput)
|
|
||||||
{
|
|
||||||
var companyDetails = _companyMasterService.GetAllList().Where(x => x.Url.Trim() == bmcMessageStatusInput.CompanyUrl.Trim()).FirstOrDefault();
|
|
||||||
var baseUrl = companyDetails.Url + "api/services/bwac/updateBmcMessageStatus/UpdateMessageStatus";
|
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> UpdateBmcMessageStatus([FromBody] List<BmcMessageStatusInput> bmcMessageStatusInput)
|
||||||
|
{
|
||||||
|
string baseUrl = "";
|
||||||
|
foreach (var outputMessage in bmcMessageStatusInput)
|
||||||
|
{
|
||||||
|
var companyDetails = _companyMasterService.GetAllList().Where(x => x.Url.Trim() == outputMessage.CompanyUrl.Trim()).FirstOrDefault();
|
||||||
|
baseUrl = companyDetails.Url + "api/services/bwac/updateBmcMessageStatus/UpdateMessageStatus";
|
||||||
|
|
||||||
|
}
|
||||||
using (HttpClient client = new HttpClient())
|
using (HttpClient client = new HttpClient())
|
||||||
{
|
{
|
||||||
var requestJson = JsonConvert.SerializeObject(bmcMessageStatusInput);
|
var requestJson = JsonConvert.SerializeObject(bmcMessageStatusInput);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user