Alex Finocchiaro
Alex Finocchiaro

Reputation: 298

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.

Upvotes: 6

Views: 6191

Answers (7)

Goran
Goran

Reputation: 96

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.

Upvotes: 2

PMV
PMV

Reputation: 81

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" />

Upvotes: 8

Midhun Rs
Midhun Rs

Reputation: 11

.setFullScreenIntent(fullScreenPendingIntent, true)

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

Upvotes: 0

Adam Kis
Adam Kis

Reputation: 1522

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.

Upvotes: 0

Daniel Lubarov
Daniel Lubarov

Reputation: 7924

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.

Upvotes: 11

Salvador Hernandez
Salvador Hernandez

Reputation: 309

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

Upvotes: 1

santosh kumar
santosh kumar

Reputation: 2962

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.

Upvotes: 3

Related Questions