Skip to content

Instantly share code, notes, and snippets.

@monir-dev
Created November 25, 2018 06:37
Show Gist options
  • Save monir-dev/26c1221b86be55b96d1fbdeead981791 to your computer and use it in GitHub Desktop.
Save monir-dev/26c1221b86be55b96d1fbdeead981791 to your computer and use it in GitHub Desktop.
public string SEND_PUSH_NOTIFICATION()
{
// one or more ids. Change "ANDROID_CLIENT_ID" with real ids
var REGISTRATIONS_IDS = new List<string>() { "ANDROID_CLIENT_ID" };
Dictionary<string, string> OptionalData = new Dictionary<string, string>()
{
{ "Category", "NEW_INDIVIDUAL_REQUEST"},
{ "Title", "Title"},
{ "SubTitle", "SubTitle"}
};
var data = new
{
registration_ids = REGISTRATIONS_IDS,
data = new { OptionalData }
};
// json encode data
var json = new JavaScriptSerializer().Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
var FCM_ServerID = "FCM_ServerID"; //"AAAAFcVaokE:APA91bGvdGCfclPKUhyLCesDygpOPPPQ5tv1j00hlR3rnqk6bv7D7fVkplTSrlUYqj"
var FCM_SENDER_ID = "FCM_SENDER_ID"; // "935053234525"
WebRequest webRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
webRequest.Method = "post";
webRequest.ContentType = "application/json";
webRequest.Headers.Add($"Authorization: key={FCM_ServerID}");
webRequest.Headers.Add($"Sender: id={FCM_SENDER_ID}");
webRequest.ContentLength = byteArray.Length;
Stream stream = webRequest.GetRequestStream();
stream.Write(byteArray, 0, byteArray.Length);
stream.Close();
WebResponse webResponse = webRequest.GetResponse();
stream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
var finalResponse = streamReader.ReadToEnd();
streamReader.Close();
stream.Close();
webResponse.Close();
return Content(finalResponse);
}
@monir-dev
Copy link
Author

This is a way to big code for a curl request. Use any c# curl library instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment