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

Kub*_*tny 6 notifications android google-cloud-messaging

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

正如文档中所述:

带有预定义选项的通知参数表示如果客户端应用程序在Android上实现GCMListenerService,GCM将代表客户端应用程序显示消息

但是,即使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>
Run Code Online (Sandbox Code Playgroud)

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

记录来自服务器的通知但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}]
Run Code Online (Sandbox Code Playgroud)

从服务器发送的消息:

{       
      "registration_ids":[...],
      "data": {
        "notification" : {
            "body" : "great match!",
            "icon" : "ic_launcher.png",
            "title" : "Portugal vs. Denmark"
          }
      } 
}
Run Code Online (Sandbox Code Playgroud)

是否还需要做其他事情才能进行自动显示?

Art*_*son 2

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

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

}
Run Code Online (Sandbox Code Playgroud)