Firebase Cloud Messaging & Notifications
Send and receive push notifications in your Flutter app using Firebase Cloud Messaging, handling foreground, background and tapped states.
Firebase Cloud Messaging & Notifications is a free Flutter Mobile Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flutter Mobile Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Push Notifications?
Firebase Cloud Messaging (FCM) lets your server send push notifications to users even when the app is closed. It is key for re-engagement and real-time alerts.
Adding the Package
Add firebase_messaging to your dependencies. You should already have firebase_core set up from earlier lessons.
dependencies:
firebase_core: ^2.0.0
firebase_messaging: ^14.0.0Requesting Permission
On iOS (and Android 13+) you must request notification permission from the user.
final messaging = FirebaseMessaging.instance;
await messaging.requestPermission(
alert: true, badge: true, sound: true,
);The Device Token
Each device gets a unique FCM token. Send it to your server so you can target that device.
final token = await messaging.getToken();
print('FCM token: ' + (token ?? 'none'));Token Refresh
Tokens can change. Listen for refreshes and update your server.
messaging.onTokenRefresh.listen((newToken) {
sendTokenToServer(newToken);
});Three Message States
You must handle messages in three situations:
- Foreground — app open
- Background — app minimized
- Terminated — app closed
Foreground Messages
When the app is open, FCM does not show a system notification automatically — you receive the data and display it yourself.
FirebaseMessaging.onMessage.listen((message) {
final title = message.notification?.title ?? '';
showLocalNotification(title);
});Background Handler
Register a top-level background handler before runApp. It must be a top-level function.
Future<void> bgHandler(RemoteMessage m) async {
print('Background: ' + (m.messageId ?? ''));
}
void main() {
FirebaseMessaging.onBackgroundMessage(bgHandler);
}Handling a Tapped Notification
When the user taps a notification that opened the app, route them to the right screen.
FirebaseMessaging.onMessageOpenedApp.listen((message) {
final route = message.data['route'];
if (route != null) navigator.pushNamed(route);
});Topic Subscriptions
Subscribe devices to topics to broadcast to groups without managing individual tokens.
await messaging.subscribeToTopic('news');
await messaging.unsubscribeFromTopic('news');Local Notifications for Display
Pair FCM with flutter_local_notifications to show rich notifications while in the foreground.
final plugin = FlutterLocalNotificationsPlugin();
await plugin.show(0, 'Title', 'Body', notificationDetails);Quick Check
What is true about a notification received while the app is in the foreground?
Recap
You learned Firebase Cloud Messaging:
- Permissions and device tokens
- Foreground, background and terminated handling
- Tapped-notification routing and topics
- Pairing with local notifications
Now your app can re-engage users with timely pushes.
Frequently asked questions
Is the “Firebase Cloud Messaging & Notifications” lesson free?
Yes — the full text of “Firebase Cloud Messaging & Notifications” is free to read here on the web, and the Flutter Mobile Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flutter Mobile Development course, upgrade to CoddyKit PRO.
What will I learn in “Firebase Cloud Messaging & Notifications”?
Send and receive push notifications in your Flutter app using Firebase Cloud Messaging, handling foreground, background and tapped states. You practise Flutter Mobile Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flutter Mobile Development?
No prior experience is required. Flutter Mobile Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Firebase Cloud Messaging & Notifications” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flutter Mobile Development lesson?
Yes. Every Flutter Mobile Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Firebase Setup & Auth
- Cloud Firestore Database
- Cloud Storage & Functions
- Firebase Cloud Messaging & Notifications