android - 在 Android 中,如何在锁定屏幕中显示通知?
问题描述
我已经去了:
- 设置
- 通知和状态栏
- 程序通知
- [应用]
- 启用锁屏通知
但是,我在我的 Android 手机(小米红米 5A、牛轧糖)的锁定屏幕中没有看到通知。顺便说一句,我在本地通知。不涉及推送通知或 GCM。
我测试了 Gmail 应用程序,它运行良好。Gmail 通知显示在锁定屏幕中。
我是否错误地构建了通知?那我应该怎么建?
private fun showNotificationWith(message: String) {
val channelId = "com.example.notif.channelId"
val channelName = "App status"
val contentTitle = "Title"
val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_small_exclamation)
.setContentTitle(contentTitle)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(channelId, channelName, importance)
notificationBuilder.setChannelId(channelName)
notificationManager.createNotificationChannel(notificationChannel)
notificationManager.notify(message.hashCode(), notificationBuilder.build())
} else {
notificationManager.notify(message.hashCode(), notificationBuilder.build())
}
}
解决方案
尝试这个...
您可以使用lockscreenVisibility
NotificationChannel.ie 的属性,
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
例如,
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(channelId, channelName, importance)
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationBuilder.setChannelId(channelName)
notificationManager.createNotificationChannel(notificationChannel)
notificationManager.notify(message.hashCode(),
notificationBuilder.build())
} else {
notificationManager.notify(message.hashCode(), notificationBuilder.build())
}
推荐阅读
- c# - Excel 单元格始终为空
- c# - Polly 断路器处理和未处理的异常
- typescript - Svelte 无法识别编译器选项“预处理”
- c# - Microsoft Identity returnURL 不适用于直接链接
- sequelize.js - 为什么 [Op.and] 在这种情况下使用 Sequelize 不起作用?
- python - Python程序只工作几秒钟就崩溃了
- javascript - 由闭包编译器编译的带有 \n 和空格的模板文字/字符串
- node.js - 使用 puppeteer 和 express js 发送自动 whatsapp 消息
- c# - 是否可以从 C# 运行 PowerShell shell 并使其保持运行,同时(同步)在其中执行多个 PowerShell 脚本?
- java - IN , AND 休眠中的查询