gpt4 book ai didi

android - Unresolved 引用 createNotificationChannel()

转载 作者:行者123 更新时间:2023-11-30 00:06:03 26 4
gpt4 key购买 nike

我尝试创建一个“O”通知机制。所以我遵循了 Android 文档的解释。

我首先在我的 gradle 文件中添加了正确的库:

dependencies {
implementation "com.android.support:support-compat:27.0.2"
}

然后我写了下面的代码:

private var notification: NotificationCompat? = null

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
val name = getString(R.string.channel_name)
val description = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(PrinterApplication.CHANNEL_ID, name, importance)
channel.description = description
// Register the channel with the system
val notificationManager = NotificationManagerCompat.from(this)
notificationManager.createNotificationChannel(channel)
notification = NotificationCompat.Builder(this, channel.id)
.setSmallIcon(R.drawable.print)
.setContentTitle(resources.getString(R.string.app_name))
.setContentText(resources.getString(R.string.print_service_running))
.build()
} else {
...
}

因此 AS3 报告了以下问题:

Unresolved reference: createNotificationChannel(channel)
Type Mismatch: required: NotificationCompat? found: Notification!

请问是什么问题?

最佳答案

我能够摆脱 Unresolved reference: createNotificationChannel

通过像这样声明 notificationManager:

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) 作为 NotificationManager

build() 返回一个 Notification 对象,而不是 NotificationCompat。

Context.getSystemService

build

关于android - Unresolved 引用 createNotificationChannel(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48984785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com