Advertisement
minafaw3

Push Notification with Asp.Net

Feb 26th, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. void send(string regId)
  2. {
  3. var applicationID = "xxxxxxxx";
  4.  
  5.  
  6. var SENDER_ID = "xxxxx";
  7. var value = txtMsg.Text;
  8. WebRequest tRequest;
  9. tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
  10. tRequest.Method = "post";
  11. tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
  12. tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
  13.  
  14. tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
  15.  
  16. // string postData = "{ 'registration_id': [ '" + regId + "' ], 'data': {'message': '" + txtMsg.Text + "'}}";
  17. string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regId + "";
  18. Console.WriteLine(postData);
  19. Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  20. tRequest.ContentLength = byteArray.Length;
  21.  
  22. Stream dataStream = tRequest.GetRequestStream();
  23. dataStream.Write(byteArray, 0, byteArray.Length);
  24. dataStream.Close();
  25.  
  26. WebResponse tResponse = tRequest.GetResponse();
  27.  
  28. dataStream = tResponse.GetResponseStream();
  29.  
  30. StreamReader tReader = new StreamReader(dataStream);
  31.  
  32. String sResponseFromServer = tReader.ReadToEnd();
  33.  
  34. lblStat.Text = sResponseFromServer;
  35. tReader.Close();
  36. dataStream.Close();
  37. tResponse.Close();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement