docs/cloud-messaging/topic-messaging.md
Project: /docs/cloud-messaging/_project.yaml Book: /docs/_book.yaml page_type: guide
{% include "_shared/apis/console/_local_variables.html" %} {% include "_local_variables.html" %} {% include "docs/cloud-messaging/_local_variables.html" %} {% include "docs/android/_local_variables.html" %}
<link rel="stylesheet" type="text/css" href="/styles/docs.css" />Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.
For example, users of a local tide forecasting app could opt in to a "tidal currents alerts" topic and receive notifications of optimal saltwater fishing conditions in specified areas. Users of a sports app could subscribe to automatic updates in live game scores for their favorite teams.
Some things to keep in mind about topics:
Topic messaging is best suited for content such as weather, or other publicly available information.
Topic messages are optimized for throughput rather than latency. For fast, secure delivery to single devices or small groups of devices, target messages to registration tokens, not topics.
If you need to send messages to multiple devices per user, consider device group messaging for those use cases.
Topic messaging supports unlimited subscriptions for each topic. However, FCM enforces limits in these areas:
429 RESOURCE_EXHAUSTED ("quota exceeded") response. Retry
with exponential backoff.Client apps can subscribe to any existing topic, or they can create a new topic. When a client app subscribes to a new topic name (one that does not already exist for your Firebase project), a new topic of that name is created in FCM and any client can subsequently subscribe to it.
To subscribe to a topic, call subscribeToTopic() with the topic name. This method
returns a Future, which resolves when the subscription succeeded:
await FirebaseMessaging.instance.subscribeToTopic("topic");
To unsubscribe, call unsubscribeFromTopic() with the topic name.
subscribeToTopic() and unsubscribeFromTopic() are not supported for web
clients. To learn how to manage subscriptions for web users, see
Send messages to topics on Web/JavaScript.