Kuba Spatny
Kuba Spatny

Reputation: 26978

GCM 3.0 - gcm doesn't automatically show notification with notification parameter

The new GCM 3.0 should allow GCM to automatically display notifications sent from server if they contain the notification parameter.

As said in the docs:

The notification parameter with predefined options indicates that GCM will display the message on the client app’s behalf if the client app implements GCMListenerService on Android

However I have trouble getting that to work even though the GCMListenerService is implemented.

AndroidManifest.xml

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="cz.kubaspatny.pushservertest" />
        </intent-filter>
    </receiver>

    <service
        android:name="cz.kubaspatny.pushservertest.gcm.CustomGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

CustomGcmListenerService.java

public class CustomGcmListenerService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle extras) {
        super.onMessageReceived(from, extras);
        Log.d("GcmListenerService", "Received gcm from " + from + " with bundle " + extras.toString());
    }
}

The notification from server is logged but not shown by GCM.

Received gcm from 333813590000 with bundle Bundle[{notification={"icon":"ic_launcher.png","body":"great match!","title":"Portugal vs. Denmark"}, collapse_key=do_not_collapse}]

The message sent from server:

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

Is there anything else needed to be done to allow the automatic display?

Upvotes: 6

Views: 1144

Answers (1)

Arthur Thompson
Arthur Thompson

Reputation: 9225

Try making the notification field a sibling of the data field. The data field is passed to onMessageReceived and the notification field is used to automatically generate the notification.

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

}

Upvotes: 2

Related Questions