使用android不推荐使用PushService类型的方法setDefaultPushCallback

usi*_*ing 6 android push deprecated parse-platform

我是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;
    }

}
Run Code Online (Sandbox Code Playgroud)

在我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>
Run Code Online (Sandbox Code Playgroud)

从这个链接 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题.您将获得帮助.

Ole*_*nko 4

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

 <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>
Run Code Online (Sandbox Code Playgroud)

然后你的接收者子类应该重写 'getActivity()` 方法

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