我確實有這樣的想法,但不知道我該怎么做。你能幫助我嗎?
private fun sendNotification(){
val snoozeIntent = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS).apply {
putExtra(CHANNEL_ID, 0)
}
val snoozePendingIntent: PendingIntent =
PendingIntent.getBroadcast(this, 0, snoozeIntent, 0)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
...
.addAction(R.drawable.ic_launcher_background, "200 мл", snoozePendingIntent)
uj5u.com熱心網友回復:
我想你可能想打電話
.setAutoCancel(true)
在你的 NotificationCompat.Builder
uj5u.com熱心網友回復:
創建額外的操作按鈕和 PendingIntent
val closeIntent = Intent(this, CloseNotificationReceiver::class.java)
.putExtra("ID", notificationId)
val closeFlag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
PendingIntent.FLAG_IMMUTABLE else 0
val closePendingIntent = PendingIntent
.getBroadcast(this, requestCode, closeIntent, closeFlag)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
// ...
.addAction(R.drawable.ic_close, "Close", closePendingIntent)
// ...
NotificationManagerCompat.from(this)
.notify(notificationId, builder.build())
然后實作BroadcastReceiver處理關閉Intents
class CloseNotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getIntExtra("ID", 0)
NotificationManagerCompat.from(context).cancel(id)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/375844.html
