显示后通知消失

khu*_*rav 12 notifications android

我们的应用程序中的代码类似于以下内容

    val pendingIntent = PendingIntent.getActivity(ctx, id.toInt(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
    val builder = NotificationCompat.Builder(ctx, Channel.TEST_CHANNEL.channelId)
    builder.setTicker(tickerText)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setVibrate(vibrate)
            .setSmallIcon(icon)
            .setAutoCancel(true)
            .setLights(-0xff0100, 300, 1000)
            .setSound(uri)
            .setContentIntent(pendingIntent)
            .setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
            .addAction(R.drawable.ic_notification, ctx.getString(R.string.notification), piAction)

    val notification = builder.build()
    val nf = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    nf.notify(NOTIFICATION_TAG, id.toInt(), notification)
}
Run Code Online (Sandbox Code Playgroud)

从最近开始,我们注意到某些运行Android 8+的设备上的通知在显示后开始消失,没有用户的交互.设置auto-cancel为有false帮助,但用户体验降低.

id是数据库中的唯一项ID.这可能是需要注意的重要事项 - 从技术上讲,我们可以id通过用户显示,删除/取消通知,以及稍后再次使用具有相同ID的类似通知的时间.这可能是原因吗?

jit*_*555 -1

我发现唯一不确定的是NotificationCompat.Builder

Android oreo 现在使用Notification.Builder而不是NotificationCompat.Builder

可能你必须检查 Android 版本,例如:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         //Use Notification.Builder         
         } else {
         // Use NotificationCompat.Builder.
         }
Run Code Online (Sandbox Code Playgroud)

我不认为唯一的 ID 会成为消失通知的问题。

Google 已针对此新更改创建了开源示例。请参阅它以获取更多信息。

https://github.com/googlesamples/android-NotificationChannels

  • “Android oreo 现在使用Notification.Builder 而不是NotificationCompat.Builder。”是什么意思?一直都有框架版本和支持版本。支持版本仅存在,因此您不必编写这些 if-else 废话。 (2认同)