我正在使用 Parse 在 Android 中推送通知,但当我关闭 wifi 时,它最终会在后台崩溃。
它给了我错误:
java.lang.RuntimeException:无法启动接收器 com.parse.ParseBroadcastReceiver:java.lang.RuntimeException:applicationContext 为 null。在使用Parse库之前,您必须调用Parse.initialize(context, applicationId, clientKey)。
这是我的代码
public class ParseBroadcastReceiverCustom extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if(intent == null)
return;
ConnectionDetector connection = new ConnectionDetector(context);
if (connection.isNetworkAvailable()) {
PushService.startServiceIfRequired(context);
}
}
}
以及 list 文件
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
以及主要 Activity 的 onCreate 方法
ConnectionDetector connection = new ConnectionDetector(this);
if (connection.isNetworkAvailable()) {
PushService.setDefaultPushCallback(this, RegisterForget.class);
}
请帮忙
最佳答案
-
新 API 已完全弃用
PushService
,您可以通过在 list 中声明ParsePushBroadcastReceiver
来启用新 API。您可以在announcement blogpost中阅读有关推送的更改- 您的答案就在错误消息本身中。调用 Parse.initialze。你的 Activity 的 onCreate 应该有
Parse.initialize(this, "您的应用程序 ID", "您的客户端 key ");
如果您将此代码放入 Activity 而不是您的应用程序中,那么 Android 可能会因不活动而关闭您的应用程序,然后通过推送 Intent 而不是您的启动器 Activity 重新启动它,在这种情况下您将不会调用 Parse.intialize。
关于android - 解析推送通知最终在 Android 后台崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506543/