Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Push Notifications | Firebase Integration
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

How do I set up Firebase Cloud Messaging in my Flutter app?

Can you explain how to handle notifications in different app states?

What are the steps to configure platform-specific settings for FCM?

bookPush Notifications

Deslize para mostrar o 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
some-alt