Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/messaging/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> _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());
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ 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.
///
/// To verify things are working, check out the native platform logs.
Future<void> _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}');
}

Expand All @@ -37,14 +42,7 @@ late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

Future<void> 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);
Expand Down