@ -0,0 +1,14 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BCS.BMC.CompanyMasters.Dto | |||
{ | |||
public class ValidUrlResponseDto | |||
{ | |||
public bool IsSuccess { get; set; } | |||
public string TenantName { get; set; } | |||
} | |||
} |
@ -0,0 +1,18 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BCS.BMC.FirebaseCloudMessaging.Dto | |||
{ | |||
public class BmcMessageStatusInput | |||
{ | |||
public string UserId { get; set; } | |||
public long? ScheduleGenerationId { get; set; } | |||
public long? BmcID { get; set; } | |||
public string Status { get; set; } | |||
public string CompanyUrl { get; set; } | |||
public bool IsSuccess { get; set; } | |||
} | |||
} |
@ -0,0 +1,35 @@ | |||
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 List<string> UserId { get; set; } | |||
[FirestoreProperty] | |||
public int CompanyId { get; set; } | |||
[FirestoreProperty] | |||
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; } | |||
} | |||
} | |||
@ -1,38 +1,30 @@ | |||
using Newtonsoft.Json; | |||
using Abp.AutoMapper; | |||
using BCS.BMC.BMC.FirebaseCloudMessages; | |||
using Newtonsoft.Json; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel.DataAnnotations; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BCS.BMC.FirebaseCloudMessaging.Dto | |||
{ | |||
[AutoMapTo(typeof (FirebaseCloudMessageDetails))] | |||
public class NotificationModel | |||
{ | |||
[JsonProperty("deviceId")] | |||
public string DeviceId { get; set; } | |||
[JsonProperty("isAndroidDevice")] | |||
public bool IsAndroidDevice { get; set; } | |||
[JsonProperty("title")] | |||
public string Title { get; set; } | |||
[JsonProperty("body")] | |||
public string Body { get; set; } | |||
} | |||
public string FcmToken { get; set; } | |||
public class GoogleNotification | |||
{ | |||
public class DataPayload | |||
{ | |||
[JsonProperty("title")] | |||
public string Title { get; set; } | |||
[JsonProperty("body")] | |||
public string Body { get; set; } | |||
} | |||
[JsonProperty("priority")] | |||
public string Priority { get; set; } = "high"; | |||
[JsonProperty("data")] | |||
public DataPayload Data { get; set; } | |||
[JsonProperty("notification")] | |||
public DataPayload Notification { 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 long? BmcID { get; set; } | |||
public string CompanyUrl { get; set; } | |||
public long? ScheduleGenerationId { get; set; } | |||
} | |||
} |
@ -0,0 +1,30 @@ | |||
using Abp.Domain.Entities.Auditing; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel.DataAnnotations; | |||
using System.ComponentModel.DataAnnotations.Schema; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BCS.BMC.BMC.FirebaseCloudMessages | |||
{ | |||
[Table("FirebaseCloudMessageDetails")] | |||
public class FirebaseCloudMessageDetails : FullAuditedEntity | |||
{ | |||
public const int SenderNameMaxLength = 32; | |||
public const int SenderImageurlMaxLength = 256; | |||
public const int FcmTokenMaxLength = 512; | |||
public int Id { get; set; } | |||
public int UserId { get; set; } | |||
[MaxLength(FcmTokenMaxLength)] | |||
public string FcmToken { get; set; } | |||
[MaxLength(SenderImageurlMaxLength)] | |||
public string SenderImageurl { get; set; } | |||
[MaxLength(SenderNameMaxLength)] | |||
public string SenderName { get; set; } | |||
public DateTime? MessageSentDateTime { get; set; } | |||
public bool Status { get; set; } | |||
public string Message { get; set; } | |||
} | |||
} |
@ -0,0 +1,21 @@ | |||
using Abp.Domain.Entities.Auditing; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel.DataAnnotations; | |||
using System.ComponentModel.DataAnnotations.Schema; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BCS.BMC.BMC.FirebaseCloudMessages | |||
{ | |||
[Table("FirebaseToken")] | |||
public class FirebaseToken : FullAuditedEntity | |||
{ | |||
public const int HostNameMaxLength = 512; | |||
public int? UserId { get; set; } | |||
[MaxLength(HostNameMaxLength)] | |||
public string HostName { get; set; } | |||
public string FcmToken { get; set; } | |||
} | |||
} |
@ -0,0 +1,45 @@ | |||
using System; | |||
using Microsoft.EntityFrameworkCore.Migrations; | |||
#nullable disable | |||
namespace BCS.BMC.Migrations | |||
{ | |||
public partial class FirebaeCloudMessageDetails : Migration | |||
{ | |||
protected override void Up(MigrationBuilder migrationBuilder) | |||
{ | |||
migrationBuilder.CreateTable( | |||
name: "FirebaseCloudMessageDetails", | |||
columns: table => new | |||
{ | |||
Id = table.Column<int>(type: "int", nullable: false) | |||
.Annotation("SqlServer:Identity", "1, 1"), | |||
UserId = table.Column<int>(type: "int", nullable: false), | |||
FcmToken = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true), | |||
SenderImageurl = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true), | |||
SenderName = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: true), | |||
MessageSentDateTime = table.Column<DateTime>(type: "datetime2", nullable: true), | |||
Status = table.Column<bool>(type: "bit", nullable: false), | |||
Message = table.Column<string>(type: "nvarchar(max)", nullable: true), | |||
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false), | |||
CreatorUserId = table.Column<long>(type: "bigint", nullable: true), | |||
LastModificationTime = table.Column<DateTime>(type: "datetime2", nullable: true), | |||
LastModifierUserId = table.Column<long>(type: "bigint", nullable: true), | |||
IsDeleted = table.Column<bool>(type: "bit", nullable: false), | |||
DeleterUserId = table.Column<long>(type: "bigint", nullable: true), | |||
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true) | |||
}, | |||
constraints: table => | |||
{ | |||
table.PrimaryKey("PK_FirebaseCloudMessageDetails", x => x.Id); | |||
}); | |||
} | |||
protected override void Down(MigrationBuilder migrationBuilder) | |||
{ | |||
migrationBuilder.DropTable( | |||
name: "FirebaseCloudMessageDetails"); | |||
} | |||
} | |||
} |
@ -0,0 +1,41 @@ | |||
using System; | |||
using Microsoft.EntityFrameworkCore.Migrations; | |||
#nullable disable | |||
namespace BCS.BMC.Migrations | |||
{ | |||
public partial class FirebaseToken : Migration | |||
{ | |||
protected override void Up(MigrationBuilder migrationBuilder) | |||
{ | |||
migrationBuilder.CreateTable( | |||
name: "FirebaseToken", | |||
columns: table => new | |||
{ | |||
Id = table.Column<int>(type: "int", nullable: false) | |||
.Annotation("SqlServer:Identity", "1, 1"), | |||
UserId = table.Column<int>(type: "int", nullable: true), | |||
HostName = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true), | |||
FcmToken = table.Column<string>(type: "nvarchar(max)", nullable: true), | |||
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false), | |||
CreatorUserId = table.Column<long>(type: "bigint", nullable: true), | |||
LastModificationTime = table.Column<DateTime>(type: "datetime2", nullable: true), | |||
LastModifierUserId = table.Column<long>(type: "bigint", nullable: true), | |||
IsDeleted = table.Column<bool>(type: "bit", nullable: false), | |||
DeleterUserId = table.Column<long>(type: "bigint", nullable: true), | |||
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true) | |||
}, | |||
constraints: table => | |||
{ | |||
table.PrimaryKey("PK_FirebaseToken", x => x.Id); | |||
}); | |||
} | |||
protected override void Down(MigrationBuilder migrationBuilder) | |||
{ | |||
migrationBuilder.DropTable( | |||
name: "FirebaseToken"); | |||
} | |||
} | |||
} |
@ -0,0 +1,19 @@ | |||
using Microsoft.EntityFrameworkCore.Migrations; | |||
#nullable disable | |||
namespace BCS.BMC.Migrations | |||
{ | |||
public partial class RemoveIdFrom_FirebaseToken : Migration | |||
{ | |||
protected override void Up(MigrationBuilder migrationBuilder) | |||
{ | |||
} | |||
protected override void Down(MigrationBuilder migrationBuilder) | |||
{ | |||
} | |||
} | |||
} |
@ -0,0 +1,21 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel.DataAnnotations; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using BCS.BMC.BMC.CompanyMasters; | |||
using Abp.AutoMapper; | |||
using BCS.BMC.BMC.FirebaseCloudMessages; | |||
namespace BCS.BMC.Models.TokenAuth | |||
{ | |||
[AutoMap(typeof(FirebaseToken))] | |||
public class CreateOrUpdateFireBaseModel | |||
{ | |||
public int? UserId { get; set; } | |||
public string HostName { get; set; } | |||
public string FcmToken { get; set; } | |||
} | |||
} |
@ -0,0 +1,14 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BCS.BMC.Models.TokenAuth | |||
{ | |||
public class FcmTokenDeleteInput | |||
{ | |||
public int UserId { get; set; } | |||
public string HostName { get; set; } | |||
} | |||
} |
@ -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; } | |||
} | |||
} | |||
@ -1,29 +0,0 @@ | |||
using BCS.BMC.Controllers; | |||
using BCS.BMC.FirebaseCloudMessaging; | |||
using FirebaseAdmin.Messaging; | |||
using Microsoft.AspNetCore.Mvc; | |||
using System.Collections.Generic; | |||
using System.Threading.Tasks; | |||
namespace BCS.BMC.Web.Controllers | |||
{ | |||
//[Route("api/notification")] | |||
//[ApiController] | |||
public class FirebaseNotificationController : BMCControllerBase | |||
{ | |||
private readonly FirebaseNotificationAppService _notificationService; | |||
public FirebaseNotificationController(FirebaseNotificationAppService notificationService) | |||
{ | |||
_notificationService = notificationService; | |||
} | |||
[Route("send")] | |||
[HttpPost] | |||
public async Task<IActionResult> Notification(string fcmToken, [FromBody] Notification notification) | |||
{ | |||
var result = await FirebaseNotificationAppService.SendNotification(fcmToken, notification); | |||
return Ok(result); | |||
} | |||
} | |||
} |
@ -0,0 +1,226 @@ | |||
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]; | |||
} | |||
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 = companyDetails.Url + "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(); | |||
} | |||
} | |||
} | |||
} |