我的设备正在运行Android 5.1。
您好,
我为一个应用程序创建了一种通知类,其中包含方法等,使得创建通知变得容易。
我尝试将其配置为使用“悬浮通知”,但它并没有起作用,它只是像普通的通知一样出现:也许我做错了些什么?
通知助手类和接口:
interface NotificationHelper {
fun startNotification(title: String, text: String, priority: Int, smallIcon: Int)
}
class DefaultNotificationHelper(private val context: Context) : NotificationHelper {
private val channelID = "channel_01"
private fun registerNotificationManagerWithSystem(channel: NotificationChannel) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
}
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
val notificationChannelInstance = NotificationChannel(channelID, "Channel 1", NotificationManager.IMPORTANCE_HIGH).let {
it.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
it.setShowBadge(true)
registerNotificationManagerWithSystem(it)
}
}
}
private fun createNotificationInstance(title: String, text: String, priority: Int, smallIcon: Int): NotificationCompat.Builder {
return NotificationCompat.Builder(context, channelID)
.setContentTitle(title)
.setContentText(text)
.setPriority(priority)
.setDefaults(DEFAULT_VIBRATE)
.setDefaults(DEFAULT_SOUND)
.setSmallIcon(smallIcon)
}
private fun notify(notificationCompat: NotificationCompat.Builder) {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(1, notificationCompat.build())
}
override fun startNotification(title: String, text: String, priority: Int, smallIcon: Int) {
createNotificationChannel()
notify(createNotificationInstance(title, text, priority, smallIcon))
}
}
实现:
val notificationHelper = DefaultNotificationHelper(this)
notificationHelper.startNotification("Hello", "Hello", NotificationCompat.PRIORITY_HIGH, R.drawable.fire_gradient)
非常感谢您的帮助——可能与我的手机有关。
祝好,
汤姆·琼尼
编辑:我已经确定这只是我的手机的问题,无论我怎么努力,它都不会工作。如果你想测试悬浮通知,请不要购买华为Y6精英版,因为无论你如何努力,它都不会工作。