【问题标题】:Parse Android disable Push notifications解析 Android 禁用推送通知
【发布时间】:2015-01-07 18:02:48
【问题描述】:

如何在新的 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");
                    }
                }
            });
        }
    }
}

【问题讨论】:

  • 看看我的回答。
  • @PsyDuck 我在上面的问题中添加了代码示例

标签: android parse-platform push-notification


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      只需访问 Parse.com 并打开应用程序的设置页面。然后打开“推送”选项卡并切换“启用客户端推送?”。More Info Here.

      看图清楚:

      【讨论】:

      • 这怎么行不通?您想禁用推送,这就是您的操作方式。
      • 所以你说如果我调用 ParsePush.subscribeInBackground("") 订阅广播频道然后启用客户端推送设置,然后调用 ParsePush.unsubscribeInBackground("") 我应该不接收未来的通知
      • @Gustavo 你应该编辑这个问题。我根据我在您的问题中看到的内容给出了答案。
      • 因此,您只想退订一个用户,但整体上保持推送活动。那么当你使用 ParsePush.unsubscribeInBackground("Channel_name") 时,你会在 LogCat 中得到什么作为响应?
      • 发布与此相关的部分代码。让我看看你在做什么,在哪里做。您订阅的部分和您取消订阅的部分。 + 当您致电ParsePush.unsubscribeInBackground("Channel_name") 时会发生什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      相关资源
      最近更新 更多