Sub topic 13
Sending to Groups with FCM Topics
This chapter assumes you know basic Dart, Flutter widgets, async/await, and Firebase initialization. It also assumes that your app already includes firebase_core and firebase_messaging, and that you can run it on a real device or emulator.
A topic is a named group managed by Firebase Cloud Messaging (FCM). A device subscribes to a topic, such as news or sports. Your server can then send one message to that topic instead of storing and sending to every device token individually.
The delivery path looks like this:
Subscribe and unsubscribe in Flutter
Call subscribeToTopic after Firebase has initialized. The topic name is case-sensitive and should contain only a stable identifier that your app understands.
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'firebase_options.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const TopicApp());
}
class TopicApp extends StatelessWidget {
const TopicApp({super.key});
Future<void> subscribeToNews() {
return FirebaseMessaging.instance.subscribeToTopic('news');
}
Future<void> unsubscribeFromNews() {
return FirebaseMessaging.instance.unsubscribeFromTopic('news');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Topic notifications')),
body: Center(
child: ElevatedButton(
onPressed: subscribeToNews,
child: const Text('Subscribe to news'),
),
),
),
);
}
}Subscription state belongs to the FCM installation, not to a particular screen. A common pattern is to subscribe when a user enables a notification category and unsubscribe when the user disables it. Do not subscribe every time a widget rebuilds. Put the operation in an initialization method or behind an explicit user action.
Send to a topic
In the Firebase console, choose Cloud Messaging, create a notification, and select a topic as the target. For production systems, your trusted server can use the FCM HTTP v1 API. The request uses topic instead of token:
{
"message": {
"topic": "news",
"notification": {
"title": "Daily update",
"body": "A new article is available."
},
"data": {
"route": "/news"
}
}
}The server must authenticate with Google credentials and send the request from a protected environment. Never place server credentials in the Flutter application.
A notification sent to a topic can reach many devices. Each device still follows its platform and app-state rules. For example, a notification message may be displayed automatically while the app is in the background, but foreground handling still belongs to your Dart listeners.
Topics versus registration tokens
Use a registration token when you need to target one installation or a small, precisely selected set. Use a topic when many devices should receive the same broadcast.
| Target | Best use | Main responsibility |
|---|---|---|
| Registration token | One device or user installation | Keep tokens current on your server |
| Topic | A shared broadcast group | Manage subscriptions and topic naming |
| Topic condition | A combination of groups | Define and test the condition carefully |
Topics do not replace authorization. If a message must be visible only to a specific account, verify that account on your server and send to its stored token or to a controlled topic.
Exercise
A user turns off the “news” setting in your app. Which FCM operation should you call, and why?
Show answer
Call FirebaseMessaging.instance.unsubscribeFromTopic('news') so that FCM removes this installation from the group and future topic messages are no longer targeted to it.