带有操作的Android通知:按钮显示了三次

时间:2018-12-04 16:58:17

标签: android notifications

我使用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();

显示通知,但重复操作三遍:

enter image description here

我无法弄清楚自己在做错什么,因此感谢您的帮助。

1 个答案:

答案 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)
}