简体   繁体   中英

Parse Android disable Push notifications

How to disable push notifications in the new Parse Android SDK?

I have a preference in my app which is for disable notification. So when the user uncheck the preference I want to disable the notifications (shut down the push service) for the app. For example in the old SDK you just have to call PushService.setDefaultCallback(null) and the push service is stopped.

This is how I subscribe for push notifications on my Application class:

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

On my preferences fragment this how I listen for preference change:

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

Enabling/disabling receivers doesn't help also. Strangely, after reenabling, the app can't receive pushes. The best way I've reached is:

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

I found it much easier to subscribe users to a "default" channel. Then, if they turn push notifications off, you can just remove that from the list of channels. It also makes it a little nicer when you trigger the push notifications to go to an actual channel, instead of just everyone.

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

Simply go to Parse.com and open your application's setting page. Then open 'Push' tab and toggle the 'Client push enabled?'. More Info Here.

See the image for clarity:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM