You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Firebase通知快捷回复实现及消息可靠送达方案咨询

React Native 聊天应用通知快捷回复实现方案(Android + iOS)

Android 端实现

直接基于FCM远程配置快捷回复,无需依赖静默通知触发本地通知,可靠性更高:

  • 在FCM通知Payload中配置android.actions字段,添加input类型操作,指定输入框提示文本、确认按钮文本,同时携带会话ID等业务数据:
    {
      "notification": {
        "title": "新消息",
        "body": "对方发来一条消息"
      },
      "android": {
        "actions": [
          {
            "action": "REPLY_ACTION",
            "title": "回复",
            "inputType": "text",
            "inputPlaceholder": "输入回复内容..."
          }
        ],
        "priority": "high"
      },
      "data": {
        "conversationId": "123",
        "senderId": "456"
      }
    }
    
  • 配置FirebaseMessagingService,重写onMessageReceived处理通知接收,同时在服务中监听REPLY_ACTION的Intent:用户回复后,系统会将输入文本通过Intent.EXTRA_TEXT传递,提取文本结合data中的业务信息发送消息。
  • 确保申请POST_NOTIFICATIONS权限(Android 12+),并在AndroidManifest.xml中正确注册服务。即使应用被强制杀死,FCM仍能推送通知,快捷回复的Intent会触发服务处理逻辑。

iOS 端实现

结合iOS UserNotifications框架与FCM实现快捷回复,规避静默通知的可靠性问题:

  • App启动时注册通知类别与快捷回复操作(示例为OC代码,RN可通过原生模块封装):
    UNTextInputNotificationAction *replyAction = [UNTextInputNotificationAction actionWithIdentifier:@"REPLY_ACTION" title:@"回复" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"发送" textInputPlaceholder:@"输入回复内容..."];
    UNNotificationCategory *chatCategory = [UNNotificationCategory categoryWithIdentifier:@"CHAT_CATEGORY" actions:@[replyAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:chatCategory]];
    
  • FCM发送通知时,在Payload中指定apns.category为CHAT_CATEGORY,同时设置apns-push-type为alert确保通知正常显示:
    {
      "notification": {
        "title": "新消息",
        "body": "对方发来一条消息"
      },
      "apns": {
        "payload": {
          "aps": {
            "category": "CHAT_CATEGORY",
            "alert": {
              "title": "新消息",
              "body": "对方发来一条消息"
            }
          }
        },
        "headers": {
          "apns-push-type": "alert"
        }
      },
      "data": {
        "conversationId": "123",
        "senderId": "456"
      }
    }
    
  • 实现UNUserNotificationCenterDelegate的userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:方法,提取response.userText作为回复内容,结合userInfo中的业务数据发送消息。即使应用被强制杀死,该方法仍会被触发(需开启Remote notifications后台模式)。

消息可靠送达方案

无论用Socket还是API,核心通过以下机制保证消息不丢失:

  • 本地持久化缓存:用户回复的消息先写入本地存储(如AsyncStorage、Realm),标记为「待发送」状态,再执行发送操作。
  • 指数退避重试:发送失败时(Socket断开、API请求超时/失败),按1s、3s、5s、10s的间隔递增重试,直到发送成功或达到最大重试次数(此时提示用户手动重发)。
  • ACK确认机制:服务器收到消息后返回唯一确认标识,客户端收到标识后再将本地缓存的消息标记为「已发送」;超时未收到确认则触发重试。
  • 幂等性处理:给每条消息生成唯一messageId,发送时携带该ID,服务器收到重复ID请求直接返回确认,避免重复消息。
  • 多通道 fallback:优先用Socket发送,若Socket不可用(如应用后台、网络切换),自动切换为API发送,确保消息通过可用通道送达。

内容的提问来源于stack exchange,提问作者Anoop M M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 07:47:27