【发布时间】: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 编辑了这个问题。但我发送
title、body和icon -
“自动显示”是什么意思?
-
@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