如何制作折叠的紧凑型通知

时间:2019-06-13 08:29:02

标签: android android-notifications

我想显示低优先级的折叠通知,例如android: 坍塌的Collapsed default notification 并展开enter image description here

这是我的代码: 通知频道的创建:

private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = getString(R.string.name)
            val descriptionText = getString(R.string.description)
            val importance = NotificationManager.IMPORTANCE_MIN
            val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                    getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

和通知本身

createNotificationChannel()
val intent = Intent(this, MyActivity::class.java).apply {
  flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
          .setContentTitle(getString(R.string.notification_title))
          .setContentText(getString(R.string.notification_message))
          .setSmallIcon(R.drawable.ic_light_black_24dp)
          .setPriority(NotificationCompat.PRIORITY_LOW)
          .setContentIntent(pendingIntent)
          .build()

startForeground(SERVICE_NOTIFICATION_ID, notification)

0 个答案:

没有答案