fadedbee
fadedbee

Reputation: 44737

Should arriving GCM messages be displayed by Android?

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?

Upvotes: 0

Views: 265

Answers (1)

Vivek Mishra
Vivek Mishra

Reputation: 5705

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

Upvotes: 1

Related Questions