简体   繁体   English

SetTimeOutAfter 不适用于 android 通知

[英]SetTimeOutAfter Not working for android Notification

The setTimeOutAfter method for notification seems not t be working.用于通知的setTimeOutAfter方法似乎不起作用。 My application meets the requirement for minSdk but I do not know why the heads-up-notification dismises from the screen before time elapses.我的应用程序满足 minSdk 的要求,但我不知道为什么提示通知会在时间结束前从屏幕上消失。

@Singleton
class NotificationHelper @Inject constructor(
    @ApplicationContext private var context: Context,
) {

    companion object {
        private const val NOTIFICATION_CHANNEL_ID = "NOTIFICATION_CHANNEL"
        private const val NOTIFICATION_CHANNEL_NAME = "NOTIFICATION_CHANNEL_NAME"
        private const val BROADCAST_REQUEST_CODE = 0
        private const val NOTIFICATION_TIME_OUT = 30000L
    }

    fun getNotificationManager() =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    fun createNotification(
        title: String,
        subtitle: String = "",
    ): Notification {
        createNotificationChannel()
        return createNotificationBase(title, subtitle)
            .build()
    }

    fun <T> createNotification(
        title: String,
        actionTitle: String,
        buttonAction: String,
        broadcastReceiver: Class<T>,
        subtitle: String = ""
    ): Notification {
        createNotificationChannel()

        val intent = Intent(context, broadcastReceiver)
            .apply {
                action = buttonAction
            }

        val pendingIntent = pendingIntentWithBroadcast(intent)

        return createNotificationBase(title, subtitle)
            .setContentIntent(pendingIntent)
            .addAction(R.drawable.ic_amazon_blue_logo, actionTitle, pendingIntent)
            .build()
    }

    fun sendNotification(notification: Notification, notificationId: Int) {
        getNotificationManager().notify(notificationId, notification)
    }


    private fun pendingIntentWithBroadcast(intent: Intent): PendingIntent =
        PendingIntent.getBroadcast(
            context,
            BROADCAST_REQUEST_CODE,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )

    private fun createNotificationChannel() {
        val channel = NotificationChannel(
            NOTIFICATION_CHANNEL_ID,
            NOTIFICATION_CHANNEL_NAME,
            NotificationManager.IMPORTANCE_HIGH
        )
            .apply {
                lockscreenVisibility = Notification.VISIBILITY_PRIVATE
            }

        getNotificationManager().createNotificationChannel(channel)
    }

    private fun createNotificationBase(
        title: String,
        subtitle: String,
    ): NotificationCompat.Builder {
        return NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_alexa_notification)
            .setContentTitle(title)
            .setContentText(subtitle)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setTimeoutAfter(NOTIFICATION_TIME_OUT)
            .setOnlyAlertOnce(true)
        
    }


    fun cancelNotification(notificationId:Int) = getNotificationManager().cancel(notificationId)
}

How can I make this work?我怎样才能使这项工作?

do you want to keep heads-up-always-visible Notification present on screen for TimeOutAfter duration?您想在TimeOutAfter持续时间内在屏幕上保持提醒始终可见的Notification吗? thats not possible, duration of head-up layout visible on screen is configured in system and you won't override this那是不可能的,屏幕上可见的抬头布局持续时间是在系统中配置的,您不会覆盖它

setTimeOutAfter value will make Notification auto-dismiss after this period, but only in API26+ (when introduced). setTimeOutAfter值将使Notification在这段时间后自动关闭,但仅限于 API26+(引入时)。 There is no guarantee that AppCompat / AndroidX version will handle this method in older system versions, common approach is that compat library just won't call setTimeOutAfter under the hood on APIs below 26 protecting your app from NoSuchMethodException crash.无法保证AppCompat / AndroidX版本将在旧系统版本中处理此方法,常见的方法是兼容库不会在 26 以下的 API 上调用setTimeOutAfter以保护您的应用程序免受NoSuchMethodException崩溃。 still this isn't same as bringing this feature to older OS versions.这仍然与将此功能引入旧操作系统版本不同。 but you can do by yourself with eg但你可以自己做,例如

new Handler(Looper.getMainLooper()).postDelayed (() -> {
    getNotificationManager().cancel(notificationId);
}, NOTIFICATION_TIME_OUT);

placed just after getNotificationManager().notify(... call放在getNotificationManager().notify(...调用之后

I was able to achieve the desired result with adding setOngoing(true) to the notification builder.通过将setOngoing(true)添加到通知生成器,我能够获得预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM