使用 android 弃用 PushService 类型的方法 setDefaultPushCallback

The method setDefaultPushCallback from the type PushService is deprecated using android

我是 android 的新手。我一直在处理 deprecated methodpush service 使用 parse 服务的问题。我在这里针对类似问题关注了类似问题,但无法获得解决方案。在我的主要 application class 中,我正在处理这个问题,下面给出了该问题的代码。

public class MApplication extends Application {
    private static MApplication mInstance;

    @Override
    public void onCreate() {
        super.onCreate();

        mInstance = this;
        Parse.initialize(this, JNI.stringFromJNI001(), JNI.stringFromJNI010());

        // Specify an Activity to handle all pushes by default.
        PushService.setDefaultPushCallback(this, MainLandingActivity.class);
// setDefaultPushCallback shows deprecated method here..

     ParseACL defaultACL = new ParseACL();
        // Optionally enable public read access.
        defaultACL.setPublicReadAccess(true);
        defaultACL.setPublicWriteAccess(true);
        ParseACL.setDefaultACL(defaultACL, true);

    }

    public static MApplication getInstance() {
        return mInstance;
    }

}

在我的 manifest 中,我正在使用这个:

<service android:name="com.parse.PushService" />


        <receiver android:name="com.parse.ParseBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

从这个link https://teamtreehouse.com/forum/app-crash-on-push-test我看到我必须像下面的方法一样使用它,但我不知道如何使用它并解决我的问题。

public class Receiver extends ParsePushBroadcastReceiver {

    @Override
    public void onPushOpen(Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

请帮我解决这个问题。您的帮助将不胜感激。

是的,PushService.setDefaultPushCallback() 现在已弃用。您所要做的就是创建您自己的 ParsePushBroadcastReceiver 的子类。因此,在您的 Manifest 文件以及默认的 Parse 推送接收器中,您应该将 Receiver 子类声明如下

 <receiver android:name="com.yourProject.YourReceiver" android:exported=false>
    <intent-filter>
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.OPEN" />
        <action android:name="com.parse.push.intent.DELETE" />
    </intent-filter>
</receiver>

然后你的接收器子类你应该覆盖'getActivity()`方法

protected Class<? extends Activity> getActivity(Context context,
                                Intent intent) {
    return YourActivity.class;
}