Android Question Is it possible to catch a GCM-Message when the screen is locked ?

Michael Müller Anywhere

Member
Licensed User
Longtime User
Hi,
I have a service to receive GCM-Messages. Is there a solution for catching the message when the user locks the screen of his phone (like WhatstUp) ? I have always tried two ways:

1) the B4A-GCM-documentation
2) the Broadcast library
Both doesn't give a signal when the new message comes in while the screen is locked. I that moment I unlock the screen (much later), the stored messages will be shown.

I have tried this both variants (change TRUE with FALSE to change the method):

B4X:
Sub Service_Create
    If True Then
        Broadcast.Initialize("BroadcastReceiver")
    Else
        Notification1.Initialize
        Notification1.Icon = "icon" 'use the application icon file for the notification
        Notification1.Vibrate = False
    End If
End Sub

Sub Service_Start (StartingIntent As Intent)

    If True Then
        Broadcast.addAction("com.google.android.c2dm.intent.RECEIVE")
        Broadcast.SetPriority(999)   
        Broadcast.registerReceiver("")       
    Else
        Select StartingIntent.Action
            Case "com.google.android.c2dm.intent.REGISTRATION"
                HandleRegistrationResult(StartingIntent)
            Case "com.google.android.c2dm.intent.RECEIVE"
                MessageArrived(StartingIntent)
        End Select
        Notification1.SetInfo("ELS Servide", "Service ist gestartet", Main)
        Notification1.Sound = False
        Service.StartForeground(1, Notification1)
    End If

End Sub


Sub BroadcastReceiver_OnReceive (Action As String, i As Object)
Dim i2 As Intent
   i2=i
   Log(i2.ExtrasToString)
   MessageArrived(i2)
End Sub
 

Michael Müller Anywhere

Member
Licensed User
Longtime User
The service will be started when the message arrives as it uses a static intent filter.

Are you checking the logs to see whether the service was started or not?

Yes the service is shown as started in the log. As i said, when I unlock the screen (pattern lockscreen) exactly in this moment the notification alerts the message I send 1 hour before. That shows, that the service is running.

Can it be that the service or the phone or something is in pause and I have to wake it somehow?

Here is the manifest:

B4X:
'C2DM Permissions
AddManifestText(<permission android:name="$PACKAGE$.permission.C2D_MESSAGE" android:protectionLevel="signature" />)
AddPermission($PACKAGE$.permission.C2D_MESSAGE)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
' Push Service Receiver Attribute
SetReceiverAttribute(mod_PushService, android:permission, "com.google.android.c2dm.permission.SEND")
' Service Receiver Text
AddReceiverText(mod_PushService,
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="$PACKAGE$" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="$PACKAGE$" />
</intent-filter>)
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Yes the service is shown as started in the log. As i said, when I unlock the screen (pattern lockscreen) exactly in this moment the notification alerts the message I send 1 hour before. That shows, that the service is running.

Can it be that the service or the phone or something is in pause and I have to wake it somehow?

Here is the manifest:

B4X:
'C2DM Permissions
AddManifestText(<permission android:name="$PACKAGE$.permission.C2D_MESSAGE" android:protectionLevel="signature" />)
AddPermission($PACKAGE$.permission.C2D_MESSAGE)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
' Push Service Receiver Attribute
SetReceiverAttribute(mod_PushService, android:permission, "com.google.android.c2dm.permission.SEND")
' Service Receiver Text
AddReceiverText(mod_PushService,
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="$PACKAGE$" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="$PACKAGE$" />
</intent-filter>)
I remember making an app for a client, where i would display the message received while the screen was locked, problem is i can't remember how i did it, but i know i created a transparent activity with a panel and a label to display the message.

I will post the code if i can find it.

thanks,
Walter
 
Upvote 0

Michael Müller Anywhere

Member
Licensed User
Longtime User
I'll found the solution:

The sender must use "delay_while_idle=0" instead of "1"


sPostData = "collapse_key=score_update&time_to_live=108" & _
"&delay_while_idle=0" & _
"&data.message=" & msg & _
"&data.time=" & Now & _
"&registration_id=" & device_id
 
Upvote 0
Top