Kotlin Android - 通知未作为悬浮通知出现

3

我的设备正在运行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精英版,因为无论你如何努力,它都不会工作。


你在最新版本上测试过它了吗?比如安卓8或以上版本? - ADM
@ADM Heads up通知在5.0中被添加了...我的设备也是华为,所以可能他们没有这个功能。无论如何,我买不起新手机,而且我的电脑几乎无法运行模拟器... - thebluepandabear
@TomJoney 你试试在模拟器上运行它怎么样? - Anatolii
@Anatolii 我会尝试这个。 - thebluepandabear
@Anatolii在模拟器中测试了一下,但还是不行:(。希望能找到解决方案... - thebluepandabear
1个回答

1

太棒了。你刚刚实现了这个问题的代码吗?这是一项惊人的工作。非常感谢! - user6043117
1
我之前曾经处理过这个功能,所以想分享一下 :) - Kavita Patil
明天我会查看这个。如果我能给你一枚金牌,我会的;因为你是超级荣誉的人。我由衷地尊重你……谢谢还不足以表达我的感激之情。 - thebluepandabear
@Kavita_p 它在模拟器上可以运行,但是在我的手机上似乎无法工作,在我的手机上它只显示为普通的一个。这很可能是我的手机出了问题... 无论如何, 谢谢. - thebluepandabear
嗨@TomJoney,我刚刚发现了与heads-up通知相关的https://github.com/SimenCodes/heads-up代码,他提到了一些“常见问题”。您可以检查一下这是否适用于您的手机。 - Kavita Patil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接