【问题标题】:Notification Duration for HeadsUp提醒的通知持续时间
【发布时间】:2018-07-29 10:41:13
【问题描述】:

是否可以将提示通知的持续时间设置为无限制?现在它只显示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();

在此线程上尝试了相同的操作:Controlling Android Notification Duration for HeadsUp,但对我没有帮助。

有趣的事实: 在我的开发者手机上,三星 S5 mini -> 显示没有时间限制 在另一部开发者手机上,三星 S7 -> 显示 5 秒

【问题讨论】:

    标签: java android notifications heads-up-notifications


    【解决方案1】:

    尝试该解决方案(您已发布为链接的那个..),但在进行更改后如果通知 ID 未更改,请不要忘记更改 NOTIFICATION_ID通知的一些先前属性保留 .. 所以

    更改通知生成器的任何属性时更改通知ID

    【讨论】:

    • 同样的反应。 Headsup 通知仅显示 5 秒。
    【解决方案2】:

    这应该可行。我为通知类别添加了额外的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

    【讨论】:

    • 这对我帮助很大。点赞和感谢是我的责任。
    • 在 android 10 设备上不适合我,5 秒后,提示通知消失了。
    • @mdroid 有安卓 10 的解决方案吗?
    • @Vikrant_Dev,不,我还没有得到解决方案。
    • 为 Android 10 及更高版本的持续通知添加了更新。
    【解决方案3】:

    我找到了这个页面,它已经工作了

    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()
    

    【讨论】:

    猜你喜欢
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多