@ -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 List<string> UserId { get; set; } | |||
public long? ScheduleGenerationId { get; set; } | |||
public List<long> BmcIDs { 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 string ScheduleGenerationId { get; set; } | |||
} | |||
} |
@ -1,38 +1,36 @@ | |||
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 List<string> UserId { get; set; } | |||
public List<long> BmcIDs { get; set; } | |||
public string CompanyUrl { get; set; } | |||
public long? ScheduleGenerationId { get; set; } | |||
} | |||
//[AutoMapFrom(typeof(NotificationModel))] | |||
//public class FirebaseNotification : NotificationModel | |||
//{ | |||
// public List<string> Id { 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,176 @@ | |||
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; | |||
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; | |||
//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) | |||
{ | |||
_notificationService = notificationService; | |||
_firebaseCloudMessageDetails = firebaseCloudMessageDetails; | |||
_firebaseToken = firebaseToken; | |||
_companyMasterService = companyMasterService; | |||
} | |||
[HttpPost] | |||
public async Task<IActionResult> SendNotification([FromBody] FireBaseResponseModel notification) | |||
{ | |||
var result = await _notificationService.SendNotification(notification); | |||
if (result.SuccessCount > 0) | |||
{ | |||
if (notification.getResponseData != null) | |||
{ | |||
notification.getResponseData.IsSuccess = true; | |||
notification.getResponseData.Status = "Delivered"; | |||
} | |||
await UpdateBmcMessageStatus(notification.getResponseData); | |||
return Ok(result); | |||
} | |||
if (result.FailureCount > 0) | |||
{ | |||
if (notification.getResponseData != null) | |||
{ | |||
notification.getResponseData.IsSuccess = false; | |||
notification.getResponseData.Status = result.Responses[0].Exception.Message; | |||
} | |||
await UpdateBmcMessageStatus(notification.getResponseData); | |||
return BadRequest(result.Responses); | |||
} | |||
return Ok(result.Responses); | |||
} | |||
[HttpPost] | |||
public async Task<IActionResult> GetNotifications([FromBody] NotificationModel notification) | |||
{ | |||
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() == notification.CompanyUrl.Trim()).FirstOrDefault(); | |||
try | |||
{ | |||
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; | |||
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; | |||
responseData.ScheduleGenerationId = notification.ScheduleGenerationId; | |||
responseData.CompanyUrl = notification.CompanyUrl; | |||
responseData.BmcIDs = notification.BmcIDs; | |||
} | |||
int userId = 0; | |||
List<string> tokenList = new List<string>(); | |||
foreach (var item in notification.UserId) | |||
{ | |||
userId = Convert.ToInt32(item); | |||
var getTokenDetails = _firebaseToken.GetAllList().Where(x => x.UserId == userId).FirstOrDefault(); | |||
tokenList.Add(getTokenDetails.FcmToken.ToString()); | |||
var entity = new FirebaseCloudMessageDetails(); | |||
entity.UserId = 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); | |||
} | |||
} | |||
var title = notification.SenderName.ToJsonString(); | |||
var body = notification.Message.ToJsonString(); | |||
var sendNotificationInfo = new FireBaseResponseModel(); | |||
{ | |||
sendNotificationInfo.FcmToken = tokenList; | |||
} | |||
Notification notifications = new Notification | |||
{ | |||
Title = title, | |||
Body = body, | |||
//ImageUrl = null | |||
}; | |||
sendNotificationInfo.notification = notifications; | |||
sendNotificationInfo.getResponseData = responseData; | |||
await SendNotification(sendNotificationInfo); | |||
} | |||
catch (Exception ex) | |||
{ | |||
} | |||
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"; | |||
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(); | |||
} | |||
} | |||
} | |||
} |