Parse push notifications crashes eventually on background in android

萝らか妹 提交于 2020-01-24 14:13:38

问题


I am using Parse to push notifications in android but it crashes eventually on background when i turn wifi off.

it gives me error:

java.lang.RuntimeException: Unable to start receiver com.parse.ParseBroadcastReceiver: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.

here is my code

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);
    }
}
}

and on manifest file

 <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>

and on main activity onCreate method

    ConnectionDetector connection = new ConnectionDetector(this);
    if (connection.isNetworkAvailable()) {
        PushService.setDefaultPushCallback(this, RegisterForget.class);
    }

any help please


回答1:


  1. PushService is complete deprecated with the new APIs, which you enabled by declaring the ParsePushBroadcastReceiver in your manifest. You can read about the changes to push in the announcement blogpost
  2. Your answer is in the error message itself. Call Parse.initialze. Your activity's onCreate should have Parse.initialize(this, "YOUR APPLICATION ID", "YOUR CLIENT KEY"); If you put this code in an activity rather than your application, then Android might shut down your app due to inactivity and then relaunch it via the push intent rather than your launcher activity, in which case you won't have called Parse.intialize.



回答2:


I followed this tutorial step by step and it works now https://parse.com/tutorials/android-push-notifications



来源:https://stackoverflow.com/questions/27506543/parse-push-notifications-crashes-eventually-on-background-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!