Updated One:
FirebaseMessagingService is important to get started in the beginning of app. And for that sake, you need to follow this:
Declare this function first :
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
debugPrint("Firebase Messaging firebase is initialized");
await Firebase.initializeApp();
}
And call this function in the main(){} of app:
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
And then you will be able to use these functions:
FirebaseMessaging.onMessage method to get messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
});
FirebaseMessaging.onMessageOpenedApp as a replacement for onLaunch and onResume handlers.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});