You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
13 KiB
316 lines
13 KiB
using Common.Models.Confiq;
|
|
using Common.Models.Enums;
|
|
using Common.Models.Notification;
|
|
using Common.Models.RequestResponse;
|
|
using Common.Utility;
|
|
using log4net;
|
|
using MoreLinq;
|
|
using Newtonsoft.Json;
|
|
using Repository.DAO.ThirdParty;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Dynamic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Script.Serialization;
|
|
|
|
namespace Business.BusinessLogic.NotificationApiService
|
|
{
|
|
public class FireBaseService
|
|
{
|
|
private readonly ILog _log = LogManager.GetLogger(typeof(FireBaseService));
|
|
|
|
private readonly IThirdPartyRepo _thirdPartyRepo;
|
|
|
|
public FireBaseService()
|
|
{
|
|
_thirdPartyRepo = new ThirdPartyRepo();
|
|
}
|
|
|
|
public TPResponse PushNotify(SendNotificationRequest model)
|
|
{
|
|
|
|
TPResponse _tPResponse = new TPResponse();
|
|
|
|
//_log.Info($"IsBulkNotification {model.IsBulkNotification}");
|
|
|
|
List<TPResponse> tPResponses = new List<TPResponse>();
|
|
|
|
Task.Run(() =>
|
|
{
|
|
Notify(model);
|
|
|
|
}).ContinueWith(y =>
|
|
{
|
|
_log.Debug($"PushNotify Responses {JsonConvert.SerializeObject(tPResponses)}");
|
|
|
|
//tPResponses.Where(x => x.ResponseCode.Equals("0")).ToList().ForEach(z =>
|
|
//{
|
|
// _thirdPartyRepo.UpdateNotificationStatus(z.Id);
|
|
//});
|
|
|
|
});
|
|
|
|
_tPResponse.ResponseCode = "0";
|
|
_tPResponse.Msg = "PushNotify Sent Sucessfully.";
|
|
|
|
|
|
return _tPResponse;
|
|
}
|
|
|
|
private void Notify(SendNotificationRequest model)
|
|
{
|
|
try
|
|
{
|
|
if (!model.Recipients.Any())
|
|
{
|
|
_log.Info("Recipient(s) are empty.");
|
|
return;
|
|
}
|
|
|
|
var applicationId = ConfigurationManager.AppSettings["FcmAuthorizationHeader"];
|
|
var senderId = ConfigurationManager.AppSettings["fcmSenderId"];
|
|
var langauge = string.IsNullOrEmpty(model.Language) ? ConfigurationManager.AppSettings["lang"] : model.Language;
|
|
var serializer = new JavaScriptSerializer();
|
|
|
|
|
|
if (model.IsBulkNotification)
|
|
{
|
|
foreach (var batch in model.Recipients.GroupBy(c => c.DeviceType).ToList())
|
|
{
|
|
var templatejson = Helpers.GetMapping(Common.Models.Enums.MappingType.PUSH_NOTIFY_TEMPLATE, model.Template.ToString(), langauge);
|
|
var deviceType = batch.Key;
|
|
var title = batch.First().NotificationContent.Title ?? GetStatic.ReadWebConfig("PushNotifyAlertTitle", "");
|
|
var body = batch.First().NotificationContent.Body;
|
|
var image = batch.First().NotificationContent.Image;
|
|
var navigateURL = batch.First().NotificationContent.NavigateURL;
|
|
var clickActivity = batch.First().NotificationContent.ClickActivity;
|
|
var messageType = batch.First().NotificationContent.MessageType;
|
|
if (model.Template != NotifyTemplate.NONE)
|
|
{
|
|
if (!string.IsNullOrEmpty(body))
|
|
{
|
|
List<Mapping> mappings = JsonConvert.DeserializeObject<List<Mapping>>(body);
|
|
|
|
body = templatejson.SText;
|
|
mappings.ForEach(x =>
|
|
{
|
|
body = body.Replace(string.Format("{{{0}}}", x.SValue), x.SText);
|
|
});
|
|
}
|
|
|
|
title = templatejson.DText;
|
|
}
|
|
|
|
foreach (var batchDevice in batch.Batch(100))
|
|
{
|
|
var recipientsList = batchDevice.Select(c => c.Address).Distinct().ToList();
|
|
|
|
string json;
|
|
|
|
if (deviceType == DeviceType.Android.ToString())
|
|
{
|
|
var data = new
|
|
{
|
|
registration_ids = recipientsList.ToArray(),
|
|
notification = new
|
|
{
|
|
body,
|
|
title,
|
|
sound = "Enabled",
|
|
click_action = clickActivity ?? string.Empty,
|
|
message_type = messageType ?? string.Empty
|
|
},
|
|
data = new
|
|
{
|
|
image = image ?? string.Empty,
|
|
url = navigateURL ?? string.Empty
|
|
}
|
|
};
|
|
|
|
json = serializer.Serialize(data);
|
|
}
|
|
else
|
|
{
|
|
var data = new
|
|
{
|
|
registration_ids = recipientsList.ToArray(),
|
|
content_available = true,
|
|
mutable_content = true,
|
|
notification = new
|
|
{
|
|
body,
|
|
title,
|
|
sound = "Enabled",
|
|
click_action = clickActivity ?? string.Empty,
|
|
message_type = messageType ?? string.Empty
|
|
},
|
|
data = new
|
|
{
|
|
mediaUrl = string.IsNullOrEmpty(image) ? string.Empty : ConfigurationManager.AppSettings["ApplicationMainUrl"] + "api/" + image,
|
|
url = navigateURL ?? string.Empty
|
|
}
|
|
};
|
|
|
|
json = serializer.Serialize(data);
|
|
}
|
|
|
|
Invoke(applicationId, senderId, json);
|
|
}
|
|
}
|
|
|
|
// _thirdPartyRepo.UpdateNotificationStatus(model.ToString());
|
|
}
|
|
else
|
|
{
|
|
string body;
|
|
string title;
|
|
|
|
var templatejson = Helpers.GetMapping(Common.Models.Enums.MappingType.PUSH_NOTIFY_TEMPLATE, model.Template.ToString(), langauge);
|
|
|
|
foreach (var batch in model.Recipients)
|
|
{
|
|
|
|
if (model.Template != NotifyTemplate.NONE)
|
|
{
|
|
body = templatejson.SText;
|
|
|
|
if (!string.IsNullOrEmpty(batch.NotificationContent.Body))
|
|
{
|
|
List<Mapping> mappings = JsonConvert.DeserializeObject<List<Mapping>>(batch.NotificationContent.Body);
|
|
|
|
mappings.ForEach(x =>
|
|
{
|
|
body = body.Replace(string.Format("{{{0}}}", x.SValue), x.SText);
|
|
});
|
|
}
|
|
|
|
title = templatejson.DText;
|
|
}
|
|
else
|
|
{
|
|
body = batch.NotificationContent.Body;
|
|
title = batch.NotificationContent.Title ?? GetStatic.ReadWebConfig("PushNotifyAlertTitle", "JME");
|
|
}
|
|
|
|
string json;
|
|
if (batch.DeviceType == DeviceType.Android.ToString())
|
|
{
|
|
|
|
var data = new
|
|
{
|
|
to = batch.Address,
|
|
notification = new
|
|
{
|
|
body = body,
|
|
title = batch.NotificationContent.Title ?? GetStatic.ReadWebConfig("PushNotifyAlertTitle", "JME"),
|
|
sound = "Enabled",
|
|
click_action = batch.NotificationContent.ClickActivity ?? string.Empty,
|
|
message_type = batch.NotificationContent.MessageType ?? string.Empty
|
|
},
|
|
data = new
|
|
{
|
|
image = batch.NotificationContent.Image ?? string.Empty,
|
|
url = batch.NotificationContent.NavigateURL ?? string.Empty
|
|
}
|
|
};
|
|
|
|
json = serializer.Serialize(data);
|
|
}
|
|
else
|
|
{
|
|
var data = new
|
|
{
|
|
to = batch.Address,
|
|
content_available = true,
|
|
mutable_content = true,
|
|
notification = new
|
|
{
|
|
body = body,
|
|
title = batch.NotificationContent.Title ?? GetStatic.ReadWebConfig("PushNotifyAlertTitle", "JME"),
|
|
sound = "Enabled",
|
|
click_action = batch.NotificationContent.ClickActivity ?? string.Empty,
|
|
message_type = batch.NotificationContent.MessageType ?? string.Empty
|
|
},
|
|
data = new
|
|
{
|
|
url = batch.NotificationContent.NavigateURL ?? string.Empty,
|
|
mediaUrl = string.IsNullOrEmpty(batch.NotificationContent.Image) ? string.Empty : ConfigurationManager.AppSettings["ApplicationMainUrl"] + "api/" + batch.NotificationContent.Image
|
|
}
|
|
};
|
|
|
|
json = serializer.Serialize(data);
|
|
}
|
|
|
|
Invoke(applicationId, senderId, json);
|
|
}
|
|
}
|
|
|
|
model.Recipients.ForEach(x =>
|
|
{
|
|
_thirdPartyRepo.UpdateNotificationStatus(x.BroadCastNoticeId.ToString());
|
|
});
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error("FireBaseService Exception", ex);
|
|
}
|
|
|
|
}
|
|
|
|
private void Invoke(string applicationId, string senderId, string json)
|
|
{
|
|
var tRequest = WebRequest.Create(ConfigurationManager.AppSettings["GoogleFcm"].ToString());
|
|
tRequest.Method = "post";
|
|
tRequest.ContentType = "application/json";
|
|
|
|
var byteArray = Encoding.UTF8.GetBytes(json);
|
|
tRequest.Headers.Add($"Authorization: key={applicationId}");
|
|
tRequest.Headers.Add($"Sender: id={senderId}");
|
|
tRequest.ContentLength = byteArray.Length;
|
|
var serializer = new JavaScriptSerializer();
|
|
|
|
try
|
|
{
|
|
using (var dataStream = tRequest.GetRequestStream())
|
|
{
|
|
dataStream.Write(byteArray, 0, byteArray.Length);
|
|
using (var tResponse = tRequest.GetResponse())
|
|
{
|
|
using (var dataStreamResponse = tResponse.GetResponseStream())
|
|
{
|
|
using (var tReader = new StreamReader(dataStreamResponse))
|
|
{
|
|
var sResponseFromServer = tReader.ReadToEnd();
|
|
var str = sResponseFromServer;
|
|
try
|
|
{
|
|
//dynamic reply = new System.Dynamic.ExpandoObject();
|
|
//reply.Response = str;
|
|
//reply.Data = str;
|
|
|
|
var js = new JavaScriptSerializer();
|
|
_log.Info($"REQUEST {json} | RESPONSE {str}");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error("Google FCM Api Invoke failed Exception", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|