【问题标题】:GCM 3.0 - gcm doesn't automatically show notification with notification parameterGCM 3.0 - gcm 不会自动显示带有通知参数的通知
【发布时间】:2015-07-02 14:38:23
【问题描述】:

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

docs中所说:

带有预定义选项的通知参数表示如果客户端应用在 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>

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

是否需要做其他事情才能允许自动显示?

【问题讨论】:

  • 你在notification有效载荷中发送什么?
  • @shkschneider 编辑了这个问题。但我发送titlebodyicon
  • “自动显示”是什么意思?
  • @injecteer 在 I/O 15 上据说如果 GCM 包含某些信息(标题、图标、文本),它将自行显示通知。或如文档中所述GCM will display the message on the client app’s behalf if the client app implements GCMListenerService
  • 可能在即将发布的 sdk 版本中?

标签: android notifications google-cloud-messaging


【解决方案1】:

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多