如何禁用推送机器人的推送通知
How to disable push notification from pushbots
我正在尝试让用户 enable/disable 是否想要接收通知。我设法实现了一个通知复选框并创建了一个首选项 class.
这是我的偏好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;
}
});
}
}
然而,当我取消选中该复选框时,我仍然会收到通知。
有什么问题吗?
我终于设法使复选框正常工作。我稍微改变了代码
这是新的。希望对其他人有帮助!!!
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)
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);
}
单击复选框
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();
}
}
});
我正在尝试让用户 enable/disable 是否想要接收通知。我设法实现了一个通知复选框并创建了一个首选项 class.
这是我的偏好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;
}
});
}
}
然而,当我取消选中该复选框时,我仍然会收到通知。 有什么问题吗?
我终于设法使复选框正常工作。我稍微改变了代码 这是新的。希望对其他人有帮助!!!
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)
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);
}
单击复选框
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();
}
}
});