繁体   English   中英

解析Android禁用推送通知

[英]Parse Android disable Push notifications

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

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

这是我在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");
                }
            }
        });
    }
}

关于我的首选项片段,这就是我如何监听首选项更改的方式:

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

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

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

我发现向用户订阅“默认”频道要容易得多。 然后,如果他们关闭了推送通知,则只需将其从频道列表中删除即可。 当您触发推送通知而不是每个人都转到实际频道时,这也使它变得更好。

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

只需访问Parse.com并打开应用程序的设置页面即可。 然后打开“推送”标签,然后切换“已启用客户端推送?”。 更多信息在这里。

为清楚起见,请参见图片:

在此处输入图片说明

暂无
暂无

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

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