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

标签 android notifications google-cloud-messaging

新的 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"
      }

}

关于android - GCM 3.0 - gcm 不会自动显示带有通知参数的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187563/

相关文章:

android notifyItemRangeInserted 禁用自动滚动

android - 检查用户是否已授予 NotificationListener 访问我的应用程序的权限

android - NotificationCompat 4.1 SetSmallIcon 和 SetLargeIcon

iphone - 卸载并重新安装应用程序时本地通知触发器

javascript - Chrome 消息传递 - 显示数据而不是固定消息

android - 如何更改数据库而不必每次都卸载 APK?

Android设备USB调试

android - Canvas 画不光滑的圆圈

android - GCM 注册 ID 会过期吗?

google-cloud-messaging - GCM 适用于 4.1 但不适用于 2.3 android 版本