public bool SendPushNotification(string deviceId, string message)
{
//applicationID = "AIzaSyxxxxxxxxxxxxxxxxxxx"
// senderId = "98xxxxxxxxx"
bool isSent = false;
string str = "";
try
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = deviceId,
notification = new
{
body = message,
title = "SpAlert",
sound = "Enabled"
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", utilityHelper.PushNotificationAppId));
tRequest.Headers.Add(string.Format("Sender: id={0}", utilityHelper.PushNotificationSenderID));
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();
str = sResponseFromServer;
Console.WriteLine(sResponseFromServer);
isSent = true;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
str = ex.Message;
}
return isSent;
}
{
//applicationID = "AIzaSyxxxxxxxxxxxxxxxxxxx"
// senderId = "98xxxxxxxxx"
bool isSent = false;
string str = "";
try
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = deviceId,
notification = new
{
body = message,
title = "SpAlert",
sound = "Enabled"
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", utilityHelper.PushNotificationAppId));
tRequest.Headers.Add(string.Format("Sender: id={0}", utilityHelper.PushNotificationSenderID));
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();
str = sResponseFromServer;
Console.WriteLine(sResponseFromServer);
isSent = true;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
str = ex.Message;
}
return isSent;
}
Comments
Post a Comment