androidnotificationsbroadcastreceiveralarmforeground-service

Android notification makes phone lag


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?


Solution

  • You should check the specifications of the device that is laggy and monitor RAM and CPU usage of the device, before, after and during your app runtime, speatially the RAM usage, maybe Android 30 is putting too much pressure on device and that is causing the issue.

    In General the issue that are seen on some devices and not on another are caused ether by the difference between Android Versions and their resource handling methods or bottleneck between Hardware and Software on same OS version on different devices.

    You can monitor device resource usage ether by Android Studio's own resource monitor tool "Android Profiler" or third party Resource Monitoring Apps found on Google Play.

    If there is a process or function in your App that causing the resource leak you can fix it easily by detecting it from android profiler but if the issue is caused by OS Resource handling or Hardware and Software bottleneck you should skip that device.