Android Heads-up notification disappears after a few seconds
Asked Answered
C

7

6

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.

Chara answered 13/1, 2017 at 17:4 Comment(1)
AFAIK, that is not possible.Bulge
A
3

Duration of heads-up notification can't be changed It is set at OS level Depends on OS how much time it provides for it.

Annates answered 13/1, 2017 at 17:9 Comment(1)
How does the built in Android alarm clock work? On my old Android 8.0 when the alarm rings the heads-up notification pops up with two action buttons (Snooze, Dismiss) and it stays there as long as it takes for me to take action.Synesthesia
T
11

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.

Tonga answered 4/8, 2017 at 22:0 Comment(0)
T
8

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" />
Testerman answered 14/4, 2021 at 0:27 Comment(0)
A
3

Duration of heads-up notification can't be changed It is set at OS level Depends on OS how much time it provides for it.

Annates answered 13/1, 2017 at 17:9 Comment(1)
How does the built in Android alarm clock work? On my old Android 8.0 when the alarm rings the heads-up notification pops up with two action buttons (Snooze, Dismiss) and it stays there as long as it takes for me to take action.Synesthesia
U
2

Apart from the same issue with foreground heads-up notifications disappearing after a few seconds, an additional issue I had was that the action buttons were collapsed.

Among other things, what helped me was:

builder.setCategory(NotificationCompat.CATEGORY_CALL);

After adding this line of code, the notification didn't disappeared after a few seconds and the action buttons weren't collapsed.

Ulysses answered 30/9, 2022 at 7:45 Comment(0)
I
1

The duration cannot be changed. Otherwise it would intrude with other heads-up notifications that are in the queue to be displayed.

Intensifier answered 13/1, 2017 at 17:10 Comment(0)
C
0

You can do it with:

notificationBuilder.setFullScreenIntent(pendingIntent, true)

You also have to use these:

val tempChannel = NotificationChannel(tempChannelId, "temp channel",
    NotificationManager.IMPORTANCE_HIGH) // Setting to high is important
tempChannel.enableVibration(true)
...
notificationBuilder.setAutoCancel(false)
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX) // PRIORITY_HIGH works too
notificationBuilder.setVibrate(LongArray(0)) // For older devices we need to add vibration or sound for the Heads-Up notification. This line will not make it vibrate, you can use another pattern, or default, if you want it to vibrate
notificationBuilder.setFullScreenIntent(pendingIntent, true)

IMPORTANT:

On most of the devices, this will show the notification on the top of the screen, overlapping everything, and staying there.

BUT ON SOME DEVICE THIS WILL IMMEDIATELY LAUNCH THE PENDING INTENT THAT IS GIVEN TO THE NOTIFICATION. (Eg. Huawei)

TO SOLVE THIS we can use a dummy pendingIntent for the fullScreenIntent, that shows the notification the same way, just without the notificationBuilder.setFullScreenIntent(pendingIntent, true); This will work as a fallback, so the notification will appear, but will shrink itself to the status bar after like 5 seconds.

val servicePendingIntent = PendingIntent.getService(context, 0, Intent(context, FullScreenIntentService::class.java), 0)
val mainActivityPendingIntent = PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), PendingIntent.FLAG_ONE_SHOT)
...
notificationBuilder.setContentIntent(mainActivityPendingIntent)
notificationBuilder.setFullScreenIntent(servicePendingIntent, true)

Where:

class FullScreenIntentService : Service() {

    override fun onCreate() {
        super.onCreate()
        showNotificationHereTheSameWayJustWithoutSetFullScreenIntent()
        stopSelf()
    }

    override fun onBind(intent: Intent?): IBinder? = null
}

And don't forget to register the service in AndroidManifest.

Cypripedium answered 4/5, 2020 at 13:49 Comment(0)
P
0
.setFullScreenIntent(fullScreenPendingIntent, true)

For heads up notification to stay on, you need add a full screen intent

Periosteum answered 12/11, 2020 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.