Android Oreo上的NotificationManagerCompat

Raz*_*ung 13 android android-notifications

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

Raz*_*ung 16

由于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)
}
Run Code Online (Sandbox Code Playgroud)

然后使用NotificationManagerCompat发布通知的时间,但不要忘记使用新的构造函数构造通知:

NotificationCompat.Builder(context, CHANNEL_ID)
Run Code Online (Sandbox Code Playgroud)


Dar*_*ish 10

将 NotificationManagerCompat 与 AndroidX 结合使用是推荐的方式。

NotificationManagerCompat 现在支持通知渠道。新版本添加了通知渠道方法,NotificationManagerCompat以便开发人员仅NotificationManagerCompat在处理通知时才能使用。

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

implementation 'androidx.core:core:1.2.0'

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

implementation 'androidx.core:core-ktx:1.2.0'

要显示通知,您必须执行以下操作

  1. 创建并注册通知通道。
  2. 创建通知。
  3. 显示通知

下面的代码片段在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)
}
Run Code Online (Sandbox Code Playgroud)

2. 创建通知。

使用NotificationCompat.Builder来创建一个Notificaiton. 请注意CHANNEL_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)
Run Code Online (Sandbox Code Playgroud)

3.显示通知

要使通知出现,请调用NotificationManagerCompat.notify(),将通知的唯一 ID 和结果传递给它NotificationCompat.Builder.build()

   NotificationManagerCompat.from(this).notify(notificationId, builder.build())
Run Code Online (Sandbox Code Playgroud)

就这样 :)


A. *_*huk 6

我通常使用这个类来管理通知渠道:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用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)
    }
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以在应用程序的任何位置使用它,就像您之前使用的一样,您可以在将来更改时轻松更改它.