Android Question GCM Push notification - message not visible in notification area

Pravin Shah

Member
Licensed User
Hi All,

I have successfully tested the GCM Push Notification library created by Erel (url below).
http://www.basic4ppc.com/android/fo...otification-gcm-framework-and-tutorial.19226/

I am using a VB application to send messages to my app and I am able to see them if the app is running as toast message. However I want these messages to appear in Notification area (similar to mail and whatsapp) irrespective of my app is running or not. The message should appear first in notification area and then user click on the message which will take him to my app.

It looks like it must be a simple answer but I could not find it after searching a lot. Appreciate your help.
 

Chandrashekhar

Member
Licensed User
Handle MessageArrvied Event like .
B4X:
Sub MessageArrived (Intent As Intent)
    '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)
    Dim n As Notification
    n.Initialize
    n.AutoCancel = True
    n.Icon = "icon"
    n.SetInfo("TEST",Data,"main")
    n.Notify(1)
 

Pravin Shah

Member
Licensed User
Thanks Chandrashekhar, appreciate your quick reply..

The above code works but the issue is that I got the notification when the app is running in background. If I close the app then the message is not received.
My requirement is that irrespective of app running or closed, the message should be received in notification area. These messages are coming from other VB application and I can not control that users need to keep running my app.

I need functionality similar to mail and whatsapp where we receive new message notification irrespective of that app is running or not.
 

DonManfred

Expert
Licensed User
My requirement is that irrespective of app running or closed, the message should be received in notification area. These messages are coming from other VB application and I can not control that users need to keep running my app.
then maybe you need to run your pushservice in your app "Sticky". The Service MUST run if you want to recieve the push-notification.
 

Pravin Shah

Member
Licensed User
Yes Erel, I tried in release mode but the message are not being shown as toast message or in notification area.
However when I started the app again then it started receiving message.

I am using the Push Notification example as per the following url
http://www.basic4ppc.com/android/fo...otification-gcm-framework-and-tutorial.19226/

and the VB.net application example created using following URL. This .net exe is used to send messages using API Key, Reg ID and project number
http://www.basic4ppc.com/android/fo...messaging-server-visual-studio.29159/#content

There are no errors in logs.
 

Pravin Shah

Member
Licensed User
Sorry Erel, it looks like I am missing something. I did some more testing based on above discussion explained below

1. I restarted handset and tried sending message. The message was sent by .net exe but NOT received by mobile app.
2. I started mobile app and then send the message. The message was displayed and was shown in notification area
3. Now instead of killing app, I have rebooted the handset and tried sending message. But the message was not delivered.

Based on above observation, it looks like the PushService service should always be run in background. Please correct me if I am wrong or if there is any alternative way.

PS Sorry I was traveling so could not reply quickly to your reply. And thanks for helping.
 

Pravin Shah

Member
Licensed User
Sorry Erel, I was out of town so could not reply earlier.. I am back yesterday

As per your suggestion, I have tested the app with USB debug on and sent messages but no message received, also I could not find info about that message in log.

Here are some more observations
When App is running - The service "pushservice" was running in the background and it was reading message and showing in the notification area
When App is not running - I could not find "PushService" process running in the background.
I also tried running this service as sticky by adding following code, but when I restarted the mobile, I did not find the service "PushService" running in background. Is there any other way to run this service in background continuously?

B4X:
#Region Module Attributes
    #StartAtBoot: true
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
 

Chandrashekhar

Member
Licensed User
Please check your manifest file. it MUST contain following code.

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(PushService, android:permission, "com.google.android.c2dm.permission.SEND")
' Service Receiver Text
AddReceiverText(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>)
Also have you tried sending msg. from Erel's JAR file?
 

Pravin Shah

Member
Licensed User
Yes Chandrashekhar, I rechecked the manifest file which is same as above and also I have tried sending messages from Erel's JAR file, if the app is running in background then it receives the message otherwise there are no messages. Can you suggest any other alternative? thanks
 

Erel

Administrator
Staff member
Licensed User
When App is not running - I could not find "PushService" process running in the background.
It shouldn't run all the time. This is how static intent filters work.

Are you killing the process with a task manager? In that case it is possible that Android disables your app until it is relaunched.
 

Pravin Shah

Member
Licensed User
No Erel, I am not killing the process. This is how I am doing the testing -
1. Run the app and send message from VB and check if they are being received. I am getting these messages.
2. Then I reboot the mobile (do not run the app) and send messages from VB and check if they are being received. In this case the messages are NOT received.
Since I am running PushService as sticky service, I tried to check if it is running in background, but I could not find it running. This is where I am stuck.
 

Pravin Shah

Member
Licensed User
Oh..Ok.. That could be the case. I am using MI3 from XIAOMI having OS as MIUI and Android 4.4.4. I will test this app on other device and let you know the results.

Thanks Erel and Chandrashekhar for your efforts to resolve this issue.
 
Top