226 lines
11 KiB
C#
226 lines
11 KiB
C#
using Abp.AutoMapper;
|
|
using Abp.Domain.Repositories;
|
|
using Abp.Json;
|
|
using BCS.BMC.BMC.CompanyMasters;
|
|
using BCS.BMC.BMC.FirebaseCloudMessages;
|
|
using BCS.BMC.Controllers;
|
|
using BCS.BMC.FirebaseCloudMessaging;
|
|
using BCS.BMC.FirebaseCloudMessaging.Dto;
|
|
using BCS.BMC.Models.TokenAuth;
|
|
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.Net.Http.Headers;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using Abp.EntityFrameworkCore.Repositories;
|
|
using System.Security.Cryptography;
|
|
using Abp.Domain.Uow;
|
|
using Google.Type;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace BCS.BMC.Web.Controllers
|
|
{
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
public class NotificationController : BMCControllerBase
|
|
{
|
|
|
|
private readonly IFirebaseNotificationAppService _notificationService;
|
|
private readonly IRepository<FirebaseCloudMessageDetails> _firebaseCloudMessageDetails;
|
|
private readonly IRepository<FirebaseToken, int> _firebaseToken;
|
|
private readonly IRepository<CompanyMaster, int> _companyMasterService;
|
|
private readonly IUnitOfWorkManager _unitOfWorkManager;
|
|
//IFirebaseConfig config = new FirebaseConfig
|
|
//{
|
|
// AuthSecret = "n6DTPQEbpnLahrrk2EYu4bJ2Wd3jCk2N8kocazdI",
|
|
// BasePath = "https://firechat-57601-default-rtdb.europe-west1.firebasedatabase.app/"
|
|
//};
|
|
//IFirebaseClient client;
|
|
public NotificationController(IFirebaseNotificationAppService notificationService,
|
|
IRepository<FirebaseCloudMessageDetails> firebaseCloudMessageDetails,
|
|
IRepository<FirebaseToken, int> firebaseToken,
|
|
IRepository<CompanyMaster, int> companyMasterService,
|
|
IUnitOfWorkManager unitOfWorkManager)
|
|
{
|
|
_notificationService = notificationService;
|
|
_firebaseCloudMessageDetails = firebaseCloudMessageDetails;
|
|
_firebaseToken = firebaseToken;
|
|
_companyMasterService = companyMasterService;
|
|
_unitOfWorkManager = unitOfWorkManager;
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public async Task<SendResponse> SendNotification([FromBody] FireBaseResponseModelForBmc notification)
|
|
{
|
|
var result = await _notificationService.SendNotification(notification);
|
|
return result.Responses[0];
|
|
}
|
|
[HttpPost]
|
|
public async Task<IActionResult> SendNotificationForBwac([FromBody] List<FireBaseResponseModel> notifications)
|
|
{
|
|
List<BmcMessageStatusInput> bmcMessageStatusInputs = new List<BmcMessageStatusInput>();
|
|
foreach (var notification in notifications)
|
|
{
|
|
var result = await _notificationService.SendNotificationBwac(notification);
|
|
|
|
if (result.SuccessCount > 0)
|
|
{
|
|
if (notification.getResponseData != null)
|
|
{
|
|
notification.getResponseData.IsSuccess = true;
|
|
notification.getResponseData.Status = "Delivered";
|
|
}
|
|
}
|
|
if (result.FailureCount > 0)
|
|
{
|
|
if (notification.getResponseData != null)
|
|
{
|
|
notification.getResponseData.IsSuccess = false;
|
|
notification.getResponseData.Status = result.Responses[0].Exception.Message;
|
|
}
|
|
//await UpdateBmcMessageStatus(notification.getResponseData);
|
|
}
|
|
bmcMessageStatusInputs.Add(notification.getResponseData);
|
|
}
|
|
|
|
await UpdateBmcMessageStatus(bmcMessageStatusInputs);
|
|
return Ok();
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> GetNotifications([FromBody] List<NotificationModel> notifications)
|
|
{
|
|
string path = AppDomain.CurrentDomain.BaseDirectory + @"firechat-57601-firebase-adminsdk-anscp-e04366c4d4.json";
|
|
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
|
|
var companyDetails = _companyMasterService.GetAllList().Where(x => x.Url.Trim() == notifications.FirstOrDefault().CompanyUrl.Trim()).FirstOrDefault();
|
|
var getTokenDetails = _firebaseToken.GetAllList();
|
|
|
|
try
|
|
{
|
|
List<FireStoreNotificationModelDto> notificationModelsList = new List<FireStoreNotificationModelDto>();
|
|
FirestoreDb db = FirestoreDb.Create("firechat-57601");
|
|
var sendNotificationInfoList = new List<FireBaseResponseModel>();
|
|
var cloudMessageDetailsEntityList = new List<FirebaseCloudMessageDetails>();
|
|
foreach (var notification in notifications)
|
|
{
|
|
FireStoreNotificationModelDto data = new FireStoreNotificationModelDto();
|
|
var sendNotificationInfo = new FireBaseResponseModel();
|
|
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;
|
|
data.CompanyId = companyDetails.Id;
|
|
data.ScheduleGenerationId = Int64.Parse(notification.ScheduleGenerationId.ToString());
|
|
//await db.Collection("BMC_Notification").Document().SetAsync(data);
|
|
notificationModelsList.Add(data);
|
|
|
|
var entity = new FirebaseCloudMessageDetails();
|
|
entity.UserId = Int32.Parse(notification.UserId);
|
|
entity.SenderImageurl = notification.SenderImageurl;
|
|
entity.SenderName = notification.SenderName;
|
|
entity.MessageSentDateTime = notification.MessageSentDateTime;
|
|
entity.Status = notification.Status;
|
|
entity.Message = notification.Message;
|
|
cloudMessageDetailsEntityList.Add(entity);
|
|
|
|
|
|
Notification noti = new Notification
|
|
{
|
|
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 getAllFireStoreData = from table in notificationModelsList
|
|
group table by table.Message
|
|
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())
|
|
{
|
|
foreach (var fireStoreData in getAllFireStoreData)
|
|
{
|
|
await db.Collection("BMC_Notification").Document().SetAsync(fireStoreData);
|
|
}
|
|
await uow.CompleteAsync();
|
|
}
|
|
|
|
using (var uow = _unitOfWorkManager.Begin())
|
|
{
|
|
foreach (var entity in cloudMessageDetailsEntityList)
|
|
{
|
|
await _firebaseCloudMessageDetails.InsertAsync(entity);
|
|
}
|
|
await uow.CompleteAsync();
|
|
}
|
|
await SendNotificationForBwac(sendNotificationInfoList);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
return Ok();
|
|
}
|
|
|
|
[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 = outputMessage.CompanyUrl + "api/services/bwac/updateBmcMessageStatus/UpdateMessageStatus";
|
|
|
|
}
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
var requestJson = JsonConvert.SerializeObject(bmcMessageStatusInput);
|
|
var requestContent = new StringContent(requestJson.ToString());
|
|
requestContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
|
|
HttpResponseMessage response = await client.PostAsync(baseUrl, requestContent);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|
|
} |