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.
Update: Now I use two separate activities for the add and update reminder functions.
For adding a reminder:
databaseManager.addReminder(titlePicked, descriptionPicked, timePicked, datePicked, dateTimePicked); startActivity(new Intent(getApplicationContext(), MainActivity.class)); setAlarm(); private void setAlarm() { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), ReminderReceiver.class); intent.putExtra("DateTime", dateTimePicked); intent.putExtra("NotifID", remId); intent.putExtra("Title", titlePicked); intent.putExtra("Description", descriptionPicked); PendingIntent addIntent = PendingIntent.getBroadcast(this, remId, intent, 0); alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, Date.parse(dateTimePicked), addIntent); }
For updating a reminder:
databaseManager.updateReminder(remindId, titlePicked2, descriptionPicked2, timePicked, datePicked, dateTimePicked); startActivity(new Intent(getApplicationContext(), MainActivity.class)); updateAlarm(); private void updateAlarm() { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), ReminderReceiver.class); intent.putExtra("DateTime", dateTimePicked); intent.putExtra("NotifID", remindId); intent.putExtra("Title", titlePicked2); intent.putExtra("Description", descriptionPicked2); PendingIntent updateIntent = PendingIntent.getBroadcast(this, remindId, intent, 0); alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, Date.parse(dateTimePicked), updateIntent); }
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("NotifID", 0); TitleChoice = intent.getStringExtra("Title"); DescriptionChoice = intent.getStringExtra("Description"); 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()); }
Adapter class:
int remindId = reminder.getReminderId(); databaseManager = new DatabaseManager(holder.view.getContext()); sqLiteDB = databaseManager.getWritableDatabase(); public void onClick(View view) { Reminder reminder = remindList.get(holder.getAdapterPosition()); PopupMenu popupMenu = new PopupMenu(view.getContext(), view); popupMenu.setGravity(Gravity.END); popupMenu.getMenu().add("Edit").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { Intent intent = new Intent(view.getContext(), UpdateReminderActivity.class); intent.putExtra("reminderId", remindId); intent.putExtra("title", reminder.getReminderTitle()); intent.putExtra("definition", reminder.getReminderDefinition()); view.getContext().startActivity(intent); return true; } });
Advertisement
Answer
For updating a pending intent, you need to use FlAG_UPDATE_CURRENT
with the same id
you used earlier to set the initial intent.
If you want to update the alarm associated with that pending intent, you need to cancel the old alarm & then the old PendingIntent
. Then create a new PendingIntent
like this,
PendingIntent updateIntent = PendingIntent.getBroadcast(this, remindId, intent,PendingIntent.FLAG_UPDATE_CURRENT);
and set it to the alarm.