使用未决意图区分按钮点击

differentiate between button clicks using pending intent

我正在显示自定义通知 (FCM)。自定义通知有两个按钮 approvedeny.

我已经成功地创建了挂起的意图和一个广播接收器,现在我知道 onReceive 按钮何时被点击。

我如何区分 onReceive 中的按钮点击,我试图传递额外的意图,但 intent.getStringExtra("clicked") 给了我空值。

知道单击哪个按钮的正确方法是什么 approve , deny

这是我试过的代码。 提前感谢您的帮助 R

      override fun onMessageReceived(message: RemoteMessage) {
        Log.d("FCMService", "onMessageReceived START ${isAppOnForeground()}")
        if(!isAppOnForeground()) {
            val notificationLayout = RemoteViews(
                packageName,
                R.layout.plugin_requires_approval_notification_small
            )
            val notificationLayoutExpanded = RemoteViews(
                packageName,
                R.layout.plugin_requires_approval_notification_large
            )

            val title = message.data[MSG_TITLE]
            val subTitle = message.data[MSG_SUB_TITLE]

            notificationLayout.setTextViewText(R.id.tvTitle, title)
            notificationLayout.setTextViewText(R.id.tvSubTitle, subTitle)

            notificationLayoutExpanded.setTextViewText(R.id.tvTitle, title)
            notificationLayoutExpanded.setTextViewText(R.id.tvSubTitle, subTitle)

// Apply the layouts to the notification
            val customNotification = NotificationCompat.Builder(
                this,
                CarInfoProcessingService.NOTIFICATION_CHANNEL_ID
            )
                .setSmallIcon(R.mipmap.ic_launcher)
                .setStyle(NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .build()

            val switchIntent = Intent(this, SwitchButtonListener::class.java)
            switchIntent.putExtra("clicked", "btnApprove")
            val pendingSwitchIntent = PendingIntent.getBroadcast(
                this, 0,
                switchIntent, 0
            )
     //TWO BUTTONS WITH SAME PENDING SWITCH INTENT
            notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnApprove, pendingSwitchIntent)
            notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnDeny, pendingSwitchIntent)

            val notificationManager =
                getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.notify(0, customNotification)
        }
        Log.d("FCMService", "onMessageReceived END")
    }

    class SwitchButtonListener : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            Log.d("fcmService", "onReceive ${intent.getStringExtra("clicked")}")
        }
    }

清单

 <receiver android:name=".messaging.FcmService$SwitchButtonListener"  android:exported="true">
            <intent-filter>
                <action android:name="Button_Clicked"/>
            </intent-filter>
        </receiver>

编辑:更新的代码可能对其他人有帮助

    override fun onMessageReceived(message: RemoteMessage) {
        Log.d("FCMService", "onMessageReceived START ${isAppOnForeground()}")
        if(!isAppOnForeground()) {
            val notificationLayout = RemoteViews(
                packageName,
                R.layout.plugin_requires_approval_notification_small
            )
            val notificationLayoutExpanded = RemoteViews(
                packageName,
                R.layout.plugin_requires_approval_notification_large
            )

            val title = message.data[MSG_TITLE]
            val subTitle = message.data[MSG_SUB_TITLE]

            notificationLayout.setTextViewText(R.id.tvTitle, title)
            notificationLayout.setTextViewText(R.id.tvSubTitle, subTitle)

            notificationLayoutExpanded.setTextViewText(R.id.tvTitle, title)
            notificationLayoutExpanded.setTextViewText(R.id.tvSubTitle, subTitle)

// Apply the layouts to the notification
            val customNotification = NotificationCompat.Builder(
                this,
                CarInfoProcessingService.NOTIFICATION_CHANNEL_ID
            )
                .setSmallIcon(R.mipmap.ic_launcher)
                .setStyle(NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .build()

            val approveIntent = Intent(this, CustomNotificationListener::class.java)
            approveIntent.putExtra("onClickListener", "approve")
            val pendingApproveIntent = PendingIntent.getBroadcast(
                this,
                0,
                approveIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnApprove, pendingApproveIntent)

            val denyIntent = Intent(this, CustomNotificationListener::class.java)
            denyIntent.putExtra("onClickListener", "deny")
            val pendingDenyIntent = PendingIntent.getBroadcast(
                this,
                1,
                denyIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )

            notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnDeny, pendingDenyIntent)

            val notificationManager =
                getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.notify(0, customNotification)
        }
        Log.d("FCMService", "onMessageReceived END")
    }

    class CustomNotificationListener : BroadcastReceiver() {
        @Inject
        lateinit var chargeSessionRepo: ChargeSessionRepository

        private var lastChargeStatus: ChargeStatusDTO? = null

        override fun onReceive(context: Context, intent: Intent) {
            Log.d("fcmService", "onReceive ${intent.getStringExtra("onClickListener")}")
            when(intent.getStringExtra("onClickListener")) {
                "approve" -> {
                    
                }
                "deny" -> {
                    
                }
            }
        }
    }

您需要使用两个不同的 up-to-date PendingIntent 对象,围绕不同的 Intent 对象(例如,具有不同附加功能的对象)。

对于“不同”,您需要 PendingIntent 个对象的 ID 不同。 ID 是 PendingIntent.getBroadcast() 调用的第二个参数。

对于“up-to-date”,您需要更新您的代码之前可能创建的任何现有 PendingIntent。为此,将 PendingIntent.FLAG_UPDATE_CURRENT 作为第四个参数传递给 PendingIntent.getBroadcast() 调用。