Skip to content

Instantly share code, notes, and snippets.

@code-atom
Created March 10, 2017 06:13
Show Gist options
  • Save code-atom/6c53e1373e9fa1995cbe20a3bd18f74c to your computer and use it in GitHub Desktop.
Save code-atom/6c53e1373e9fa1995cbe20a3bd18f74c to your computer and use it in GitHub Desktop.
FCM Notification based on API
public class Notifications
{
public string SendNotification(GcmNotiInputModel model)
{
try
{
string GoogleAppID = Config.BrowserKey;
var SENDER_ID = Config.SenderId;
WebRequest tRequest;
tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
// string postData = "{ 'registration_id': [ '" + regId + "' ], 'data': {'message': '" + txtMsg.Text + "'}}";
// title=Title Text
// message=This is a test message
// type=1
// sound=0
// vibrate=1
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + model.Message
+ "&data.title=" + model.Title
+ "&data.type=" + model.Type
+ "&data.sound=" + model.Sound
+ "&data.vibrate=" + model.Vibrate
+ "&data.time=" + System.DateTime.Now.ToString()+model.extra
+ "&registration_id=" + model.DeviceID + "";
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment