Android 如何区分通知中已单击的操作?
我正在制作一款Android应用程序,它每天发送几次带有动作的通知,问题是在这个时刻,用户点击哪个动作并不重要,它总是向广播接收器发送第一个意图 我的代码:Android 如何区分通知中已单击的操作?,android,kotlin,broadcastreceiver,android-notifications,android-pendingintent,Android,Kotlin,Broadcastreceiver,Android Notifications,Android Pendingintent,我正在制作一款Android应用程序,它每天发送几次带有动作的通知,问题是在这个时刻,用户点击哪个动作并不重要,它总是向广播接收器发送第一个意图 我的代码: fun sendNotification(title: String, content: String, tomaID: Int){ val takeShotIntent = Intent(context, TreatmentBroadcastReceiver::class.java).apply { putExtr
fun sendNotification(title: String, content: String, tomaID: Int){
val takeShotIntent = Intent(context, TreatmentBroadcastReceiver::class.java).apply {
putExtra("TomaID", tomaID)
putExtra("AcctionToma", 0)
}
val takeShotPendingIntent = PendingIntent.getBroadcast(context, NOTIFICACION_ID, takeShotIntent, PendingIntent.FLAG_ONE_SHOT)
val skipShotIntent = Intent(context, TreatmentBroadcastReceiver::class.java).apply {
putExtra("TomaID", tomaID)
putExtra("AcctionToma", 1)
}
val skipShotPendingIntent = PendingIntent.getBroadcast(context, NOTIFICACION_ID, skipShotIntent, PendingIntent.FLAG_ONE_SHOT)
val postPoneShotIntent = Intent(context, TreatmentBroadcastReceiver::class.java).apply {
putExtra("TomaID", tomaID)
putExtra("AcctionToma", 2)
}
val postPoneShotPendingIntent = PendingIntent.getBroadcast(context, NOTIFICACION_ID, postPoneShotIntent, PendingIntent.FLAG_ONE_SHOT)
val notifyBuilder = getNotificationBuilder(title,content)
notifyBuilder.addAction(R.drawable.ic_capsula, context.getString(R.string.tomar), takeShotPendingIntent)
notifyBuilder.addAction(R.drawable.ic_capsula,context.getString(R.string.saltar), skipShotPendingIntent)
notifyBuilder.addAction(R.drawable.ic_capsula,context.getString(R.string.posponer), postPoneShotPendingIntent)
mNotifyManager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
mNotifyManager.notify(NOTIFICACION_ID, notifyBuilder.build())
}
以及广播接收器类别:
class TreatmentBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val idToma = intent.getIntExtra("TomaID", -1)
val acctionToma = intent.getIntExtra("AcctionToma", -1)
Log.d("EstasReciviendo", idToma.toString() + " " + acctionToma)
}
}
我需要发送一个ID和一个代表应用程序应该根据用户选择做什么的数字。ID发送时没有问题,但正如我上面提到的,无论点击哪个操作,onReceive方法中的“AccionToma”始终为0
我的logcat输出:
2019-07-21 17:09:10.993 20286-20286/com.kps.spart.moskimedicationreminder D/EstasReciviendo: 1049 0
所以,
如何区分已单击的操作?对三个
PendingEvent.getBroadcast()
调用使用不同的唯一值,而不是通知ID
目前,您的第二个pendingent.getBroadcast()
调用将替换第一个调用,第三个pendingent.getBroadcast()
调用将替换第二个调用