问题
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.
回答1:
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.
回答2:
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.
回答3:
The duration cannot be changed. Otherwise it would intrude with other heads-up notifications that are in the queue to be displayed.
回答4:
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.
来源:https://stackoverflow.com/questions/41639846/android-heads-up-notification-disappears-after-a-few-seconds