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.
72 lines
2.9 KiB
72 lines
2.9 KiB
using Common;
|
|
using log4net;
|
|
using System;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Web.Script.Serialization;
|
|
|
|
namespace PushNotification
|
|
{
|
|
public static class FcmNotifier
|
|
{
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(FcmNotifier));
|
|
|
|
public static void Notify(string fcmToken, string body, string title, bool isGroup = false, object data = null)
|
|
{
|
|
if (string.IsNullOrEmpty(fcmToken))
|
|
{
|
|
Log.Debug("Fcm device id is empty: " + fcmToken);
|
|
return;
|
|
}
|
|
PushNotificationPayload payload = new PushNotificationPayload();
|
|
payload.to = isGroup == true ? "/topics/" + fcmToken : fcmToken;
|
|
payload.notification = new Notification { body = body, title = title };
|
|
payload.data = data;
|
|
|
|
try
|
|
{
|
|
Log.Debug("Server is firing push notification" + fcmToken);
|
|
|
|
WebRequest tRequest = WebRequest.Create(ConfigurationManager.AppSettings["GoogleFcm"].ToString());
|
|
tRequest.Method = "POST";
|
|
tRequest.ContentType = "application/json";
|
|
|
|
var serializer = new JavaScriptSerializer();
|
|
var json = serializer.Serialize(payload);
|
|
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
|
|
tRequest.Headers.Add(string.Format("Authorization: key={0}", ConfigurationManager.AppSettings["FcmAuthorizationHeader"].ToString()));
|
|
tRequest.Headers.Add(string.Format("Sender: id={0}", ConfigurationManager.AppSettings["fcmSenderId"].ToString()));
|
|
tRequest.ContentLength = byteArray.Length;
|
|
using (Stream dataStream = tRequest.GetRequestStream())
|
|
{
|
|
dataStream.Write(byteArray, 0, byteArray.Length);
|
|
using (WebResponse tResponse = tRequest.GetResponse())
|
|
{
|
|
using (Stream dataStreamResponse = tResponse.GetResponseStream())
|
|
{
|
|
using (StreamReader tReader = new StreamReader(dataStreamResponse))
|
|
{
|
|
String sResponseFromServer = tReader.ReadToEnd();
|
|
string str = sResponseFromServer;
|
|
|
|
// Log.Info($"Google FCM Api call success: {str}");
|
|
Log.Info(serializer.Serialize( new FireBaseLogResponseViewModel() { Response = str, Data = json }));
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex.Message, ex);
|
|
Log.ErrorFormat("Google FCM Api call failed Exception {0}:", ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|