Android Question GCM Push Notification - Device Registration problems

henrywood

Active Member
Licensed User
Hi !

I am writing an app that needs to register the DEVICE ID on my servers, so that messages can be sent using this ID as recipient.

Having looked over the forum, I


In my Main Activity I have

B4X:
Dim objPhone As Phone
DEVICE_ID = objPhone.GetSettings("android_id")
SenderId = "xxxx"
Then in my Init activity I have

B4X:
Sub registerDevice

   CallSubDelayed2(PushService, "RegisterDevice", False)
   
End Sub
In my PushService, I have:

B4X:
Sub Service_Start (StartingIntent As Intent)
   Select StartingIntent.Action
     Case "com.google.android.c2dm.intent.REGISTRATION"
       HandleRegistrationResult(StartingIntent)
     Case "com.google.android.c2dm.intent.RECEIVE"
       MessageArrived(StartingIntent)
   End Select
End Sub

Sub RegisterDevice (Unregister As Boolean)
   Dim i As Intent
   If Unregister Then     
     i.Initialize("com.google.android.c2dm.intent.UNREGISTER", "")
   Else
     i.Initialize("com.google.android.c2dm.intent.REGISTER", "")
     i.PutExtra("sender", Main.GCM_PROJECT_ID)
   End If
   Dim r As Reflector
   Dim i2 As Intent
   i2 = r.CreateObject("android.content.Intent")
   Dim pi As Object
   pi = r.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
     Array As Object(r.GetContext, 0, i2, 0), _
     Array As String("android.content.Context", "java.lang.int", "android.content.Intent", "java.lang.int"))
   i.PutExtra("app", pi)
   StartService(i)
End Sub

Sub HandleRegistrationResult(Intent As Intent)

   If Intent.HasExtra("error") Then
     Utils.Log_("Error: " & Intent.GetExtra("error"))
     ToastMessageShow("Error: " & Intent.GetExtra("error"), True)
   
   Else If Intent.HasExtra("unregistered") Then
   
     ' Here we unregister the DEVICE_ID at our own servers
     Dim j As HttpJob
     j.Initialize("UnregisterTask", Me)
     Dim paramsStr As String = "deviceID="&Main.DEVICE_ID&"&uuid="&Main.CURRENTUSER.ID&"&apptype=ANDROID&app="&Main.APP_APPLICATION_NAME.Replace("%s", "")
     j.PostString(Callbacks.CB_DEVICE_UNREGISTRATION_URL, paramsStr)

   Else If Intent.HasExtra("registration_id") Then
     Dim rid As String
     rid = Intent.GetExtra("registration_id")
     Dim j As HttpJob
     j.Initialize("RegisterTask", Me)

     ' Here we register the DEVICE_ID at our own servers
     Dim paramsStr As String = "deviceID="&rid&"&uuid="&Main.CURRENTUSER.ID&"&apptype=ANDROID&app="&Main.APP_APPLICATION_NAME.Replace("&s", "")     
     j.PostString(Callbacks.CB_DEVICE_REGISTRATION_URL, paramsStr)

   End If
End Sub
Sub JobDone(Job As HttpJob)

....

End Sub
What I fail to understand is, since the RegisterDevice() does not set the registration_id as an extra for the intent started (to Main.DEVICE_ID), how does GCM/Google know the registration ID ???
So in RegisterDevice() do I need to set the registration_id to Main.DEVICE_ID in some extra for the intent ?

Please help me understand !

Thanks !


/Henrik
 
Top