簡體   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