Farbod Salamat-Zadeh Farbod Salamat-Zadeh - 1 year ago 75
Android Question

Displaying multiple notifications at user-defined times

I've been programming in Android for over a year but have never used notifications before, which I need for one of my apps.

In the app, there are events that can be set at different times. Obviously, the user can choose to change these times or add/remove events.

I plan to use notifications to notify the user 5 minutes before their event starts.

I've read through the Android developer docs for Building a Notification and Services (which I am also new to). I have decided to use

s because I figured that the notification would need to be shown even when the app not running. To help me, I have been referring to one particular SO answer as guidance.

I begun by experimenting a little with the notification builder classes in my
class like so:

public class NotificationService extends IntentService {

private static final int NOTIFICATION_ID = 1;

public NotificationService() {

protected void onHandleIntent(Intent intent) {

private void showNotification() {
Intent intent = new Intent(this, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(
this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle("Test notification")
.setContentText("Test description")

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());


I also use the following to start this
in the splash screen of my app:

Intent serviceIntent = new Intent(this, NotificationService.class);

To display notifications at specific times, I've read about
, mainly through SO questions like this one. I also know that to display multiple notifications, I would need different notification ids (like my constant

However, what I am unsure of is dynamically updating the times that notifications would be shown each time an event is added, removed, or its times changed.

Could someone provide me some guidance on how I could achieve this?

Answer Source

You implement notification part. For notify user at Specific time you should set AlarmManager. I paste whole code you need then explain each part:

public class AlarmReceiver extends WakefulBroadcastReceiver {

    AlarmManager mAlarmManager;
    PendingIntent mPendingIntent;

    public void onReceive(Context context, Intent intent) {

        int mReceivedID = Integer.parseInt(intent.getStringExtra(AddReminderActivity.EXTRA_REMINDER_ID));

        // Get notification title from Reminder Database
        ReminderDatabase rb = new ReminderDatabase(context);
        ApiReminderModel reminder = rb.getReminder(mReceivedID);
        String mTitle = reminder.getName();

        // Create intent to open ReminderEditActivity on notification click

        // handling when you click on Notification what should happen
        Intent editIntent = YourActivity.createActivity(context, reminder.getChannelId(), reminder.getStartTime());
        PendingIntent mClick = PendingIntent.getActivity(context, mReceivedID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Create Notification
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_logo))

        NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(mReceivedID, mBuilder.build());

    public void setAlarm(Context context, Calendar calendar, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Put Reminder ID in Intent Extra
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(AddReminderActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Calculate notification time
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        // Start alarm using notification time
        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

        // Restart alarm if device is rebooted
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

    public void cancelAlarm(Context context, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Cancel Alarm using Reminder ID
        mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);

        // Disable alarm
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

As you see we have setAlarm which notify users at specific time. I also pass calendar instance which is initiated before ( assign a time which user should be notify).

 Calendar mCalendar = Calendar.getInstance();
         mCalendar.add(Calendar.DAY_OF_YEAR, 7);// assume you want send notification 7 days later

        new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar, id);

We have another method cancelAlarm. If you want to delete a Alaram just pass unique ID of Alarm (Which already used for creation of Alarm).

Also don't forget to add this Service to your AndroidManifest.xml:

<receiver android:name=".service.AlarmReceiver" />

There is only on thing remain When you reboot your device you should set AlarmsManager again so you need a BootRecivier service.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download