Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseMessaging.instance.getInitialMessage() returning null

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

like image 377
Gaëtan GR Avatar asked Jun 05 '26 22:06

Gaëtan GR


2 Answers

in Android you need to tap 'notification' and "priority": "high" to get 'getInitialMessage' when tap on notification to get remoteMessage.

{
    "registration_ids": [
        "deClPkv1RgmIYCK3sdxKmh:APA91bFIrld_TDA8FGDRcIcgdigyIYpzneULjvAqoRBfujcZ-hUankuzizfMmPFIU6G2Jy-7Edn4IUPrtuA8oPqdEMhLM9FrUozljgXJ69cl9mnqoRp2Q6J0nFirfo819PqCywox4e5T"
    ],
     "data": {
        "body": "this is subtitle",
        "title": "this is title"
    },
    // don't remove this
    "notification": {
        "title": "this is title",
        "body": "this is subtitle"
    },
    // don't remove this
    "priority": "high",
    "contentAvailable": 1
}
like image 192
MKD Avatar answered Jun 09 '26 19:06

MKD


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:

The firebase-messaging package provides two ways to handle this interaction:

getInitialMessage(): If the application is opened from a terminated state a Future containing a RemoteMessage will be returned. Once consumed, the RemoteMessage will be removed. onMessageOpenedApp: A Stream which posts a RemoteMessage when the application is opened from a background state.

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.

like image 28
Ulf Avatar answered Jun 09 '26 21:06

Ulf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!