HeadsUp通知的持续时间

5

是否可以将heads-up通知的持续时间设置为无限制?目前只会显示5秒钟。已尝试更改类别等不同方法,但持续时间始终为5秒。

以下是我的代码:

Notification notification =
            notificationBuilder
                    .setCategory(Notification.CATEGORY_CALL)
                    .setContentText("TKSE Werk Duisburg")
                    .setSmallIcon(R.drawable.ic_tk_individual_signet_logo)
                    .setOngoing(true)
                    .setAutoCancel(false)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setContentIntent(contentIntent)
                    .setCustomHeadsUpContentView(viewNotificationHeadsUp)                                                      .setCustomContentView(viewNotificationSmall)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setVibrate(new long[20]).build();

我尝试了与此帖子相同的方法:控制 HeadsUp 的 Android 通知持续时间,但没有帮助我。

有趣的事实是: 在我的开发者手机 Samsung S5 mini 上 -> 它显示没有时间限制 在另一个开发者手机 Samsung S7 上 -> 它显示 5 秒钟

3个回答

8

这应该可以工作。我为通知类别添加了额外的onGoing(true)category call

   NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);

        // Configure the notification channel.
        notificationChannel.setDescription("Channel description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    // assuming your main activity
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setCategory(Notification.CATEGORY_CALL)
            .setOngoing(true)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("Hearty365")
            .setPriority(Notification.PRIORITY_MAX)
            .setContentTitle("Default notification")
            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            .setFullScreenIntent(pendingIntent,true)
            .setContentInfo("Info");

    notificationManager.notify(/*notification id*/1, notificationBuilder.build());

PS. 导入 android.app.Notification; 用于设置通知类别(呼叫)

更新 Android 10

您需要在进行中的通知中添加优先级和类别。

val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
    fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)

val notificationBuilder =
        NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Incoming call")
    .setContentText("(919) 555-1234")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setCategory(NotificationCompat.CATEGORY_CALL)

    // Use a full-screen intent only for the highest-priority alerts where you
    // have an associated activity that you would like to launch after the user
    // interacts with the notification. Also, if your app targets Android 10
    // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
    // order for the platform to invoke this notification.
    .setFullScreenIntent(fullScreenPendingIntent, true)

val incomingCallNotification = notificationBuilder.build()

Source


这真的帮了我很多。点赞和感谢是我的责任。 - Rushikant Pawar
6
在我的 Android 10 设备上无法工作,在 5 秒后,悬浮通知就消失了。 - mdroid
@mdroid,你有Android 10的解决方案吗? - Vikrant_Dev
@Vikrant_Dev,不好意思,我还没有得到解决方案。 - mdroid
在Android 10及更高版本上添加了持续通知的更新。 - Aks4125

1

我找到了这个页面并且它起作用了

https://developer.android.com/training/notify-user/time-sensitive

    val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
    fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)

val notificationBuilder =
        NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Incoming call")
    .setContentText("(919) 555-1234")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setCategory(NotificationCompat.CATEGORY_CALL)



// Use a full-screen intent only for the highest-priority alerts where you
    // have an associated activity that you would like to launch after the user
    // interacts with the notification. Also, if your app targets Android 10
    // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
    // order for the platform to invoke this notification.
    .setFullScreenIntent(fullScreenPendingIntent, true)

当然要与foregroundService一起使用

val incomingCallNotification = notificationBuilder.build()

1
欢迎提供解决方案的链接,但请确保您的答案即使没有链接也是有用的:在链接周围添加上下文,以便其他用户知道它是什么以及为什么存在,然后引用您链接的页面中最相关的部分,以防目标页面不可用。仅仅是一个链接的答案可能会被删除。 - user12867493

0
尝试使用您发布的链接中提到的那个解决方案,但在更改后不要忘记更改 NOTIFICATION_ID 。如果通知ID未更改,则会保留一些以前的通知属性,因此请在更改通知构建器的任何属性时更改通知ID。

1
相同的反应。Headsup通知只会显示5秒钟。 - Serdar Nur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接