新的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"
}
}
}
是否还需要做其他事情才能进行自动显示?
答案 0 :(得分:2)
尝试将通知字段作为数据字段的兄弟。数据字段将传递给onMessageReceived,通知字段用于自动生成通知。
{
"registration_ids":[...],
"notification" : {
"body" : "great match!",
"icon" : "ic_launcher.png",
"title" : "Portugal vs. Denmark"
}
}