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; private readonly IRepository _firebaseToken; private readonly IRepository _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, IRepository firebaseToken, IRepository companyMasterService) { _notificationService = notificationService; _firebaseCloudMessageDetails = firebaseCloudMessageDetails; _firebaseToken = firebaseToken; _companyMasterService = companyMasterService; } [HttpPost] public async Task 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 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 tokenList = new List(); 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 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(); } } } }