0

I'm trying to let the user enable/disable if they want to receive notifications or not. I managed to implement a checkbox for notification and to create a preference class.

This is the my preferences class

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

However when I uncheck the checkbox I still receive the notification. Whats the problem?

zapl
  • 63,179
  • 10
  • 123
  • 154
K3v
  • 71
  • 7
  • 1
    idk how pushbots works but in https://github.com/pushbots/enable-disable-push-example-android/blob/master/app/src/main/java/com/test/login/Home.java someone seems to have implemented what you're trying and in that code it also (un)registers. – zapl Apr 23 '16 at 20:24
  • still getting notifications even box is unchecked – K3v Apr 23 '16 at 21:07

2 Answers2

0

I finally managed to get the checkbox working. I changed a little bit the code here it the new one. Hope it helps others!!!

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

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

setPushEnabled(false) unregister the device from Pushbolt Dashboard. If you want to disable the notification only, you can simply do setNotificationEnabled(false)

You can Enable/Disable notification using checkbox. The code is given below :

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 of checkbox

    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();
            }
        }
    });
Amal Dev S I
  • 938
  • 13
  • 18