0

The problem is that my GCM message arrives at my app but is not (automatically) displayed on screen.

07-06 21:33:11.525 11269-11269/com.example.myapp D/ActivityThread: BDC-Calling onReceive: intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp cmp=com.example.myapp/com.google.android.gms.gcm.GcmReceiver (has extras) }, receiver=com.google.android.gms.gcm.GcmReceiver@3a59595e
07-06 21:33:11.534 11269-11269/com.example.myapp D/ActivityThread: BDC-RECEIVER handled : 0 / ReceiverData{intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp (has extras) } packageName=com.example.myapp resultCode=-1 resultData=null resultExtras=null}
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-Calling onStartCommand: com.example.myapp.MyGcmListenerService@bc04a0c, flags=0, startId=1
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@df9e13f className=com.example.myapp.MyGcmListenerService packageName=com.example.myapp intent=null}
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@df9e13f className=com.example.myapp.MyGcmListenerService packageName=com.example.myapp intent=null}
07-06 21:33:11.543 11269-11269/com.example.myapp D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@df9e13f startId=1 args=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp (has extras) }}07-06 21:33:11.565 11269-11269/com.example.myapp D/ActivityThread: SVC-Destroying service: com.example.myapp.MyGcmListenerService@bc04a0c
07-06 21:33:11.565 11269-11269/com.example.myapp D/ActivityThread: SVC-STOP_SERVICE handled : 0 / android.os.BinderProxy@df9e13f
07-07 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: bundle: Bundle[{key1=message1, key2=message2, notification=Bundle[{e=1, body=This is a notification that will be displayed ASAP., icon=ic_launcher, title=Hello, World}], collapse_key=com.example.myapp}]
07-06 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: From: 28REDACTED98
07-06 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: Message: null

My receiving code is:

public class MyGcmListenerService extends GcmListenerService {
    public static final String TAG = MyGcmListenerService.class.getSimpleName();

    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.d(TAG, "bundle: " + data);
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
        ...

The sending code (using node-gcm) is:

  var message = new ngcm.Message({
        priority: 'high',
        contentAvailable: true,
        restrictedPackageName: "com.example.myapp",
        data: {
                key1: 'message1',
                key2: 'message2'
        },
        notification: {
                title: "Hello, World",
                icon: "ic_launcher",
                body: "This is a notification that will be displayed ASAP."
        }
  });

I have tried with and without contentAvailable and restrictedPackage but they make no difference.

My questions are:

  1. Should my receiving code need to create the on-screen notification when a message arrives, or should this (as I believe) be done for me by Android?
  2. The sample code says that the bundle should include a data.getString("message"); but my app sees this as null. Why?
fadedbee
  • 42,671
  • 44
  • 178
  • 308
  • 1
    To get a notification created automatically, your push message had to follow a specific format for message, otherwise you have to create it manualy – Vivek Mishra Jul 07 '16 at 05:26
  • @VivekMishra Thanks for the information. Can you write an answer with an example of that specific format? – fadedbee Jul 07 '16 at 07:10

1 Answers1

1

To show the gcm notification automatically your push message json format should be same like this

"notification" : {
            "body" : "great match!",
            "icon" : "ic_launcher.png",
            "title" : "Portugal vs. Denmark"
                }

For further clarification you can see this link
Android GCM Notifications

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • Many thanks for answering. Could you explain how your example is different from the `notification` that is present in my sent message, and in the received bundle `Bundle[{key1=message1, key2=message2, notification=Bundle[{e=1, body=This is a notification that will be displayed ASAP., icon=ic_launcher, title=Hello, World}], collapse_key=com.example.myapp}]`? – fadedbee Jul 07 '16 at 07:30
  • 1
    @chrisdew Only difference that I can see that extra `e=1` in your bundle. – Vivek Mishra Jul 07 '16 at 07:34
  • 1
    @chrisdew I have added one link for reference in answer – Vivek Mishra Jul 07 '16 at 08:52
  • The answer was in your link - I had the app in the foreground and that was suppressing the notifications. – fadedbee Jul 08 '16 at 07:38
  • Glad it helped you – Vivek Mishra Jul 08 '16 at 07:39