Flutter聊天应用实现类WhatsApp通知栏快捷回复功能的方法
通知栏快捷回复功能实现方案
以下为Android、iOS双端的原生实现方案,均对应系统原生支持的API,无需引入第三方依赖。
Android 端实现(适配Android 7.0/API 24及以上版本)
Android 7.0开始系统原生支持通知快捷回复能力,实现流程如下:
- 第一步:创建
RemoteInput实例,用于承载用户输入的回复内容// 自定义回复内容的唯一Key,后续接收输入时需要匹配该Key const val KEY_REPLY = "key_text_reply" val remoteInput = RemoteInput.Builder(KEY_REPLY).run { setLabel("输入回复内容") build() } - 第二步:构建接收回复的
PendingIntent,推荐使用BroadcastReceiver/Service处理逻辑,避免跳转Activity打断用户操作val replyIntent = Intent(context, ReplyReceiver::class.java).apply { putExtra("conversation_id", currentChatId) // 携带当前会话ID,用于定位要回复的聊天对象 } val replyPendingIntent: PendingIntent = PendingIntent.getBroadcast( context, currentChatId, replyIntent, // Android 12及以上必须指定可变性,FLAG_MUTABLE是RemoteInput写入内容的必要条件 PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE ) - 第三步:给通知添加快捷回复动作
val replyAction = NotificationCompat.Action.Builder( R.drawable.ic_reply, // 回复按钮图标 "回复", // 按钮显示文案 replyPendingIntent ).addRemoteInput(remoteInput).build() // 构建通知时添加该动作即可 val notification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(senderName) .setContentText(messageContent) .addAction(replyAction) .build() - 第四步:处理用户提交的回复内容
class ReplyReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val conversationId = intent.getIntExtra("conversation_id", -1) // 从RemoteInput中提取用户输入的内容 val replyText = RemoteInput.getResultsFromIntent(intent)?.getCharSequence(KEY_REPLY)?.toString() if (replyText != null && conversationId != -1) { // 执行业务逻辑:上报服务端、更新本地聊天记录、更新会话列表 sendReplyToServer(conversationId, replyText) // 处理完成后更新通知,移除输入框避免重复提交 val repliedNotification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentText("已回复") .build() NotificationManagerCompat.from(context).notify(conversationId, repliedNotification) } } }
iOS 端实现(适配iOS 10及以上版本)
iOS 10及以上通过UserNotifications框架实现快捷回复,流程如下:
- 第一步:App启动时注册通知类别,声明支持快捷回复动作
let replyAction = UNTextInputNotificationAction( identifier: "REPLY_ACTION", title: "回复", options: [], textInputButtonTitle: "发送", textInputPlaceholder: "输入回复内容" ) let chatCategory = UNNotificationCategory( identifier: "CHAT_MESSAGE_CATEGORY", actions: [replyAction], intentIdentifiers: [], options: [] ) UNUserNotificationCenter.current().setNotificationCategories([chatCategory]) - 第二步:推送通知时绑定对应类别
本地通知直接设置categoryIdentifier,远程推送在APNs payload中添加category字段即可,系统会自动识别并展示回复按钮:let content = UNMutableNotificationContent() content.title = senderName content.body = messageContent content.categoryIdentifier = "CHAT_MESSAGE_CATEGORY" content.userInfo = ["conversation_id": currentChatId] // 携带会话ID用于定位聊天 - 第三步:接收处理用户的回复内容
实现UNUserNotificationCenterDelegate代理方法:func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { guard response.actionIdentifier == "REPLY_ACTION", let textResponse = response as? UNTextInputNotificationResponse, let conversationId = response.notification.request.content.userInfo["conversation_id"] as? Int else { completionHandler() return } let replyText = textResponse.userText // 执行业务逻辑:上报服务端、更新本地聊天记录 sendReplyToServer(conversationId: conversationId, content: replyText) completionHandler() }
低于Android 7.0或iOS 10的系统不支持原生快捷回复,收到回复操作时直接跳转应用内对应会话页面即可。
内容的提问来源于stack exchange,提问作者Gbenga B Ayannuga
相关产品推荐
相关产品推荐

