flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [firebase_messaging] `getInitialMessage` returns `null` on iOS

Open Tom3652 opened this issue 3 years ago • 5 comments

Bug report

Describe the bug

I am calling RemoteMessage? message = await FirebaseMessaging.instance.getInitialMessage(); on my app to open a specific page when the user clicks on the push notification.

On Android the message var above is not null and the page is pushed. On iOS, the message var above is null when the user opens the app after touching a push notification.

I am using the firebase_messaging: 13.0.0 version.

Steps to reproduce

Steps to reproduce the behavior:

  1. Setup Firebase FCM to work with iOS
  2. Run the sample code
  3. Send a remote notification
  4. Tap on the notification on your device with the app terminated
  5. See that it doesn't push the new page

Expected behavior

The method should not return null as it is done on Android when there is an actual RemoteMessage.

Sample project

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';


Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const TestApp());
}

class TestApp extends StatelessWidget {
  const TestApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: HomePage()
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  Future<void> test() async {
    RemoteMessage? message = await FirebaseMessaging.instance.getInitialMessage();
    if (message != null) {
      Navigator.push(context, MaterialPageRoute(builder: (ctx) =>
          Scaffold(body: Center(
            child: Text("Page pushed from getInitialMessage"),),)));
    }
  }

  @override
  void initState() {
    test();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold();
  }
}

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.0, on macOS 12.5.1 21G83 darwin-x64, locale fr-FR)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.70.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!

Tom3652 avatar Sep 03 '22 15:09 Tom3652

I must say that it's working half of the time on iOS.

Yesterday i could see that it's working on 1/2 notifications in release mode.

I am sending push notifications from my server with the admin sdk in python. I have setup the notification and the data in the RemoteMessage the same way for Android and iOS.

Tom3652 avatar Sep 05 '22 06:09 Tom3652

@Tom3652 Can you take a look at this PR https://github.com/firebase/flutterfire/pull/9292, wherein, the plugin example was updated to confirm that messaging handlers work as expected.

I had also used the plugin example and verified that, in terminated state, when a notification is triggered using the script added in the plugin and run using node js, the notification is received and tapping on it directs the user towards an app screen.

darshankawar avatar Sep 05 '22 10:09 darshankawar

Hey @darshankawar, it's working with the PR and also in my app (half of the time) so i don't know what else to try. Note : i have checked the PR and i see it fixes especially background messaging not invoked and onMessageOpenedApp (both methods that always worked in my app)

I am guessing there is some race condition maybe using getInitialMessage() from a terminated state but i can't reproduce it 100% so... It's not a blocking feature in my app either but i prefer raise the ticket in case there is something beneath.

I will try to catch the error if there is any

Tom3652 avatar Sep 09 '22 09:09 Tom3652

I am guessing there is some race condition maybe using getInitialMessage() from a terminated state

Are you using any community plugin which might be conflicting with messaging's notification ? like, flutter_apns ? This could be something you can also look and see if that' really the case.

Considering the fact that the behavior is not consistent everytime and the plugin example does clear the notion that all handlers actually works, maybe this has something to do with an external factor / dependency.

darshankawar avatar Sep 09 '22 11:09 darshankawar

I have run the example provided with the package and same problem!

hatemragab avatar Sep 15 '22 23:09 hatemragab

Hey @Tom3652. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Sep 26 '22 01:09 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@Tom3652 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Oct 05 '22 01:10 google-oss-bot