【发布时间】: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