Android Oreo上的NotificationManagerCompat

Android Oreo上的NotificationManagerCompat,android,android-notifications,Android,Android Notifications,当使用NotificationManagerCompat和NotificationCompat时,有没有办法在Android Oreo上设置频道?由于NotificationManagerCompat只是一个包装类,可以让生活更轻松,因此您可以正常创建频道: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val name = getString(R.string.channel_title) val descri

当使用
NotificationManagerCompat
NotificationCompat
时,有没有办法在Android Oreo上设置频道?

由于
NotificationManagerCompat
只是一个包装类,可以让生活更轻松,因此您可以正常创建频道:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val name = getString(R.string.channel_title)
    val description = getString(R.string.channel_description)
    val importance = NotificationManager.IMPORTANCE_HIGH
    val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
    mChannel.description = description
    mChannel.enableLights(true)
    mChannel.lightColor = Color.parseColor("#5B3C88")
    mChannel.enableVibration(true)
    mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
    val manager = (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
    manager.createNotificationChannel(mChannel)
}
然后在发布通知时使用
通知管理器compat
,但不要忘记使用新构造函数构造通知:

NotificationCompat.Builder(context, CHANNEL_ID)

我通常使用此类来管理通知通道:

class NotificationManager(private val context: Context) {

    companion object {
        private val CHANNEL_ID = "YOUR_CHANNEL_ID"
        private val CHANNEL_NAME = "Your human readable notification channel name"
        private val CHANNEL_DESCRIPTION = "description"
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun getMainNotificationId(): String {
        return CHANNEL_ID
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun createMainNotificationChannel() {
            val id = CHANNEL_ID
            val name = CHANNEL_NAME
            val description = CHANNEL_DESCRIPTION
            val importance = android.app.NotificationManager.IMPORTANCE_LOW
            val mChannel = NotificationChannel(id, name, importance)
            mChannel.description = description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
            mNotificationManager.createNotificationChannel(mChannel)
    }
}
然后可以像这样使用util

fun createNotificationCompatBuilder(context: Context): NotificationCompat.Builder {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return NotificationCompat.Builder(context, NotificationManager(context).mainNotificationId)
    } else {
        return NotificationCompat.Builder(context)
    }
}
通过这种方式,您可以在应用程序的任何地方使用它,并带有签名,就像您以前使用的一样,并且您可以在将来发生更改时轻松地对其进行更改

建议将NotificationManagerCompat与AndroidX一起使用。
NotificationManagerCompat
支持通知通道。新版本在
NotificationManagerCompat
中添加了通知通道方法,因此开发人员在处理通知时只能使用
NotificationManagerCompat

对于Java,请在build.gradle文件中包含以下内容

实现“androidx.core:core:1.2.0”

对于Kotlin,在build.gradle文件中包括以下内容,而不是上述依赖项

实现“androidx.core:core ktx:1.2.0”

要显示通知,您必须执行以下操作
  • 创建并注册通知通道
  • 创建一个通知
  • 显示通知
  • 下面的代码段位于Kotlin的中,但如果需要,也可以使用Java

    1.创建并注册通知通道。 通知通道为类似类型的通知提供通用的视觉和听觉体验。由于它们是在API 26中引入的,所以现在需要为通知设置一个通道,否则它们将不会显示在较新版本的Android上

    因此,定义一个助手方法,如下所示,为您创建一个通知通道

    //define your channel id
    val CHANNEL_ID = "com.yourpackagename.your_channel_id"
    
    //create notification channel for android Oreo and above devices.
    if (Build.VERSION.SDK_INT >= 26) {
        val channel = NotificationChannel(CHANNEL_ID , "Your channel name", NotificationManager.IMPORTANCE_DEFAULT)
        NotificationManagerCompat.from(this).createNotificationChannel(channel)
    }
    
    2.创建一个通知。 使用
    NotificationCompat.Builder
    创建
    notification
    。请注意,频道ID已传递给构建器

    var builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Much longer text that cannot fit one line...")
        .setStyle(NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    
    3.显示通知 要显示通知,请调用
    NotificationManagerCompat.notify()
    ,为其传递通知的唯一ID和
    NotificationCompat.Builder.build()的结果


    仅此而已:)

    仅显示通知与普通NotificationManager有什么区别?@Florian Walther它使通知与旧版本兼容。例如,正在忽略通道id。每次我们想创建一个通知来传递channelid或notNow时,用androidx.appcompat:appcompat:1.1.0-rc01检查NotificationManagerCompat.createNotificationChannel()
    。非常整洁。你知道吗,顺便问一下,如何让这个通知也出现在Android Auto上?鉴于AndroidCompat被androidx取代,Darish给出了最现代的答案;我可能希望razvan cristian lung@praful bhatnagar仍然可以更新他们的答案,以反映/指出这次迁移?这个答案为我省去了很多麻烦。谢谢你,亲爱的!也许我遗漏了什么,但是该方法需要一个android.app.NotificationChannel列表,它只存在于API 26上,所以如何创建通道来通过它而不检查API 26…?@androidguy在创建NotificationChannel时,您仍然需要检查API 26。通道对象准备好后,您可以使用NotificationManagerCompat向系统注册它。非常整洁。你知道吗,顺便问一下,如何使此通知也出现在Android Auto上?@Josh抱歉,没有机会在Android Auto上测试通知,因此无法在此提供建议
       NotificationManagerCompat.from(this).notify(notificationId, builder.build())