Parse push notifications crashes eventually on bac

2019-07-19 07:11发布

问题:

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