GCM 3.0 - gcm 不会自动显示带有通知参数的通知

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

新的 GCM 3.0 应该允许 GCM 自动显示从服务器发送的通知,如果它们包含 notification 参数。

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

然而,即使实施了 GCMListenerService,我也很难让它工作。

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());
    }
}

来自服务器的通知已记录但未由 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}]

从服务器发送的消息:

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

是否需要执行其他操作才能自动显示?

尝试使通知字段成为数据字段的同级字段。数据字段传递给onMessageReceived,通知字段用于自动生成通知。

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

}