In this recipe, you will see how to send notification messages when your app is in the background:
-
Add the latest version of the firebase_messaging plugin to the pubspec.yaml file of your app.
- At the top of the login_screen.dart file in your app, import the firebase_messaging.dart file:
import 'package:firebase_messaging/firebase_messaging.dart';
- At the top of the _LoginScreenState class, add the following declaration:
final FirebaseMessaging messaging = FirebaseMessaging.instance;
- In the initState method, edit the code in the whenComplete callback of the Firebase initializeApp method as follows (you will get an error in _firebaseBackgroundMessageReceived that we will fix in the next step):
Firebase.initializeApp().whenComplete(() {
auth = FirebaseAuthentication();
FirebaseMessaging.onBackgroundMessage(
_firebaseBackgroundMessageReceived);
setState(() {});
});
- Outside of the _LoginScreenState class, add a method, called _firebaseBackgroundMessageReceived...