using System;
namespace WebToSendIonicNotic
{
public partial class Default : System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
btnSend_Click(object sender,
EventArgs e)
{
string devicePushToken = "et7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsX";
string Message = txtMessage.Text;
//Start sending
push notification to apps
PushNotification fcmPush = new PushNotification();
PushNotification result =
fcmPush.SendNotification(devicePushToken,"Notification
title", Message, "news");
//End push
notification
}
}
}
Step 2: Create a class with the name PushNotification and paste the following code. You can get YOUR_FCM_SERVER_API_KEY and YOUR_FCM_SENDER_ID at console firebase, for more details click here
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace WebToSendIonicNotic
{
public class PushNotification
{
public PushNotification()
{
}
public bool
Successful
{
get;
set;
}
public string Response
{
get;
set;
}
public Exception Error
{
get;
set;
}
public PushNotification SendNotification(string YOUR_FCM_DEVICE_ID, string _title, string
_message, string _topic)
{
string YOUR_FCM_SERVER_API_KEY = "TWI7pb0DYxxxxxxxxxxxxxxxxe9S1Oq1KFzd4NkNZDQH";
string YOUR_FCM_SENDER_ID = "xxxxxxxxxxxx";
PushNotification result = new PushNotification();
try
{
result.Successful = true;
result.Error = null;
var requestUri = "https://fcm.googleapis.com/fcm/send";
WebRequest webRequest =
WebRequest.Create(requestUri);
webRequest.Method = "POST";
webRequest.Headers.Add(string.Format("Authorization: key={0}",
YOUR_FCM_SERVER_API_KEY));
webRequest.Headers.Add(string.Format("Sender: id={0}",
YOUR_FCM_SENDER_ID));
webRequest.ContentType = "application/json";
var data = new
{
to = YOUR_FCM_DEVICE_ID, //If you want to test for single device.
//to = "/topics/" + _topic, // this is for topic
notification = new
{
title = _title,
body = _message,
//icon="myicon"
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray =
Encoding.UTF8.GetBytes(json);
webRequest.ContentLength =
byteArray.Length;
using (Stream dataStream = webRequest.GetRequestStream())
{
dataStream.Write(byteArray,
0, byteArray.Length);
using (WebResponse webResponse =
webRequest.GetResponse())
{
using (Stream dataStreamResponse =
webResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String
sResponseFromServer = tReader.ReadToEnd();
result.Response
= sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
result.Successful = false;
result.Response = null;
result.Error = ex;
}
return result;
}
}
}
Thank you. timban2.com
ReplyDeleteAlways welcome.
ReplyDeleteSo lot to occur over your amazing blog. Your blog procures me a fantastic transaction of enjoyable.. Salubrious lot beside the scene. visit this page
ReplyDeleteAt this point you'll find out what is important, it all gives a url to the appealing page: check this site
ReplyDeleteThat's amazing informative post, I want to add one more thing, If you want to make your web visitor to your subscriber then you should definitely check gravitec lifetime deal Best push notification for website ever.
ReplyDelete