Flutter 上的主題訊息
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
根據發布/訂閱模式,FCM 主題訊息功能可讓您將訊息傳送給已選擇特定主題的多部裝置。您可以視需要撰寫主題訊息,FCM 會處理訊息的路由和傳送作業,確保訊息可靠地傳送到正確的裝置。
舉例來說,當地潮汐預報應用程式的使用者可以選擇加入「潮汐流警報」主題,並在指定區域的鹽水釣魚條件最佳時收到通知。體育應用程式使用者可以訂閱喜愛球隊的即時比數自動更新。
主題注意事項:
主題訊息最適合用於天氣或其他公開資訊等內容。
主題訊息是針對總處理量進行最佳化調整,而非針對延遲。如要快速且安全地將訊息傳送至單一裝置或小型裝置群組,請將訊息指定至註冊權杖,而非主題。
如要為每位使用者傳送訊息到多部裝置,請考慮針對這些用途使用裝置群組訊息。
主題訊息傳送功能支援每個主題的無限訂閱項目。不過,FCM 在下列方面設有限制:
- 一個應用程式執行個體最多可訂閱 2000 個主題。
- 如果使用批次匯入訂閱應用程式執行個體,每個要求最多只能包含 1, 000 個應用程式執行個體。
- 每個專案的新訂閱頻率都有速率限制。如果在短時間內傳送過多訂閱要求,FCM 伺服器會傳回
429 RESOURCE_EXHAUSTED
(「超過配額」) 回應。以指數輪詢方式重試。
讓用戶端應用程式訂閱主題
用戶端應用程式可以訂閱任何現有主題,也可以建立新主題。當用戶端應用程式訂閱新的主題名稱 (Firebase 專案中尚未存在的主題),FCM 就會建立該名稱的新主題,任何用戶端隨後都能訂閱。
如要訂閱主題,請使用主題名稱呼叫 subscribeToTopic()
。這個方法會傳回 Future
,訂閱成功時會解析:
await FirebaseMessaging.instance.subscribeToTopic("topic");
如要取消訂閱,請使用主題名稱呼叫 unsubscribeFromTopic()
。
網頁用戶端不支援「subscribeToTopic()
」和「unsubscribeFromTopic()
」。如要瞭解如何管理網站使用者的訂閱項目,請參閱「在 Web/JavaScript 上傳送主題訊息」。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-16 (世界標準時間)。
[null,null,["上次更新時間: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)."]]