简体   繁体   English

如何禁用来自推送机器人的推送通知

[英]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? 有什么问题?

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

setPushEnabled(false) unregister the device from Pushbolt Dashboard. setPushEnabled(false)从Pushbolt仪表板注销设备。 If you want to disable the notification only, you can simply do setNotificationEnabled(false) 如果只想禁用通知,则只需执行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 复选框的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();
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM