GCM 3.0 - gcm does not automatically display a notification with a notification option

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

As the docs say:

A notification parameter with predefined parameters indicates that GCM will display a message on behalf of the client if the client application implements GCMListenerService on Android

However, it’s hard for me to get this to work even if the GCMListenerService 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()); } } 

A notification from the server is logged, but GCM is not displayed.

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

Message sent from server:

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

Is there anything else to do to automatically display?

+6
source share
1 answer

Try to make the notification field native to 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" } } 
+2
source

All Articles