gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:33 41 4
gpt4 key购买 nike

新的 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/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com