After carefully trying other answer, nothing seems to work.
Notification are working fine, when I click on them and the app has been initialised.
When the app is closed or terminated it is impossible for me to redirect the user to a page because the value of the getInitialMessage() is null.
For example, this return null, but it is the home page of my Flutter app
@override
void initState() {
super.initState();
FirebaseMessaging.instance
.getInitialMessage()
.then((message) => print('Initial message: $message'))
Main.dart
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
runApp(const FrontDeskApp());
}
Thank you
2
Answers
This may be the reason because you are using getInitialMessage() method in wrong way.
Modify your
main.dart
like the below way and test.For more reference please refer to the official document for Handling Interaction of Notification
I was working on the same issue. And it seems always null.
I believe the misconception is that getInitialMessage() is not filled if the app is not opened via clicking the push message, but by directly opening via AppIcon
The documentation says:
And by interaction it probably refers to "For example, if a new chat message is sent via a notification and the user presses it,"
https://firebase.google.com/docs/cloud-messaging/flutter/receive#handling_interaction
So, seemingly there is no way to track if there is a lingering push notification, when opened by app icon.
Correct me if I’m wrong.