I'm making a reminder like app where a user can register and create tasks and set a reminder for each task. The tasks are stored in the firebase realtime database. I have an activity "TaskEditActivity" where a reminder can be set for a task, the user enters a date and I want to show a notification on that specific time/date.
So far I was able to schedule the notification and actually showing it with a BroadcastReceiver (AlarmReceiver.java).
I would like the scheduled notifications to show up after the device was rebooted, but i don't know how. I saw other posts using JobIntentService and a BroadcastReceiver listening for BOOT_COMPLETED but I don't know how I can make the notification show the information of the specific task information like title and description, since I can't pass an Intent when the device reboots and reschedule my notifications with the correct information. Additionally I would like the user to be able to cancel the notification from the "TaskEditActivity" via a button, which didn't work using the cancel(notificationId) method from NotificationManager.
A code snippet, example or tutorial would really be appreciated since this app will be graded and the deadline is approaching quite rapidly and I have been looking for a solution for hours now.
Here is the relevant part of my TaskEditActivity.java
private void scheduleNotification() {
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("notification", getNotification());
intent.putExtra("id", task.getNotificationId());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (reminderCalendar.getTimeInMillis() - System.currentTimeMillis()), pendingIntent);
}
private Notification getNotification() {
Intent i = new Intent(this, TaskEditActivity.class);
i.putExtra("isNew", false);
i.putExtra("listKey", listKey);
i.putExtra("task", task);
PendingIntent tapIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NotificationUtil.getInstance().CHANNEL_ID)
.setSmallIcon(R.drawable.ic_check)
.setContentTitle(task.getTitle())
.setContentText(task.getDescription())
.setContentIntent(tapIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_REMINDER);
return mBuilder.build();
}
Here is my AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra("notification");
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(intent.getIntExtra("id", 0), notification);
}
}