diff --git a/docs/messaging/usage.mdx b/docs/messaging/usage.mdx index 4b2909671129..b428164fcc59 100644 --- a/docs/messaging/usage.mdx +++ b/docs/messaging/usage.mdx @@ -221,16 +221,23 @@ There are a few things to keep in mind about your background message handler: 2. It must be a top-level function (e.g. not a class method which requires initialization). ```dart +// We recommend initializing the app from the top-level so it can be shared with your background message handler +// See this issue for further details: https://github.com/FirebaseExtended/flutterfire/issues/6087 +final initApp = Firebase.initializeApp(); + Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // If you're going to use other Firebase services in the background, such as Firestore, - // make sure you call `initializeApp` before using other Firebase services. - await Firebase.initializeApp(); + // make sure you await `initApp` before using other Firebase services. + await initApp; print("Handling a background message: ${message.messageId}"); } -void main() { +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + await initApp; FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); + runApp(MyApp()); } ``` diff --git a/packages/firebase_messaging/firebase_messaging/example/lib/main.dart b/packages/firebase_messaging/firebase_messaging/example/lib/main.dart index 431b6ddf30f8..53cc769ce216 100644 --- a/packages/firebase_messaging/firebase_messaging/example/lib/main.dart +++ b/packages/firebase_messaging/firebase_messaging/example/lib/main.dart @@ -18,6 +18,11 @@ import 'message_list.dart'; import 'permissions.dart'; import 'token_monitor.dart'; +/// In light of https://github.com/FirebaseExtended/flutterfire/issues/6087, +/// we make the initialized Firebase app top-level so it can be shared with the background message handler & the app. +final initApp = + Firebase.initializeApp(options: DefaultFirebaseConfig.platformOptions); + /// Define a top-level named handler which background/terminated messages will /// call. /// @@ -25,7 +30,7 @@ import 'token_monitor.dart'; Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // If you're going to use other Firebase services in the background, such as Firestore, // make sure you call `initializeApp` before using other Firebase services. - await Firebase.initializeApp(options: DefaultFirebaseConfig.platformOptions); + await initApp; print('Handling a background message ${message.messageId}'); } @@ -37,14 +42,7 @@ late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; Future main() async { WidgetsFlutterBinding.ensureInitialized(); - await Firebase.initializeApp( - options: const FirebaseOptions( - apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0', - appId: '1:448618578101:ios:0b11ed8263232715ac3efc', - messagingSenderId: '448618578101', - projectId: 'react-native-firebase-testing', - ), - ); + await initApp; // Set the background messaging handler early on, as a named top-level function FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);