Android 推送在几秒后关闭

Android push dismises after few seconds

我正在尝试创建来电推送通知。当呼叫事件发生时,带有通知的前台服务启动。我为它创建了一个频道和通知。这是代码:

频道设置:

    private fun createCallChannelChannel(): NotificationChannel {
    val attributes = AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
        .build()
    val importance = NotificationManager.IMPORTANCE_HIGH
    return NotificationChannel(CALL_CHANNEL_ID, CALL_CHANNEL_NAME, importance).apply {
        description = CALL_CHANNEL_DESCRIPTION
        setSound(
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE),
            attributes
        )
        enableLights(true)
        enableVibration(true)
    }
}

通知设置:

    private fun buildIncomingCallNotification(payload: VoIpCallResponse): Notification {

    val remoteView = RemoteViews(packageName, R.layout.notification_call_view)
    remoteView.setOnClickPendingIntent(R.id.declineBtn, getDeclinePendingIntent())
    remoteView.setOnClickPendingIntent(R.id.acceptBtn, getAcceptPendingIntent(payload))
    return NotificationCompat.Builder(this, CALL_CHANNEL_ID)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setCategory(NotificationCompat.CATEGORY_CALL)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(remoteView)
        .setAutoCancel(false)
        .build()
}

有效。正在显示通知。但问题是通知在几秒钟后最小化到通知栏。目标是防止通知最小化,直到用户拒绝/接受呼叫或结束呼叫事件发生。例如 WhatsApp。来电通知会无限期地停留在屏幕顶部。我怎样才能做同样的事情?我频道的重要性是NotificationManager.IMPORTANCE_HIGH,通知优先级是NotificationCompat.PRIORITY_MAX

我找到了这个页面并且有效

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