Push notifications let your app send messages to users even when the app is closed. This helps keep users informed and engaged.
0
0
Push notifications (FCM) in Flutter
Introduction
To alert users about new messages or updates instantly.
To remind users about events or tasks.
To send promotional offers or news.
To notify users about app changes or important alerts.
Syntax
Flutter
import 'package:firebase_messaging/firebase_messaging.dart'; FirebaseMessaging messaging = FirebaseMessaging.instance; // Request permission (iOS) await messaging.requestPermission(); // Listen for messages FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Message received: ${message.notification?.title}'); });
Use FirebaseMessaging.instance to access FCM features.
Always request permission on iOS before receiving notifications.
Examples
This gets the unique device token needed to send notifications to this device.
Flutter
FirebaseMessaging messaging = FirebaseMessaging.instance; // Get the device token String? token = await messaging.getToken(); print('Device token: $token');
This listens for notifications when the app is running in the foreground.
Flutter
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Notification received while app is open');
});This handles what happens when the user taps a notification to open the app.
Flutter
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('User tapped on notification');
});Sample App
This app initializes Firebase, requests notification permission, and listens for incoming push notifications. When a notification arrives while the app is open, it shows the notification title on screen.
Flutter
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { String _message = 'No message yet'; @override void initState() { super.initState(); FirebaseMessaging.instance.requestPermission(); FirebaseMessaging.onMessage.listen((RemoteMessage message) { setState(() { _message = message.notification?.title ?? 'No title'; }); }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('FCM Push Notifications')), body: Center(child: Text('Latest message: $_message')), ), ); } }
OutputSuccess
Important Notes
Push notifications require setting up Firebase project and adding config files to your app.
On iOS, users must allow notifications in system settings.
Test notifications using Firebase Console or server APIs.
Summary
Push notifications keep users updated even when app is closed.
Use FirebaseMessaging to get device token and listen for messages.
Always request permission on iOS and handle messages in app.