未解析的引用createNotificationChannel()

时间:2018-02-26 09:05:32

标签: android android-notifications

我尝试创建一个'O'通知机制。所以我按照Android doc。

的说明进行了操作

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

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!

请问有什么问题?

1 个答案:

答案 0 :(得分:0)

我能够摆脱Unresolved reference: createNotificationChannel

通过声明notificationManager这样:

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

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

Context.getSystemService

build