1- Initialize PushBots SDK:
Pushbots.sharedInstance().init(this);
2-Register for Notifications: In onCreate() inside MainActivity call registerForRemoteNotifications:
Pushbots.sharedInstance().registerForRemoteNotifications();
3-Add connection checker to your code:
public boolean isNetworkAvailable(Context c) {
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
3-Add toggle button inside activity_main.xml:
<ToggleButton android:id="@+id/toggle"
android:textOff="Receiving notifications disabled."
android:textOn="Receiving notifications enabled."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/toggle_notifications" />
4-Then inside MainActivity onCreate() method inflate toggle button to turn on or off receiving notifications:
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
toggle.setChecked(Pushbots.sharedInstance().isNotificationEnabled());
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isNetworkAvailable(MainActivity.this)) {
Pushbots.sharedInstance().toggleNotifications(isChecked);
} else {
Toast.makeText(MainActivity.this, "You are not connected to internet!", Toast.LENGTH_LONG).show();
}
}
});