Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Heads-up notification disappears after a few seconds

I would that the notification does not disappear after a few seconds. So i have create the notification like this:

  NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.cast_ic_notification_small_icon)
                .setDefaults(Notification.FLAG_ONGOING_EVENT)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentTitle(notificationDetails.getSubject())
                .setContentText(notificationDetails.getMessage())
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setOngoing(true);

and setting the FLAG_ONGOING_EVENT and the method setOngoing(true).

but after a few seconds the notification continues to disappears. I wish the notification to disappear only when the user clicks on. Thank you.

like image 257
javierZanetti Avatar asked Dec 08 '22 19:12

javierZanetti


2 Answers

It is actually possible to make a heads-up notification persistent. The trick is to use setFullScreenIntent. If you don't want your notification to have a full-screen version, you can use a dummy intent that won't actually launch any activity, like this:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
notificationBuilder.setFullScreenIntent(pendingIntent, true);

It's a hack, but the behavior makes some sense. If an app is trying to show a full-screen notification, then it must be an important event, like an alarm or a phone call. If the phone decides not to show the full-screen notification, it should probably still show something persistent until the user takes action.

This works on the phones I've tested, but the behavior isn't documented, so there are no guarantees.

like image 112
Daniel Lubarov Avatar answered Dec 10 '22 13:12

Daniel Lubarov


This issue is observed on Honor and Huawei devices. You can try to fix it by using setFullScreenIntent and adding permissions to AndroidManifest.

Code:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setFullScreenIntent(pIntent, true);

AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
like image 26
PMV Avatar answered Dec 10 '22 13:12

PMV