主题消息传递 (Flutter)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
基于其发布/订阅的模式,FCM 主题消息传递功能可让您将消息发送至已经选择加入特定主题的多台设备。您只需根据需要撰写主题消息,FCM 会负责确定发送路径并将消息可靠地传送至正确的设备。
例如,某个本地潮汐预报应用的用户可选择加入“潮汐警报”主题,并接收关于特定区域最佳海水捕鱼条件的通知。体育应用的用户可以订阅他们心仪球队的实时比分自动更新。
关于主题,请注意以下事项:
主题消息传递适合传递天气或其他可通过公开途径获得的信息。
主题消息的优化重心是吞吐量而非延迟。如需将消息快速安全地传送到单台设备或小规模设备组,应将消息定位至注册令牌,而非主题。
如果您需要向每位用户的多台设备发送消息,请考虑为这类场景使用设备组消息传递。
主题消息传递不限制每个主题的订阅数。但是,FCM 在以下方面有强制性限制:
- 一个应用实例最多只能订阅 2000 个主题。
- 如果您通过批量导入来订阅应用实例,则每次请求仅限订阅 1000 个应用实例。
- 每个项目新增订阅的频率存在限制。如果您在短时间内发送过多订阅请求,FCM 服务器将给出
429 RESOURCE_EXHAUSTED
(“已超出配额”)响应。请使用指数退避算法重试。
为客户端应用订阅主题
客户端应用可以订阅任何现有主题,也可创建新主题。当客户端应用订阅新的主题名称(您的 Firebase 项目中尚不存在的主题)时,系统会在 FCM 中使用这个名称创建一个新主题,随后任何客户端都可订阅该主题。
如需订阅主题,请使用主题名称调用 subscribeToTopic()
。此方法会返回一个 Future
,它会在订阅成功时进行解析:
await FirebaseMessaging.instance.subscribeToTopic("topic");
如需退订,请使用主题名称调用 unsubscribeFromTopic()
。
Web 客户端不支持 subscribeToTopic()
和 unsubscribeFromTopic()
。如需了解如何管理 Web 用户的订阅,请参阅在 Web/JavaScript 上向主题发送消息。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-16。
[null,null,["最后更新时间 (UTC):2025-08-16。"],[],[],null,["\u003cbr /\u003e\n\nBased on the publish/subscribe model, FCM topic messaging allows you to send a message\nto multiple devices that have opted in to a particular topic. You compose topic messages as\nneeded, and FCM handles routing and delivering the message reliably to the right\ndevices.\n\nFor example, users of a local tide\nforecasting app could opt in to a \"tidal currents alerts\" topic and receive\nnotifications of optimal saltwater fishing conditions in specified areas. Users of a sports app\ncould subscribe to automatic updates in live game scores for their favorite\nteams.\n\nSome things to keep in mind about topics:\n\n- Topic messaging is best suited for content such as weather, or other publicly\n available information.\n\n- Topic messages are **optimized for throughput rather than latency** . For fast,\n secure delivery to single devices or small groups of devices,\n [target messages to registration tokens](/docs/cloud-messaging/send-message#send_messages_to_specific_devices),\n not topics.\n\n- If you need to send messages to multiple devices *per user* , consider\n [device group messaging](/docs/cloud-messaging/send-message#send_messages_to_device_groups)\n for those use cases.\n\n- Topic messaging supports unlimited subscriptions for each topic. However, FCM\n enforces limits in these areas:\n\n - One app instance can be subscribed to no more than 2000 topics.\n - If you are using [batch import](https://developers.google.com/instance-id/reference/server#manage_relationship_maps_for_multiple_app_instances) to subscribe app instances, each request is limited to 1000 app instances.\n - The frequency of new subscriptions is rate-limited per project. If you send too many subscription requests in a short period of time, FCM servers will respond with a `429 RESOURCE_EXHAUSTED` (\"quota exceeded\") response. Retry with exponential backoff.\n\nSubscribe the client app to a topic\n\nClient apps can subscribe to any existing topic, or they can create a new\ntopic. When a client app subscribes to a new topic name (one that does\nnot already exist for your Firebase project), a new topic of that name is\ncreated in FCM and any client can subsequently subscribe to it.\n\nTo subscribe to a topic, call `subscribeToTopic()` with the topic name. This method\nreturns a `Future`, which resolves when the subscription succeeded: \n\n await FirebaseMessaging.instance.subscribeToTopic(\"topic\");\n\nTo unsubscribe, call `unsubscribeFromTopic()` with the topic name.\n\n`subscribeToTopic()` and `unsubscribeFromTopic()` are not supported for web\nclients. To learn how to manage subscriptions for web users, see\n[Send messages to topics on Web/JavaScript](https://firebase.google.com/docs/cloud-messaging/js/topic-messaging).\n\nNext steps\n\n- Learn how to [send topic messages](/docs/cloud-messaging/send-message#send-messages-to-topics).\n- Learn how to [Manage topic subscriptions on the server](/docs/cloud-messaging/manage-topics)."]]