In my app it is important to display a notification at a certain point in time (as alarm for Accident detection if the user does not move anymore).
This alarm is working on all Android versions < 10 and is also working with the most Phones with android 10. One of the exceptions seams to be the Samsung S10 (Plus). With this Phone all notifications are for 1-2 minutes delayed if the Phone is in Sleep-Mode (Display is black)!
Here some code snippets to show the Problem.
First we tried to do it with an JobIntentService:
public class MyNotificationService extends JobIntentService {
....
@Override
protected void onHandleWork(@NonNull Intent intent) {
NotificationType notificationType = NotificationType.valueOf(intent.getExtras().getInt(INTENT_EXTRA_NOTIFICATION_TYPE));
boolean alarmingNotification = intent.getBooleanExtra(INTENT_EXTRA_NOTIFICATION_TYPE_ALARM, false);
NotificationStorage notificationStorage = NotificationStorage.getInstance(context);
notificationStorage.setPendingNotificationType(notificationType);
intent.putExtra(INTENT_EXTRA_ALARM_NOTIFICATION, alarmingNotification);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setClass(context, PopupOnLockScreenActivity.class);
startActivity(notificationStarterIntent)
}
...
}
Result: Popup does not open immediately as expected (1-2 minutes delayed)
Then i tried as workaround with Notification with a NotificationChannel (IMPORTANCE_HIGH) and NotificationCompat.Builder (PRIORITY_MAX):
public class MyNotificationService extends JobIntentService {
....
@Override
protected void onHandleWork(@NonNull Intent intent) {
NotificationType notificationType = NotificationType.valueOf(intent.getExtras().getInt(INTENT_EXTRA_NOTIFICATION_TYPE));
boolean alarmingNotification = intent.getBooleanExtra(INTENT_EXTRA_NOTIFICATION_TYPE_ALARM, false);
NotificationStorage notificationStorage = NotificationStorage.getInstance(context);
notificationStorage.setPendingNotificationType(notificationType);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d(TAG, "showPopupOnLockScreen XXX");
String channelName = getResources().getString(R.string.safety_notification_channel_name);
//String description = getString(R.string.channel_description);
String description = "XXXXX";
int importance = NotificationManager.IMPORTANCE_HIGH;
android.app.NotificationChannel channel = new android.app.NotificationChannel(UepaaNetAndroidConstants.ANDROID_NOTIFICATION_CHANNEL_ID, channelName, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
android.app.NotificationManager notificationManager = getSystemService(android.app.NotificationManager.class);
notificationManager.createNotificationChannel(channel);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel.getId())
.setSmallIcon(R.drawable.notification_symptom_small)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
notificationManager.notify(123, builder.build());
}
}
...
}
Result: Notification will be not showed immediately as expected (1-2 minutes delayed)
I also tried to put the app in the lists to prevent the app from Battery-Manager and Sleep-Modes. (https://www.youtube.com/watch?v=npGw_r-v25k) But the problem still exists!
Can somebody help me? Our app is useless without notifications.