Как отключить push-уведомления от push-ботов
Я пытаюсь позволить пользователям включать / отключать, если они хотят получать уведомления или нет. Мне удалось реализовать флажок для уведомления и создать класс предпочтений.
Это класс моих предпочтений
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;
}
});
}
}
Однако, когда я снимаю флажок, я все равно получаю уведомление. В чем проблема?
2 ответа
Решение
Наконец-то мне удалось установить флажок. Я немного изменил код, вот он новый. Надеюсь, что это помогает другим!
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;
}
});
}
}
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);
}
щелчок флажка
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();
}
}
});