'Updating notifications after being added

I want to update the content for a reminder after it has been added before being received by the user. After setting a reminder through AlarmManager using the data stored in sqlite database, the notification from the reminder set only shows the data, title and description, that was first added, not any updated data stored corresponding to the ID as primary key.

Things I have tried:

  • cancelling the pending intent for the reminder then setting it again after updating the data stored in the database but it still displays the same result.
  • using an activity for adding data to be stored in the database to set a reminder and using another activity for updating this data as an attempt to update the reminder content with the same ID issued. One result shows two notifications received, one with initial title and description, and the other with updated information.

Currently, the methods I use to set and cancel a reminder is in my Adapter class for Recyclerview. I am stuck on updating although adding and cancel works fine.

For adding a reminder:

popupMenu.getMenu().add("Set Reminder").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    Reminder reminder = remindList.get(holder.getAdapterPosition());
                    AlarmManager alarmManager = (AlarmManager) view.getContext().getSystemService(Context.ALARM_SERVICE);
                    Intent intent = new Intent(view.getContext(), ReminderReceiver.class);
                    intent.putExtra("reminderId", remindId);
                    intent.putExtra("title", reminder.getReminderTitle());
                    intent.putExtra("definition", reminder.getReminderDefinition());
                    PendingIntent addIntent = PendingIntent.getBroadcast(view.getContext(), remindId, intent, 0);
                    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, Date.parse(reminder.getReminderDateTime()), addIntent);
                    return false;
                }
            });

For cancelling a reminder:

popupMenu.getMenu().add("Cancel Reminder").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    AlarmManager alarmManager = (AlarmManager) view.getContext().getSystemService(Context.ALARM_SERVICE);
                    Intent intent = new Intent(view.getContext(), ReminderReceiver.class);
                    intent.putExtra("reminderId", remindId);
                    PendingIntent addIntent = PendingIntent.getBroadcast(view.getContext(), remindId, intent, 0);
                    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, Date.parse(reminder.getReminderDateTime()), addIntent);

                    assert alarmManager != null;
                    alarmManager.cancel(addIntent);
                    return false;
                }
            });

Receiver class:

public class ReminderReceiver extends BroadcastReceiver {

private static final String CHANNEL_ID = "CHANNEL_REMIND";
String DateTimeChoice, TitleChoice, DescriptionChoice;
int notificationID;

@Override
public void onReceive(Context context, Intent intent) {
    DateTimeChoice = intent.getStringExtra("DateTime");
    notificationID = intent.getIntExtra("reminderId", 0);
    TitleChoice = intent.getStringExtra("title");
    DescriptionChoice = intent.getStringExtra("definition");

    Intent mainIntent = new Intent(context, ViewReminder.class);
    mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent contentIntent = PendingIntent.getActivity(context, notificationID, mainIntent, 0);
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // For API 26 and above
        CharSequence channelName = "My Notification";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
        notificationManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(TitleChoice)
            .setContentText(DescriptionChoice)
            .setContentIntent(contentIntent)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setColor(context.getResources().getColor(R.color.purple_700))
            .setAutoCancel(true);

    notificationManager.notify(notificationID, builder.build());
}

I am new here so I am still trying to get used to asking good questions. Thank you very much for your time and sorry if this is an already asked question.



Solution 1:[1]

Problem

In your Setting reminder method, all the code that updates the reminder is not called because you have returned false in the onMenuItemClick() method.

According to the android documentation :

onMenuItemClick() returns true if the event was handled, false otherwise.

Solution

Just return true instead of false in your onMenuItemClick() method

Update #1

You have to set the updated pending intent again with the exact alarm Id that was used to set the initial alarm. The second argument is of alarm id. Like this, PendingIntent.getActivity(this,UNIQUE_ID_GOES_HERE, intent, 0). The id must be same for every updated pending intent

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1