flutterfire
flutterfire copied to clipboard
🐛 [firebase_messaging] `getInitialMessage` returns `null` on iOS
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:
- Setup Firebase FCM to work with iOS
- Run the sample code
- Send a remote notification
- Tap on the notification on your device with the app
terminated - 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!
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 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.
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
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.
I have run the example provided with the package and same problem!
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!
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.