🐛 [Firebase Cloud Messaging] getInitialMessage() called multiple times for a single notification
Bug report
Whenever a message is sent from server or any other device, getInitailMessage() gets called 3 times and getInitialMessage stream is being listened multiple times. It is expected to be called only once as notification is processed or read. I am using firebase_messaging: ^8.0.0-dev.8
Hi @MuhammadFaizan980
What do you mean? getInitialMessage is an asynchronous method that returns a RemoteMessage if the app was opened from a terminated state through one. It is not a stream and it is only called by using it in your own code. The method also consumes the initial RemoteMessage and any calls to it after the first will return null.
This is either a mistake in your code or you are referring to a different method/stream. Please check your code or provide additional clarification as to what you mean. Thank you
Thanks for your reply @markusaksli-nc, I had a typo (now typo is fixed). Let me elaborate further. When getInitialMessage() is called or onMessageOpenedApp is listened, it is supposed to consume RemoteMessage right? but thing is, it does not, when I open the app from terminated state or suspended state by clicking notification, respective functions are called but when I restart app, it again calls above functions with RemoteMessage still not null as I have added a null check for RemoteMessage, This behavior persists for two to three attempts (that is what I mean by 2-3 times call) before RemoteMessage is consumed and null is returned.
Can you please provide a minimal complete reproducible code sample?
I was not able to reproduce this with the official example using firebase_messaging: ^8.0.0-dev.8
flutter doctor -v
[√] Flutter (Channel master, 1.24.0-8.0.pre.365, on Microsoft Windows [Version 10.0.19041.630], locale et-EE)
• Flutter version 1.24.0-8.0.pre.365 at C:\Development\flutter_master
• Framework revision 2ceb371d21 (8 hours ago), 2020-11-24 17:18:06 -0800
• Engine revision 6ed357c8b3
• Dart version 2.12.0 (build 2.12.0-74.0.dev)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:\Users\marku\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.7.30621.155
• Windows 10 SDK version 10.0.18362.0
[√] Android Studio (version 4.1.0)
• Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] IntelliJ IDEA Ultimate Edition (version 2020.2)
• IntelliJ at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\202.7660.26
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
[√] Connected device (5 available)
• SM G950F (mobile) • ce12171c51cc001c03 • android-arm64 • Android 9 (API 28)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19041.630]
• Web Server (web) • web-server • web-javascript • Flutter Tools
• Chrome (web) • chrome • web-javascript • Google Chrome 87.0.4280.66
• Edge (web) • edge • web-javascript • Microsoft Edge 86.0.622.68
• No issues found!
Sorry for late reply, was out of town, here is the minimal code:
void initState() {
_configureFcm();
super.initState();
}
Future<void> _configureFcm() async {
_firebaseMessaging = FirebaseMessaging.instance;
await _firebaseMessaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
// Works fine
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
AppToast.showToast(message: 'onMessageOpenedApp called');
// This gets called three times, every time gets called when app is restarted or I navigate back using pushReplacement()
});
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message){
AppToast.showToast(message: 'getInitialMessage() called');
// This gets called 4 times, every time gets called when app is restarted or I navigate back using pushReplacement()
});
}
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:/sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted. [!] Android Studio (version 4.1.0)
• Android Studio at C:\Program Files\Android\Android Studio1
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) [√] VS Code (version 1.50.1)
• VS Code at C:\Users\My Own\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.15.1 [√] Connected device (1 available)
• Android SDK built for x86 (mobile) • emulator-5554 • android-x86 • Android 10 (API 29) (emulator) ! Doctor found issues in 1 category. flutter doctor -v output
[√] Flutter (Channel stable, 1.22.4, on Microsoft Windows [Version 10.0.10240], locale en-US)
• Flutter version 1.22.4 at C:\flutter
• Framework revision 1aafb3a8b9 (13 days ago), 2020-11-13 09:59:28 -0800
• Engine revision 2c956a31c0
• Dart version 2.10.4
Do you mean just hot restarting the app or actually terminating and re-opening it?
I tested on hot restart and full app restart both
Issue persists not only with restarting of the app but also with the replacement of screen using pushReplacement()
Do you experience this with the code in the official example as well?
Though this shouldn't be possible in the first place.
I followed flutter fire documentation but will try pfficial example as well let you know
We used the same example on this page: https://pub.dev/packages/firebase_dynamic_links/example And we still have the same problem. We have this code on our home page in our app. Then based on the input, we redirect the user to another page. User can then go back to previous page. In this case, it gets data to PendingDynamicLinkData variable from FirebaseDynamicLinks.instance.getInitialLink() again and redirects the user back to second page. Hence causing infinite loop. I have a workaround, to save the incoming URLs in a temporary variable, but that's a dirty workaround. Please suggest if there is a better way to solve this.
Hey @MuhammadFaizan980. 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!
Sorry, was out of country, as for the official example, I tried it as well but no luck, same issue.
Hey @MuhammadFaizan980. 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!
Hi @MuhammadFaizan980 Did you figure it out? I have your same problem here with firebase_messaging: 9.1.1
Hi @MuhammadFaizan980 Did you figure it out? I have your same problem here with firebase_messaging: 9.1.1
![]()
Yes I did, I used shared preferences in combination with the FCM plugin and it works fine. I store FCM IDs into shared preferences and check every time if the notiication ID already exists in the prefs or not, if it does, do not take any action. Following is the code:
void _handleNotificationsListeners() {
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
// works fine |Y|
if (message != null) Utils.showToast('onMessageOpenedApp');
});
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) async {
// fixed, working with a minor tweak |Y|
if (message != null &&
(await Utils.getFcmId(message.messageId) == null)) {
Utils.showToast('getInitialMessage');
Utils.setFcmId(message.messageId);
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// works fine |Y|
if (message != null) Utils.showToast('onMessage');
});
}
And following functions are used to set and get FCM IDs:
static Future<void> setFcmId(String fcmId) async {
var pref = await SharedPreferences.getInstance();
await pref.setString(fcmId, fcmId);
}
static Future<String> getFcmId(String fcmId) async {
return (await SharedPreferences.getInstance()).getString(fcmId) ?? null;
}
Thank you very much !!
Hi. Method getInitialMessage() return null and FirebaseMessaging.onMessageOpenedApp not working when I clicked on push notification. How to fix it? Do you have any ideas to fix it?
Hi. Method getInitialMessage() return null and FirebaseMessaging.onMessageOpenedApp not working when I clicked on push notification. How to fix it? Do you have any ideas to fix it?
Hi, take a look at the official example https://pub.dev/packages/firebase_messaging/example For me the only problem was the .getInitialMessage() being called multiple times, but you can solve this using Shared Preferences as shown above
Hi. Method getInitialMessage() return null and FirebaseMessaging.onMessageOpenedApp not working when I clicked on push notification. How to fix it? Do you have any ideas to fix it?
If you are not receiving any notification at all, even if the app is in the background, add the firebase messagin dependency in the app level build.gradle file and it will work fine
i am not getting data while opening app from the notification click RemoteMessage is giving null.