Sub Process_Globals
End Sub
Sub Service_Create
End Sub
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 MessageArrived (Intent As Intent)
Dim From, CollapseKey, Data As String 'ignore
If Intent.HasExtra("from") Then From = Intent.GetExtra("from")
If Intent.HasExtra("data") Then Data = Intent.GetExtra("data")
If Intent.HasExtra("collapse_key") Then CollapseKey = Intent.GetExtra("collapse_key")
'Here you should handle the new message:
Log("New message arrived: " & Data)
ToastMessageShow("New message: " & Data, True)
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.SenderId)
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
Log("Error: " & Intent.GetExtra("error"))
ToastMessageShow("Error: " & Intent.GetExtra("error"), True)
Else If Intent.HasExtra("unregistered") Then
'No lo usamos
Else If Intent.HasExtra("registration_id") Then
Dim rid As String
rid = Intent.GetExtra("registration_id")
Dim j As HttpJob
j.Initialize("RegisterTask", Me)
j.PostString("http://miservidor.com/registro.php","codigoRegistro=" & rid)
End If
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success Then
Select Job.JobName
Case "RegisterTask"
ToastMessageShow("Registration completed successfully.", False)
Case "UnregisterTask"
ToastMessageShow("Unregistration completed successfully.", False)
End Select
Else
ToastMessageShow("Error sending request.", True)
Log(Job.ErrorMessage)
End If
Job.Release
End Sub
Sub Service_Destroy
End Sub