0

Below are the code suggested by "iamverysmart" and its working like a charm.

But now, how to set notification on user selected date and time?

if that particular notification is been set, how am i suppose to cancel the notification?

Thanks in advance

private void addNotification() {

NotificationCompat.Builder builder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon1)
                .setContentTitle("Notifications Example")
                .setContentText("This is a test notification");

Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);

// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, builder.build());
}
mrifnoc
  • 1
  • 2
  • Possible duplicate of [show a notification on a particular date and time](http://stackoverflow.com/questions/31086226/show-a-notification-on-a-particular-date-and-time) – Sufian Aug 30 '16 at 16:14

1 Answers1

0

most of this code is inspired from How exactly to use Notification.Builder so you should do more research before you ask

add the toggle button in your xml

<ToggleButton
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="on"
    android:textOff="off"
    android:onClick="myButtonClicked"/>

then inside the onclick method we call our notification method

public void myButtonClicked(View view) {

    boolean isOn = ((ToggleButton)view.findViewById(R.id.myButton)).isChecked();

    if (isOn) {
        // do stuff
    } else {
        addNotification()
    }
}

the actual notification method. FM_NOTIFICATION_ID is a constant int

private void addNotification() {

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon1)
                    .setContentTitle("Notifications Example")
                    .setContentText("This is a test notification");

    Intent notificationIntent = new Intent(this, MyActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(FM_NOTIFICATION_ID, builder.build());
}

to remove the notification

private void removeNotification() {
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(FM_NOTIFICATION_ID);
}

I am not sure what you mean by "on user selected date". you mean to send a notification when the user uses the toggle button? that's covered in the myButtonClicked method. be more precise about what you mean.

Community
  • 1
  • 1