I am using GMC to send a message to an Android phone from C#.
link: https://android.googleapis.com/gcm/send
Method: POST
Header:
Authorization: key=AIzaSyCPNaU7Pn6csAKxXQHJXSprwWjO0JWaFsw
Body:
{
"registration_ids": [
"APA91bEzrLAiphAldxy6lSpSO503YaNm7CCng5uLzGRFWw93dwQIKfisdioyTY2Y0AyJrV81DHd8afBvmOi_KWgMcQKLFwCXJR8vLzuu2_nijBBu9kXRmcBDAGdwTMq9PQ5QPRWDcHh3PGFViTs4lC2DysiRLykASvV25ivY7Vwl_EvNZgcnqkmDM"
],
"data": {
"price": "Nguyễn Anh Quế. hehe"
}
}
I have tried it from web with success but in C# I don't know how to do it.
Like this:
string postData = "{ \"registration_ids\": [ \"APA91bEzrLAiphAldxy6lSpSO503YaNm7CCng5uLzGRFWw93dwQIKfisdioyTY2Y0AyJrV81DHd8afBvmOi_KWgMcQKLFwCXJR8vLzuu2_nijBBu9kXRmcBDAGdwTMq9PQ5QPRWDcHh3PGFViTs4lC2DysiRLykASvV25ivY7Vwl_EvNZgcnqkmDM\" ], \"data\": { \"price\": \"Nguyễn Anh Quế. hehe\" } }";
var request = (HttpWebClient)WebClient.Request("https://android.googleapis.com/gcm/send");
request.Method="POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
var response = (HttpWebRequest)request.GetResponse ();
// response contains the response
string postData = "{ \"registration_ids\": [ \"APA91bEzrLAiphAldxy6lSpSO503YaNm7CCng5uLzGRFWw93dwQIKfisdioyTY2Y0AyJrV81DHd8afBvmOi_KWgMcQKLFwCXJR8vLzuu2_nijBBu9kXRmcBDAGdwTMq9PQ5QPRWDcHh3PGFViTs4lC2DysiRLykASvV25ivY7Vwl_EvNZgcnqkmDM\" ], \"data\": { \"price\": \"Nguyễn Anh Quế. hehe\" } }";
var request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
request.Method = "POST";
request.Headers.Add("Authorization: key=AIzaSyCPNaU7Pn6csAKxXQHJXSprwWjO0JWaFsw");
request.ContentType = "application/json";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
var response = request.GetResponse();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.