【问题标题】:Android push dismises after few seconds几秒钟后Android推送消失
【发布时间】:2020-07-01 08:16:04
【问题描述】:

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

频道设置:

    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

【问题讨论】:

    标签: android foreground-service android-push-notification


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多