skip to Main Content

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


  1. This may be the reason because you are using getInitialMessage() method in wrong way.

    Modify your main.dart like the below way and test.

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
    
      FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    
      await FirebaseMessaging.instance.getInitialMessage().then((remoteMessage) {
    
        // whenever you open the app from the terminate state by clicking on Notification message, 
           here you will get your remoteMessage.
    
        // Set you App Screen Redirection here.
    
        // Once consumed, the RemoteMessage will be removed.
    
      });
      runApp(const MyApp());
    }
    

    For more reference please refer to the official document for Handling Interaction of Notification

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search