Search code examples
androidandroid-studioandroid-sqliteandroid-notifications

Adding multiple reminders - Reminder Application


I am building a reminders app which should be able to set multiple reminders. I have a AddNewReminder bottom sheet view which has an editText to enter reminder name, a datepicker dialog, a timepicker dialog and a save button which saves the data to an SQLite database and sets the reminder.

AddNewReminder.java (Save Button's OnClickListener) :-

String name = mReminderNameInsert.getText().toString();
id = new Random().nextInt(100);

Intent reminderReciever = new Intent(getContext(), ReminderReceiver.class);
reminderReciever.putExtra("reminderName", name)
                        .putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, reminderReciever, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, ih);
calendar.set(Calendar.MINUTE, imin);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DAY_OF_MONTH, iday);
calendar.set(Calendar.MONTH, imon);
long lTime = calendar.getTimeInMillis();

AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.setExact(AlarmManager.RTC_WAKEUP, lTime, pendingIntent);

Then used a broadcast reciever to display the notification.

String mReminderName;
String CHANNEL_ID = "channel";
int id;

    @Override
    public void onReceive(Context context, Intent intent) {
        mReminderName = intent.getStringExtra("reminderName");
        id = intent.getIntExtra("id", 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
            NotificationManager manager = context.getSystemService(NotificationManager.class);
            manager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "id");
        mBuilder.setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
                .setContentTitle("It's Time..!!")
                .setContentText(mReminderName + "" + id)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setAutoCancel(true);
        NotificationManagerCompat compat = NotificationManagerCompat.from(context);
        compat.notify(id, mBuilder.build());
    }

The reminder notification is displayed, but only the latest one.. (Like if I set two reminders, one for 8:30 and the other for 8:31, only the reminder for 8:31 is displayed as notification)..

Should i add the Alarm manager code to MainActivity instead of AddNewReminder fragment...

Any help will be really appreciated


Solution

  • The PendingIntent you use in both the cases, one for alarm at 8:30 and other for alarm at 8:31 represents the same object thereby calling cancel() to remove the old one. As official docs mentions,

    A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen

    If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent#filterEquals(Intent), or different request code integers supplied to PendingIntents

    So try the following.

    Intent reminderReciever = new Intent(getContext(), ReminderReceiver.class);
    reminderReciever.putExtra("reminderName", name);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), id, reminderReciever, 0);