解析 Android 禁用推送通知

Parse Android disable Push notifications

如何在新的 Parse Android SDK 中禁用推送通知?

我在我的应用程序中有一个首选项是禁用通知。因此,当用户取消选中我想要禁用该应用程序的通知(关闭推送服务)的首选项时。例如,在旧的 SDK 中,您只需调用 PushService.setDefaultCallback(null) 即可停止推送服务。

这是我在应用程序类上订阅推送通知的方式:

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

    // Initialize the Parse SDK.
    Parse.initialize(this, BuildConfig.PARSE_APP_ID, BuildConfig.PARSE_CLIENT_KEY);

    // Register for Push Notifications ?
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    boolean notificationsEnabled =
            sharedPref.getBoolean(SettingsFragment.PREF_KEY_ENABLE_NOTIFICATIONS, true);
    if(notificationsEnabled){
        ParsePush.subscribeInBackground("", new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Timber.d("successfully subscribed to the broadcast channel.");
                } else {
                    Timber.e(e, "failed to subscribe for push");
                }
            }
        });
    }
}

关于我的偏好片段,我是如何监听偏好变化的:

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if(key.equals(PREF_KEY_ENABLE_NOTIFICATIONS)){
        boolean notificationsEnabled = sharedPreferences.getBoolean(PREF_KEY_ENABLE_NOTIFICATIONS, true);
        if(notificationsEnabled){
            ParsePush.subscribeInBackground("", new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        Timber.d("successfully subscribed to the broadcast channel.");
                    } else {
                        Timber.e(e, "failed to subscribe for push");
                    }
                }
            });
        }
        else {
            ParsePush.unsubscribeInBackground("", new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        Timber.d("successfully un-subscribed from the broadcast channel.");
                    } else {
                        Timber.e(e, "failed to un-subscribe for push");
                    }
                }
            });
        }
    }
}

只需转至 Parse.com 并打开应用程序的设置页面。然后打开 'Push' 选项卡并切换 'Client push enabled?'.More Info Here.

为了清楚起见,请查看图片:

Enabling/disabling 接收器也无济于事。奇怪的是,重新启用后,该应用无法接收推送。我达到的最佳方式是:

public class Receiver extends ParsePushBroadcastReceiver {

    @Override
    protected void onPushOpen(Context context, Intent intent) {...}

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        if (Esperandro.getPreferences(Prefs.class, context).show_notifications()) { //your shared preferences
            super.onPushReceive(context, intent);
        }
    }
}

我发现让用户订阅 "default" 频道要容易得多。然后,如果他们关闭推送通知,您只需将其从频道列表中删除即可。当您将推送通知触发到实际频道而不是每个人时,它也会变得更好一些。

ParseInstallation installation = ParseInstallation.getCurrentInstallation();
List<String> channels = PushNotificationManger.getDefaultChannels();
installation.put("channels", channels);
installation.saveInBackground();