firebase / flutterfire

πŸ”₯ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.73k stars 3.98k forks source link

πŸ› [firebase_messaging] Latest foreground messages delivered again after app is closed and re-opened #12411

Open timukasr opened 9 months ago

timukasr commented 9 months ago

Bug report

Describe the bug Main issue is that if foreground message is delivered via FirebaseMessaging.onMessage.listen, Android app is closed by pressing "Back" and re-opened by pressing app icon, then onMessage.listen is called again with latest message. Accidentally noticed, that if i call await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(); before registering onMessage listener, then it works as expected (previous message is not delivered again). But I have Android app, so setForegroundNotificationPresentationOptions should not have this behavior. If this method is called after the onMessage listener is registered, then latest message is again re-delivered during app startup.

Also noticed that, if FirebaseMessaging.instance.getToken() is not called, then FirebaseMessaging.onMessage.listen is also never called (even if I have active token).

Steps to reproduce

Steps to reproduce the behavior:

  1. Change Firebase.initializeApp options in example app code.
  2. Run app using example code
  3. Send push message
  4. Notice that message is delivered in app
  5. Close app with "Back" button
  6. Open app using app icon
  7. Notice that "Messages" contain previous push message

Also check other behaviors in initState:

Expected behavior

"Messages" should be empty after app is opened

Sample project

Click To Expand ``` import 'dart:async'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'config.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: const FirebaseOptions( apiKey: Config.apiKey, appId: Config.appId, messagingSenderId: Config.messagingSenderId, projectId: Config.projectId, ), ); // await setupFlutterNotifications(); runApp(MessagingExampleApp()); } /// Entry point for the example application. class MessagingExampleApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Messaging Example App', theme: ThemeData.dark(), routes: { '/': (context) => Application(), }, ); } } /// Renders the example application. class Application extends StatefulWidget { @override State createState() => _Application(); } class _Application extends State { String? _token; List _messages = []; @override void initState() { super.initState(); ifGetToken_thenMessageRedelivered(); // ifNoTokenAndNoSetOptions_thenNoMessages(); // ifSetOptionsBefore_thenOk(); // ifSetOptionsAfter_thenMessageRedelivered(); } ifSetOptionsBefore_thenOk() async { await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(); FirebaseMessaging.onMessage.listen(onMessage); } ifSetOptionsAfter_thenMessageRedelivered() async { FirebaseMessaging.onMessage.listen(onMessage); await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(); } ifNoTokenAndNoSetOptions_thenNoMessages() { FirebaseMessaging.onMessage.listen(onMessage); } ifGetToken_thenMessageRedelivered() { FirebaseMessaging.instance.getToken().then(setToken); FirebaseMessaging.onMessage.listen(onMessage); } void setToken(String? token) { print('FCM Token: $token'); setState(() { _token = token; }); } void onMessage(RemoteMessage message) { print('--- got push message ${DateTime.now()} ${message.toMap()}'); setState(() { _messages.add(message); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Cloud Messaging'), ), body: SingleChildScrollView( child: Column( children: [ MetaCard('Push Messaging token', Text(_token ?? 'unavailable')), MetaCard( 'Messages', Column( children: _messages.map((message) { return Padding( padding: const EdgeInsets.all(8), child: Text( message.data.toString(), style: const TextStyle(fontSize: 12), ), ); }).toList(), ), ), ], ), ), ); } } /// UI Widget for displaying metadata. class MetaCard extends StatelessWidget { final String _title; final Widget _children; // ignore: public_member_api_docs MetaCard(this._title, this._children); @override Widget build(BuildContext context) { return Container( width: double.infinity, margin: const EdgeInsets.only(left: 8, right: 8, top: 8), child: Card( child: Padding( padding: const EdgeInsets.all(16), child: Column( children: [ Container( margin: const EdgeInsets.only(bottom: 16), child: Text(_title, style: const TextStyle(fontSize: 18)), ), _children, ], ), ), ), ); } } ```

Additional context


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.19.1, on Microsoft Windows [Version 10.0.19045.3996], locale et-EE) ! Warning: `flutter` on your path resolves to J:\fvm\3.19.1\bin\flutter, which is not inside your current Flutter SDK checkout at J:\fvm\default. Consider adding J:\fvm\default\bin to the front of your path. ! Warning: `dart` on your path resolves to J:\fvm\3.19.1\bin\dart, which is not inside your current Flutter SDK checkout at J:\fvm\default. Consider adding J:\fvm\default\bin to the front of your path. [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.2.3) [√] Android Studio (version 2023.1) [√] IntelliJ IDEA Ultimate Edition (version 2023.3) [√] VS Code (version 1.86.2) [√] Connected device (4 available) [√] Network resources ! Doctor found issues in 1 category. ```

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand ``` Dart SDK 3.3.0 Flutter SDK 3.19.1 flutter_fcm 1.0.0+1 dependencies: - cupertino_icons 1.0.6 - firebase_core 2.25.5 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_messaging 14.7.17 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] dev dependencies: - flutter_lints 3.0.1 [lints] - flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math leak_tracker_flutter_testing async boolean_selector characters collection leak_tracker leak_tracker_testing material_color_utilities meta source_span stream_channel string_scanner term_glyph vm_service] transitive dependencies: - _flutterfire_internals 1.3.23 [collection firebase_core firebase_core_platform_interface flutter meta] - async 2.11.0 [collection meta] - boolean_selector 2.1.1 [source_span string_scanner] - characters 1.3.0 - clock 1.1.1 - collection 1.18.0 - fake_async 1.3.1 [clock collection] - firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 2.11.5 [firebase_core_platform_interface flutter flutter_web_plugins js meta web] - firebase_messaging_platform_interface 4.5.25 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 3.6.6 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta web] - flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math] - js 0.6.7 [meta] - leak_tracker 10.0.0 [clock collection meta path vm_service] - leak_tracker_flutter_testing 2.0.1 [flutter leak_tracker leak_tracker_testing matcher meta] - leak_tracker_testing 2.0.1 [leak_tracker matcher meta] - lints 3.0.0 - matcher 0.12.16+1 [async meta stack_trace term_glyph test_api] - material_color_utilities 0.8.0 [collection] - meta 1.11.0 - path 1.9.0 - plugin_platform_interface 2.1.8 [meta] - sky_engine 0.0.99 - source_span 1.10.0 [collection path term_glyph] - stack_trace 1.11.1 [path] - stream_channel 2.1.2 [async] - string_scanner 1.2.0 [source_span] - term_glyph 1.2.1 - test_api 0.6.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph] - vector_math 2.1.4 - vm_service 13.0.0 - web 0.4.2 ```

darshankawar commented 9 months ago

Thanks for the report @timukasr How are you sending the push notifications ? Can you try by using the plugin example script that uses node js and see if using it you still get same behavior as reported ?

Also, is this behavior occuring on physical device or using emulator ?

timukasr commented 9 months ago

@darshankawar I tried with example script and I'm getting same behavior. I'm using physical device.

darshankawar commented 9 months ago

Please check if this is related to your case or not.

timukasr commented 9 months ago

Does not seem to be exactly related, but found out that if i call await FirebaseMessaging.instance.getInitialMessage(); before FirebaseMessaging.onMessage.listen(onMessage);, then everything works as expected (no old message during startup). If I get initial message after registering onMessage listener, then the issue occurs. Basically same thing as with await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions();. So it seems that something is messed up there.

bqubique commented 8 months ago

For me trying what @timukasr wrote above did not help. Same issue where the last message is re-delivered when app is opened from background or restarted.

class StubScreen extends StatefulWidget {
  const StubScreen({super.key});

  @override
  State<StubScreen> createState() => _StubScreenState();
}

class _StubScreenState extends State<StubScreen> {
  RemoteMessage? initialMessage;

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

  void checkForMessage() async {
    final notification = await FirebaseMessaging.instance.getInitialMessage();
    if (notification != null) {
      checkMessage(notification);
    }

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('A new onMessageOpenedApp event was published!');

      checkMessage(message);
    });
  }

  void checkMessage(RemoteMessage? message) {
    log('${message?.data.toString()}');
    if (message != null && message.data['KEY'] != null) {
      Navigator.push(...);
    }
  }

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

This is the most similar approach to the github implementation of this case.

  firebase_core: ^2.15.1
  firebase_messaging: ^14.7.9

Replicated on a physical device (Pixel 7 pro).

flutter doctor -v output:

Details

[βœ“] Flutter (Channel stable, 3.13.9, on macOS 14.0 23A344 darwin-arm64, locale en-GB) β€’ Flutter version 3.13.9 on channel stable at /Users/blinqipa/development/flutter β€’ Upstream repository https://github.com/flutter/flutter.git β€’ Framework revision d211f42860 (4 months ago), 2023-10-25 13:42:25 -0700 β€’ Engine revision 0545f8705d β€’ Dart version 3.1.5 β€’ DevTools version 2.25.0 [βœ“] Android toolchain - develop for Android devices (Android SDK version 33.0.1) β€’ Android SDK at /Users/blinqipa/Library/Android/sdk β€’ Platform android-34, build-tools 33.0.1 β€’ Java binary at: /Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin/java β€’ Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) β€’ All Android licenses accepted. [βœ“] Xcode - develop for iOS and macOS (Xcode 15.0) β€’ Xcode at /Applications/Xcode.app/Contents/Developer β€’ Build 15A240d β€’ CocoaPods version 1.14.3 [βœ“] Chrome - develop for the web β€’ Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio (version unknown) β€’ Android Studio at /Applications/Android Studio Preview.app/Contents β€’ 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 βœ— Unable to determine Android Studio version. β€’ Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) [βœ“] VS Code (version 1.86.2) β€’ VS Code at /Applications/Visual Studio Code.app/Contents β€’ Flutter extension version 3.84.0 [βœ“] Connected device (4 available) β€’ Pixel 7 Pro (mobile) β€’ 2A181FDH30064X β€’ android-arm64 β€’ Android 14 (API 34) β€’ Blin’s iPhone (mobile) β€’ 00008020-000B598A3C41402E β€’ ios β€’ iOS 17.4 21E217 β€’ macOS (desktop) β€’ macos β€’ darwin-arm64 β€’ macOS 14.0 23A344 darwin-arm64 β€’ Chrome (web) β€’ chrome β€’ web-javascript β€’ Google Chrome 122.0.6261.94 [βœ“] Network resources β€’ All expected network resources are available. ! Doctor found issues in 1 category.

Edit 2: Same issue persists with Flutter 3.19 and latest versions of firebase_core and firebase_messaging.

firebase_messaging: ^14.7.18
firebase_core: ^2.26.0
Updated flutter doctor -v output

[βœ“] Flutter (Channel stable, 3.19.2, on macOS 14.0 23A344 darwin-arm64, locale en-GB) β€’ Flutter version 3.19.2 on channel stable at /Users/blinqipa/development/flutter β€’ Upstream repository https://github.com/flutter/flutter.git β€’ Framework revision 7482962148 (8 days ago), 2024-02-27 16:51:22 -0500 β€’ Engine revision 04817c99c9 β€’ Dart version 3.3.0 β€’ DevTools version 2.31.1 [βœ“] Android toolchain - develop for Android devices (Android SDK version 33.0.1) β€’ Android SDK at /Users/blinqipa/Library/Android/sdk β€’ Platform android-34, build-tools 33.0.1 β€’ Java binary at: /Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin/java β€’ Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) β€’ All Android licenses accepted. [βœ“] Xcode - develop for iOS and macOS (Xcode 15.0) β€’ Xcode at /Applications/Xcode.app/Contents/Developer β€’ Build 15A240d β€’ CocoaPods version 1.14.3 [βœ“] Chrome - develop for the web β€’ Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio (version unknown) β€’ Android Studio at /Applications/Android Studio Preview.app/Contents β€’ 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 βœ— Unable to determine Android Studio version. β€’ Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) [βœ“] VS Code (version 1.86.2) β€’ VS Code at /Applications/Visual Studio Code.app/Contents β€’ Flutter extension version 3.84.0 [βœ“] Connected device (4 available) β€’ Pixel 7 Pro (mobile) β€’ 2A181FDH30064X β€’ android-arm64 β€’ Android 14 (API 34) β€’ Blin’s iPhone (mobile) β€’ 00008020-000B598A3C41402E β€’ ios β€’ iOS 17.4 21E217 β€’ macOS (desktop) β€’ macos β€’ darwin-arm64 β€’ macOS 14.0 23A344 darwin-arm64 β€’ Chrome (web) β€’ chrome β€’ web-javascript β€’ Google Chrome 122.0.6261.94 [βœ“] Network resources β€’ All expected network resources are available. ! Doctor found issues in 1 category.

kamranbekirovyz commented 2 weeks ago

I think I'm encountering the same issue: when app opened getInitialMessage returns and onMessageOpenedApp emits same RemoteMessage, causing user to be navigated to same route twice.

As far as I know, onMessageOpenedApp should only emit RemoteMessages only when app is not killed.