Yuri Petrov
Yuri Petrov

Reputation: 193

How to make a notification in Android that does not collapse itself?

The notification is now shown and hidden in the status bar after 2 seconds. How do I make the notification not go to the status bar?

     val builder = NotificationCompat.Builder(context, "Over limit channel")
        .setContentTitle("")
        .setContentText("")
        .setGroupSummary(false)
        .setSmallIcon(R.mipmap.launcher_icon)
        .setContent(remoteViews)
        .setAutoCancel(false)
        .setGroup("false")
        .setGroupSummary(false)
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_MAX)

    val notificationManager = NotificationManagerCompat.from(context)
    notificationManager.notify(800, builder.build())

enter image description here

Upvotes: 2

Views: 1145

Answers (2)

Shay Kin
Shay Kin

Reputation: 2657

The trick I found is to use the method setFullScreenIntent(android.app.PendingIntent intent,boolean highPriority) with this method the notification will never be removed from the status bar except when you do it yourself.

here is an example:

 val intent = Intent()

 val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

 val builder = NotificationCompat.Builder(context, "Over limit channel")
        .setContentTitle("")
        .setContentText("")
        .setGroupSummary(false)
        .setSmallIcon(R.mipmap.launcher_icon)
        .setContent(remoteViews)
        .setAutoCancel(false)
        .setGroup("false")
        .setGroupSummary(false)
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setFullScreenIntent(pendingIntent,true)

From the android Studio documentation :

An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time. If this facility is used for something else, please give the user an option to turn it off and use a normal notification, as this can be extremely disruptive. On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.

Params: intent – The pending intent to launch. highPriority – Passing true will cause this notification to be sent even if other notifications are suppressed.

UPDATE

For some API version for this to work you must add this permission on the manifest

 <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"></uses-permission>

Upvotes: 4

SooryaSRajan
SooryaSRajan

Reputation: 64

You can start a foreground service with your required notification, or you can set the notification as an onGoing event to make it non dismissible

Upvotes: 0

Related Questions