Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Push Notifications | Firebase Integration
Flutter Architecture and Features

bookPush Notifications

Firebase Cloud Messaging (FCM) is a powerful service that allows you to send push notifications to users on iOS, Android, and web platforms. Integrating FCM with your Flutter app enables real-time user engagement, such as alerts, reminders, or updates. To use FCM in Flutter, you rely on the firebase_messaging package, which manages the setup, message handling, and device registration for notifications. The integration process includes setting up Firebase in your project, configuring platform-specific settings, and writing code to receive and handle notifications.

main.dart

main.dart

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print('Background message: ${message.messageId}'); } void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { String? _notificationMessage = 'No notifications yet'; @override void initState() { super.initState(); _initFCM(); } void _initFCM() async { FirebaseMessaging messaging = FirebaseMessaging.instance; // Request permission for iOS devices await messaging.requestPermission(); // Get the device token String? token = await messaging.getToken(); print('FCM Token: $token'); // Handle foreground messages FirebaseMessaging.onMessage.listen((RemoteMessage message) { setState(() { _notificationMessage = 'Foreground: ${message.notification?.title ?? ''} - ${message.notification?.body ?? ''}'; }); }); // Handle messages when app is opened via notification FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { setState(() { _notificationMessage = 'Opened from notification: ${message.notification?.title ?? ''} - ${message.notification?.body ?? ''}'; }); }); } @override Widget build(BuildContext context) { return MaterialApp( title: 'FCM Demo', home: Scaffold( appBar: AppBar(title: Text('Firebase Cloud Messaging')), body: Center( child: Text( _notificationMessage ?? '', textAlign: TextAlign.center, ), ), ), ); } }

Firebase Cloud Messaging delivers different payloads depending on the app state. Use FirebaseMessaging.onMessage to handle notifications in the foreground, onMessageOpenedApp when the user opens the app from a notification, and a background handler for messages received while the app is closed. Access message.notification?.title and .body to show the content in your UI.

question mark

Which statement best describes how Firebase Cloud Messaging is integrated and used in a Flutter app, based on the code and explanations provided above?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookPush Notifications

Swipe to show menu

Firebase Cloud Messaging (FCM) is a powerful service that allows you to send push notifications to users on iOS, Android, and web platforms. Integrating FCM with your Flutter app enables real-time user engagement, such as alerts, reminders, or updates. To use FCM in Flutter, you rely on the firebase_messaging package, which manages the setup, message handling, and device registration for notifications. The integration process includes setting up Firebase in your project, configuring platform-specific settings, and writing code to receive and handle notifications.

main.dart

main.dart

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print('Background message: ${message.messageId}'); } void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { String? _notificationMessage = 'No notifications yet'; @override void initState() { super.initState(); _initFCM(); } void _initFCM() async { FirebaseMessaging messaging = FirebaseMessaging.instance; // Request permission for iOS devices await messaging.requestPermission(); // Get the device token String? token = await messaging.getToken(); print('FCM Token: $token'); // Handle foreground messages FirebaseMessaging.onMessage.listen((RemoteMessage message) { setState(() { _notificationMessage = 'Foreground: ${message.notification?.title ?? ''} - ${message.notification?.body ?? ''}'; }); }); // Handle messages when app is opened via notification FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { setState(() { _notificationMessage = 'Opened from notification: ${message.notification?.title ?? ''} - ${message.notification?.body ?? ''}'; }); }); } @override Widget build(BuildContext context) { return MaterialApp( title: 'FCM Demo', home: Scaffold( appBar: AppBar(title: Text('Firebase Cloud Messaging')), body: Center( child: Text( _notificationMessage ?? '', textAlign: TextAlign.center, ), ), ), ); } }

Firebase Cloud Messaging delivers different payloads depending on the app state. Use FirebaseMessaging.onMessage to handle notifications in the foreground, onMessageOpenedApp when the user opens the app from a notification, and a background handler for messages received while the app is closed. Access message.notification?.title and .body to show the content in your UI.

question mark

Which statement best describes how Firebase Cloud Messaging is integrated and used in a Flutter app, based on the code and explanations provided above?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
some-alt