Basit Khan

Basit Khan

  • NA
  • 336
  • 122.7k

How to send GCM notification in javascript.

Dec 23 2016 11:22 AM
Hi,
 
How to send GCM notification in javascript.
 
I did in vb.net and it successfully sent with below code.
 
i want to convert below code or do it in javascript.
 
Private Sub Frm_SendNotification()
RegId = "xxxxx"
ApplicationID = "xxxxx"
SENDER_ID = "xxxx"
Dim value = TextBox1.Text
'message text box
Dim tRequest As WebRequest
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
tRequest.Method = "post"
tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"
tRequest.Headers.Add(String.Format("Authorization: key={0}", ApplicationID))
tRequest.Headers.Add(String.Format("Sender: id={0}", SENDER_ID))
'Data post to the Server
'Dim postData As String = (Convert.ToString("collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=") & RegId) + ""
Dim postData As String = (Convert.ToString("data.message=" + value + "&registration_id=") & RegId) + ""
Console.WriteLine(postData)
Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(postData)
tRequest.ContentLength = byteArray.Length
Dim dataStream As Stream = tRequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim tResponse As WebResponse = tRequest.GetResponse()
dataStream = tResponse.GetResponseStream()
Dim tReader As New StreamReader(dataStream)
Dim sResponseFromServer As [String] = tReader.ReadToEnd()
'Get response from GCM server
Label1.Text = sResponseFromServer
'Assigning GCM response to Label text
tReader.Close()
dataStream.Close()
tResponse.Close()
End Sub
 
 
Thanks
Basit. 

Answers (1)