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?
notification
payload? – shkschneidertitle
,body
andicon
– Kuba SpatnyGCM will display the message on the client app’s behalf if the client app implements GCMListenerService
– Kuba Spatny