Basit Khan

Basit Khan

  • 1.7k
  • 295
  • 81.7k

How to write a android code where I can get registration ID

May 5 2016 2:40 AM
How to write a android code where I can get registration ID for GCM with the help of project/sender ID
 
Before i was not understood how its GCM Notifications work. I google it. and i found the following step and then i understood.
steps are give below.
1-First Create a developer account.
2-From that developer account we can get 1) Sender ID/Project ID 2) API Key.
3- To send message on each client we need to have their Device Registration ID
' This one i don't know how to get, for that i used GCM Server Helper in Play store. on that apps i put project ID then
this apps give me registration ID
then i create small ASP.Net apps code behind file Vb.net
and i pass four thing. 1) Sender/Project ID 2) API Key 3) Registration ID 4) Message.
then now i understood how popup notification works. When data inserted into sql table then we need to tell GCM services to send message to registered client.
Now what my problem are:
1) How to do the programe in android where i can pass the send ID/Project ID and API parameter to GCM service and get the Registration ID
2) Then i want to save this registration ID in sql becoz i will tell GCM to send message to particular client.
Below is perfectly working code in asp.net.
Private Sub Frm_SendNotification()
RegId = ""
ApplicationID = ""
SENDER_ID = ""
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) + ""
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
 

Answers (2)