2016-04-23 75 views
0

我想让用户启用/禁用,如果他们想要接收通知或不。我设法实现了一个通知复选框并创建了一个首选项类。如何禁用pushbots的推送通知

这是我的喜好类

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

然而,当我取消选中该复选框我仍然收到通知。 什么问题?

+1

IDK的是如何pushbots的作品,但在https://github.com/pushbots/enable-disable-push-example -android/blob/master/app/src/main/java/com/test/login/Home.java有人似乎已经实现了你正在尝试的内容,并且在该代码中它也(未)注册。 – zapl

+0

即使未选中框也会收到通知 – Kevin

回答

0

我终于设法让复选框工作。我改变了一点点代码 这里它是新的。希望它可以帮助别人!

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

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

0

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

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

谢谢我已经修复了代码,但无论如何谢谢 – Kevin