2015-01-07 183 views
1

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

看看我的答案。 –

+0

@PsyDuck我將代碼示例添加到 – Gustavo

回答

4

啓用/禁用接收器不也有幫助。奇怪的是,重新啓用後,該應用程序無法收到推送。我已經達到的最佳方式是:

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

只要到Parse.com和打開你的應用程序的設置頁面。然後打開「推送」標籤並切換「客戶端推送已啓用?」。 More Info Here.

爲了清晰看到圖像:

enter image description here

+0

以上的問題,這對我無效 – Gustavo

+0

這怎麼能工作?你想禁用推,這是你如何做到這一點。 –

+0

所以你說如果我調用ParsePush.subscribeInBackground(「」)來訂閱廣播頻道,然後啓用客戶端設置上的推送,然後調用ParsePush.unsubscribeInBackground(「」),我應該不會收到將來的通知 – Gustavo

2

我發現訂閱用戶到「默認」頻道要容易得多。然後,如果他們關閉推送通知,則可以從頻道列表中刪除該通知。當你觸發推送通知去實際的頻道,而不是每個人時,它也會使它更好一些。

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