|
|
@ -11,10 +11,12 @@ 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.Threading.Tasks; |
|
|
|
|
|
|
|
namespace BCS.BMC.Web.Controllers |
|
|
@ -23,60 +25,91 @@ namespace BCS.BMC.Web.Controllers |
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
public class NotificationController : BMCControllerBase |
|
|
|
{ |
|
|
|
|
|
|
|
private readonly IFirebaseNotificationAppService _notificationService; |
|
|
|
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, |
|
|
|
IRepository<FirebaseCloudMessageDetails> firebaseCloudMessageDetails) |
|
|
|
IRepository<FirebaseCloudMessageDetails> firebaseCloudMessageDetails, |
|
|
|
IRepository<FirebaseToken, int> firebaseToken) |
|
|
|
{ |
|
|
|
_notificationService = notificationService; |
|
|
|
_firebaseCloudMessageDetails = firebaseCloudMessageDetails; |
|
|
|
|
|
|
|
|
|
|
|
_firebaseCloudMessageDetails = firebaseCloudMessageDetails; |
|
|
|
_firebaseToken = firebaseToken; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[HttpPost] |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
[HttpPost] |
|
|
|
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 |
|
|
|
{ |
|
|
|
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(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |