简体   繁体   English

解析Android禁用推送通知

[英]Parse Android disable Push notifications

How to disable push notifications in the new Parse Android SDK? 如何在新的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. 例如,在旧的SDK中,您只需要调用PushService.setDefaultCallback(null)即可停止推送服务。

This is how I subscribe for push notifications on my Application class: 这是我在Application类上订阅推送通知的方式:

@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. 只需访问Parse.com并打开应用程序的设置页面即可。 Then open 'Push' tab and toggle the 'Client push enabled?'. 然后打开“推送”标签,然后切换“已启用客户端推送?”。 More Info Here. 更多信息在这里。

See the image for clarity: 为清楚起见,请参见图片:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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