gpt4 book ai didi

android - GCM - 以编程方式注册广播接收器时获取 SERVICE_NOT_AVAILABLE

转载 作者:行者123 更新时间:2023-11-29 21:39:51 26 4
gpt4 key购买 nike

我正在将 GCM 与 Android 应用集成以接收推送通知。一切正常,我能够收到通知。问题是,即使应用程序未运行,我也会收到这些通知,但我需要在应用程序正在运行时接收它们。

所以我想如果我在应用程序中以编程方式注册广播接收器,然后在 onDestroy() 中注销它就可以完成这项工作。
然而,这不起作用,当尝试获取注册 ID 时,它返回 SERVICE_NOT_AVAILABLE。

这是工作 list 中声明的​​广播接收器,但我一直收到通知:

 <receiver android:name=".service.MyBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.testgcm"/>
</intent-filter>
</receiver>

这就是我以编程方式创建它的方式,但收到上述错误:

receiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.google.android.c2dm.intent.RECEIVE");
filter.addCategory("com.testgcm");
registerReceiver(receiver, filter, "com.google.android.c2dm.permission.SEND", null);

你能告诉我我做错了什么吗?或者,也许有更好的方法,以便仅在应用程序运行时接收通知?

最佳答案

事实证明,这与我以编程方式注册 BroadcastReceiver 的事实关系不大,但实际上它代表注册操作失败。

这是文档所说的:

When the application receives a com.google.android.c2dm.intent.REGISTRATION intent with the error extra set as SERVICE_NOT_AVAILABLE, it should retry the failed operation (register or unregister).

In the simplest case, if your application just calls register and GCM is not a fundamental part of the application, the application could simply ignore the error and try to register again the next time it starts. Otherwise, it should retry the previous operation using exponential back-off. In exponential back-off, each time there is a failure, it should wait twice the previous amount of time before trying again. If the register (or unregister) operation was synchronous, it could be retried in a simple loop. However, since it is asynchronous, the best approach is to schedule a pending intent to retry the operation.

关于android - GCM - 以编程方式注册广播接收器时获取 SERVICE_NOT_AVAILABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17485270/

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