我使用Notification
创建一个NotificationCompat.Builder
。该通知具有一个操作按钮:
notification = notificationBuilder
.setContentTitle(getString(R.string.blabla)).
.setContentText("")
.setSmallIcon(R.drawable.blabla)
.setContentIntent(pendingIntentOpenApp)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Cancel", pendingIntentAction)
.build();
显示通知,但重复操作三遍:
我无法弄清楚自己在做错什么,因此感谢您的帮助。
答案 0 :(得分:0)
您可以使用以下代码创建通知:
var mBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Title")
.setContentText("Description")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_notification,"OK",pendingIntent)
.setChannelId(chanelId).build()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val mChanel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(mChanel)
notificationManager.notify(channelId, mBuilder)
}else{
notificationManager.notify(channelId, mBuilder)
}