PAYATHON 2026

flutter_local_notifications 中 MessagingStyle 内联回复卡在加载状态

数据小陈

结论

需要改。MessagingStyleInformation.person 要设为当前用户,也就是接收和回复的一方,不是对话对方。另外,处理完 response.input 后必须用同一个 notification id 重新 show,这样 Android 才会结束 RemoteInput 的加载状态。只把 person 改对还不够,重显时换了 id,旧通知照样一直转圈。

为什么会卡住

Android 的通知内联回复依赖 RemoteInput。用户提交回复后,系统会等应用更新或取消对应通知。如果回调里只把消息发出去,没有回头更新原通知,通知栏上的 spinner 就会一直留着。

这里要分清两个 person:MessagingStyleInformation.person 表示会话的“当前用户”,每条 Message.person 表示该条消息的发送者。两者混用,回复身份和消息展示都会出问题。

正确做法

  1. 创建 MessagingStyleInformation 时,把 person 设为当前用户。
  2. 每条 Message 的 person 按实际发送者填写,自己发出的回复用当前用户。
  3. 给 action 配置输入项,NotificationResponse.input 才能拿到用户输入。
  4. 在 onDidReceiveNotificationResponse 或后台回调里处理 response.input,然后用同一个 notification id 重新显示通知。
  5. 即使 response.input 为空,也要更新或取消通知,避免 spinner 残留。

下面的代码用内存列表示意,实际项目把消息存到本地数据库或状态管理里。

const int chatNotificationId = 1001;
const String replyActionId = 'reply';

final currentUser = const Person(name: '我');
final alice = const Person(name: 'Alice');

Future<void> showChatNotification(
  FlutterLocalNotificationsPlugin plugin,
  List<Message> messages,
) async {
  final androidDetails = AndroidNotificationDetails(
    'chat_messages',
    '聊天消息',
    channelDescription: '聊天消息通知',
    importance: Importance.max,
    priority: Priority.high,
    onlyAlertOnce: true, // 更新时避免重复提醒
    styleInformation: MessagingStyleInformation(
      currentUser, // 关键:当前用户,不是对方
      conversationTitle: 'Alice',
      groupConversation: false,
      messages: messages,
    ),
    actions: [
      AndroidNotificationAction(
        replyActionId,
        '回复',
        showsUserInterface: false,
        allowGeneratedReplies: true,
        inputs: [
          AndroidNotificationActionInput(label: '输入回复'),
        ],
      ),
    ],
  );

  await plugin.show(
    chatNotificationId, // 更新时必须复用这个 id
    'Alice',
    messages.isEmpty ? '' : messages.last.body,
    NotificationDetails(android: androidDetails),
    payload: 'chat:alice',
  );
}

Future<void> handleNotificationResponse(
  FlutterLocalNotificationsPlugin plugin,
  List<Message> messages,
  NotificationResponse response,
) async {
  if (response.actionId != replyActionId) return;

  final text = response.input?.trim();
  if (text != null && text.isNotEmpty) {
    messages.add(Message(text, DateTime.now(), currentUser));
  }

  // input 为空也要重显或取消,否则 spinner 可能一直转
  await showChatNotification(plugin, messages);
}

如果回复可能在应用退到后台、甚至进程被回收后触发,还要同时接后台回调:

@pragma('vm:entry-point')
Future<void> onBackgroundNotificationResponse(
  NotificationResponse response,
) async {
  final plugin = FlutterLocalNotificationsPlugin();

  // 这里需要按你的项目重新初始化插件,并把 response.input 保存到数据库/本地存储。
  // 保存后读取最新消息,再调用 showChatNotification(plugin, latestMessages)。
  // 注意:后台 isolate 不能依赖页面里的内存变量。
}

初始化插件时把前台和后台回调都挂上,InitializationSettings 按你的平台配置补全。回调里用同一个 chatNotificationId 更新通知,不要新建 id。

注意事项

  • response.input 可能是 null,也可能是空字符串,不要只在非空时才更新通知。
  • 更新通知时,notification id 和会话对应关系要保持一致,用新 id 会留下一个还在加载的旧通知。
  • action 设置了 cancelNotification: true 时,系统可能自动取消通知,但回复仍要写进会话状态,不能只依赖取消。
  • 后台回调读不到页面内存里的消息列表,记得先持久化再读取。
  • Android 14 上先确认通知权限和通知渠道是否正常,渠道被降级或通知不可交互时,RemoteInput 的行为也会受影响。
  • 更新通知时加上 onlyAlertOnce: true,避免每次回复都重新响铃。

备注:内容仅供参考。