如何从通知发送回复?

How to send a reply from a notification?

我正在开发一个应用来管理收到的通知。目前,我正在实现一个用户可以通过回复操作回复的功能,但我找不到正确设置回复消息和发送消息的方法。

这是我试过的

fun sendReplyMessage(sbn: StatusBarNotification, replyMessage: String) {
    sbn.notification.actions.firstOrNull { it.remoteInputs != null }?.let { action ->
        action.remoteInputs?.get(0)?.extras
            ?.putCharSequence(action.remoteInputs?.get(0)?.resultKey, replyMessage)
        action.actionIntent.send()
    }
}

您必须获取通知操作才能访问挂起的意图,在此意图上添加远程输入,然后调用方法 PendingIntent#send(context, requestCode, intent)

val notificationAction: android.app.Notification.Action = "Get the Action here"

val bundle = Bundle().apply{
     putString(remoteInput.resultKey, "Add the text here")
}

val intent = Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND)

RemoteInput.addResultsToIntent(notificationAction.getRemoteInputs(), intent, bundle)

notificationAction.actionIntent.send(context, 0, intent)