I'm developing an app where users can create routines and set reminders for these routines.
I use alarms scheduled by AlarmManager and a BroadcastReceiver to call a foregroud service that sends a notification by using the notification manager. Here's some code:
// Mark as completed
Intent markAsCompletedIntent = new Intent(this, RoutincoBroadcastReceiver.class);
markAsCompletedIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_ID, routineModelId);
markAsCompletedIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_REMINDER_ID, routineReminderModelId);
markAsCompletedIntent.setAction(RoutincoBroadcastReceiver.ACTION_BROADCAST_MARK_AS_COMPLETED);
PendingIntent markAsCompletedPendingIntent = PendingIntent.getBroadcast(this, routineReminderModelId, markAsCompletedIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Dismiss the notification
Intent closeIntent = new Intent(this, RoutincoBroadcastReceiver.class);
closeIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_ID, routineModelId);
closeIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_REMINDER_ID, routineReminderModelId);
closeIntent.setAction(RoutincoBroadcastReceiver.ACTION_BROADCAST_CLOSE);
PendingIntent closePendingIntent = PendingIntent.getBroadcast(this, routineReminderModelId, closeIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder;
if (!routineModel.Description.equals("")) {
builder = new NotificationCompat.Builder(this, ChannelID)
.setSmallIcon(R.drawable.ic_appiconnotifications)
.setContentTitle(routineModel.Caption)
.setContentText(routineModel.Description)
.addAction(0, "MARK AS COMPLETED", markAsCompletedPendingIntent)
.addAction(0, "DISMISS", closePendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setAutoCancel(true);
} else {
builder = new NotificationCompat.Builder(this, ChannelID)
.setSmallIcon(R.drawable.ic_appiconnotifications)
.setContentTitle(routineModel.Caption)
.addAction(0, "MARK AS COMPLETED", markAsCompletedPendingIntent)
.addAction(0, "DISMISS", closePendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setAutoCancel(true);
}
Intent openIntent = new Intent(this, RoutineReminderActivity.class);
openIntent.putExtra(RoutineReminderActivity.RRA_ROUTINE_ID, routineModelId);
openIntent.putExtra(RoutineReminderActivity.RRA_ROUTINE_REMINDER_ID, routineReminderModelId);
openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
PendingIntent openActivityIntent = PendingIntent.getActivity(this, routineReminderModelId, openIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(openActivityIntent);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(routineReminderModelId, notification);
On some devices while this notification exists in the notification drawer the phone is acting very laggy. It takes a very long time to activate the phone screen, to view the notifaction drawer, to unlock the phone. On some phones it works perfectly fine.
The phone that lags has Android 30 on it. If the notification is dismissed the phone starts working normally again.
Any thoughts?