Flutter でのトピック メッセージング
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
パブリッシュ / サブスクライブ モデルに基づく FCM トピック メッセージングでは、特定のトピックにオプトインした複数のデバイスにメッセージを送信できます。必要に応じて作成したトピック メッセージを FCM がルーティング処理し、確実に正しいデバイスにメッセージを配信します。
たとえば、地域の潮汐予測アプリのユーザーは、「潮流警報」トピックにオプトインし、特定の地域の海釣りに最適な状況の通知を受信できます。スポーツアプリのユーザーは、お気に入りのチームの実況ゲームスコアの自動更新に登録できます。
トピックに関する留意点を以下に示します。
トピック メッセージングは、天気、その他の広く入手可能な情報コンテンツに最適です。
トピック メッセージは、レイテンシではなくスループットに対して最適化されます。単一のデバイスまたはデバイスの小グループに高速で安全に配信する場合は、トピックではなく登録トークンをメッセージのターゲットとしてください。
各ユーザーの複数のデバイスにメッセージを送信する必要がある場合は、そのようなユースケース向けのデバイス グループ メッセージングを検討してください。
トピック メッセージングでは、トピックごとの登録数に制限はありませんが、FCM により次の制限が課されます。
- 1 つのアプリ インスタンスが登録できるのは、2,000 トピックまでです。
- 一括インポートを使用してアプリ インスタンスを登録する場合、各リクエストのアプリ インスタンスの数が 1,000 に制限されます。
- 新規登録の頻度がプロジェクトごとに制限されます。短期間に送信する登録リクエストが多すぎると、FCM サーバーから
429 RESOURCE_EXHAUSTED
(「割り当てを超過した」)というレスポンスが返されます。この場合は、指数バックオフを使用して再試行します。
クライアント アプリをトピックに登録する
クライアント アプリを既存のトピックに登録することも、新しいトピックを作成することもできます。クライアント アプリを新しいトピック名(Firebase プロジェクトにまだ存在していないトピック名)に登録すると、その名前の新しいトピックが FCM に作成され、その後すべてのクライアントはそのトピック名に登録できるようになります。
トピックに登録するには、トピック名を指定して subscribeToTopic()
を呼び出します。このメソッドは Future
を返します。これは登録が正常に完了すると解決されます。
await FirebaseMessaging.instance.subscribeToTopic("topic");
登録解除するには、トピック名を指定して unsubscribeFromTopic()
を呼び出します。
subscribeToTopic()
と unsubscribeFromTopic()
はウェブ クライアントではサポートされていません。ウェブユーザーの登録を管理する方法については、ウェブや JavaScript でメッセージをトピックに送信するをご覧ください。
次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-16 UTC。
[null,null,["最終更新日 2025-08-16 UTC。"],[],[],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)."]]