如何在新的Parse Android SDK中禁用推送通知?解析Android禁用推送通知
我在我的應用程序中有一個首選項,用於禁用通知。因此,當用戶取消選擇我想要禁用應用程序通知(關閉推送服務)的偏好設置時。例如,在舊的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我將代碼示例添加到 – Gustavo