K3v
K3v

Reputation: 71

How to disable push notification from pushbots

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?

Upvotes: 0

Views: 293

Answers (2)

Amal Dev S I
Amal Dev S I

Reputation: 958

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

Upvotes: 0

K3v
K3v

Reputation: 71

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

Upvotes: 0

Related Questions