2016-04-23 15 views
0

通知を受信するかどうかをユーザーに許可/無効にしようとしています。私は通知のためのチェックボックスを実装し、プリファレンスクラスを作成することができました。プッシュボットからプッシュ通知を無効にする方法

これは、私は、チェックボックスのチェックを外ししかし、私はまだ通知を受け取った私の好みのクラス

import com.pushbots.push.Pushbots; 
... 

public class UserSettings extends PreferenceActivity { 

    private CheckBoxPreference notification; 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     addPreferencesFromResource(R.xml.prefs); 

     SharedPreferences settingsPrefs = PreferenceManager.getDefaultSharedPreferences(this); 
     notification = (CheckBoxPreference) findPreference("prefSendNotification"); 
     notification.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
      public boolean onPreferenceChange(Preference preference, 
        Object newValue) { 
       if (newValue.toString().equals("true")) 
       { 
        notificationsOn(); 
        Pushbots.sharedInstance().setNotificationEnabled(true); 

       } 
       else 
       { 
        notificationsOff(); 
        Pushbots.sharedInstance().setNotificationEnabled(false); 
       } 
       return true; 
      } 

      private void notificationsOn() { 
       Pushbots.sharedInstance().setNotificationEnabled(true); 

      } 

      private void notificationsOff() { 
       Pushbots.sharedInstance().setNotificationEnabled(false); 

      } 

      @Override 
      public boolean onPreferenceClick(Preference preference) { 
       // TODO Auto-generated method stub 
       return false; 
      } 
     }); 
     } 
} 

です。 問題が何ですか?

+1

はhttps://github.com/pushbots/enable-disable-push-exampleではなく、作品をpushbotsかIDKのあなたが試しているものを実装しているようですが、そのコードでも登録されています。 – zapl

+0

まだ通知を受け取るボックスはチェックされていません – Kevin

答えて

0

最終的にチェックボックスが機能するようになりました。私は少し新しいコード をここに変更しました。他人を助けることを願っています!

SharedPreferences settingsPrefs = PreferenceManager.getDefaultSharedPreferences(this); 
 
     notification = (CheckBoxPreference) findPreference("prefSendNotification"); 
 
     notification.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 
     \t 
 

 
\t \t \t @Override 
 
\t \t \t public boolean onPreferenceClick(Preference preference) { \t 
 
\t \t \t \t if(notification.isChecked()){ 
 
\t      Log.e("PB", "Notifications are currently ON"); 
 
\t      Pushbots.sharedInstance().setPushEnabled(true); 
 
\t      Pushbots.sharedInstance().register(); 
 
\t     }else{ 
 
\t      Log.e("PB", "Notifications are currently OFF"); 
 
\t      Pushbots.sharedInstance().setPushEnabled(false); 
 
\t      Pushbots.sharedInstance().unRegister(); 
 
\t     } 
 
\t \t \t \t \t return true; 
 
\t \t \t } 
 
}); 
 
\t } 
 
}

0

setPushEnabled(false) Pushboltダッシュボードからデバイスを登録解除します。通知を無効にする場合は、単にsetNotificationEnabled(false)

チェックボックスを使用して通知を有効または無効にすることができます。コードは以下 与えられる:チェックボックスの

Pushbots.sharedInstance().init(this); 

CheckBox toggle_settings=(CheckBox) findViewById(R.id.checkbox_id); 

    final SharedPreferences prf = getSharedPreferences("YOUR PREFERENCE NAME", Context.MODE_PRIVATE); 
    final SharedPreferences.Editor editor = prf.edit(); 

    Boolean is_enable=prf.getBoolean("is_push_enabled", true); 

    if(is_enable==true) 
    { 
     toggle_settings.setChecked(true); 

     //enable notification in push bots 
      Pushbots.sharedInstance().setNotificationEnabled(true); 
    } 
    else 
    { 
     toggle_settings.setChecked(false); 

     //disable notification in push bots    
      Pushbots.sharedInstance().setNotificationEnabled(false); 
    } 

のonclickを

toggle_settings.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // TODO Auto-generated method stub 
      if(isChecked) 
      { 
       editor.putBoolean("is_push_enabled", true); 
       editor.commit(); 

       //enable notification in push bots 
        Pushbots.sharedInstance().setNotificationEnabled(true); 

       Toast.makeText(getApplicationContext(), "Notification is Enabled..!", Toast.LENGTH_SHORT).show(); 

      } 
      else 
      { 
       editor.putBoolean("is_push_enabled", false); 
       editor.commit(); 

       //disable notification in push bots    
       Pushbots.sharedInstance().setNotificationEnabled(false); 

       Toast.makeText(getApplicationContext(), "Notification is Disabled..!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
+0

ありがとう私はすでにコードを修正しましたが、とにかく感謝します – Kevin

関連する問題