Skip to content

🐛 [firebase_messaging] Latest foreground messages delivered again after app is closed and re-opened #12411

@timukasr

Description

@timukasr

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:

  • ifNoTokenAndNoSetOptions_thenNoMessages(); - no messages delivered at all
  • ifSetOptionsBefore_thenOk(); - hack to fix the issue
  • ifSetOptionsAfter_thenMessageRedelivered(); - hack does not work if called after onMessage.listen

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<void> 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<StatefulWidget> createState() => _Application();
}

class _Application extends State<Application> {
  String? _token;
  List<RemoteMessage> _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



Activity

darshankawar

darshankawar commented on Feb 29, 2024

@darshankawar

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 ?

added
blocked: customer-responseWaiting for customer response, e.g. more information was requested.
and removed
Needs AttentionThis issue needs maintainer attention.
on Feb 29, 2024
timukasr

timukasr commented on Feb 29, 2024

@timukasr
Author

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

added
Needs AttentionThis issue needs maintainer attention.
and removed
blocked: customer-responseWaiting for customer response, e.g. more information was requested.
on Feb 29, 2024
darshankawar

darshankawar commented on Feb 29, 2024

@darshankawar

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

added
blocked: customer-responseWaiting for customer response, e.g. more information was requested.
and removed
Needs AttentionThis issue needs maintainer attention.
on Feb 29, 2024
timukasr

timukasr commented on Feb 29, 2024

@timukasr
Author

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.

added
Needs AttentionThis issue needs maintainer attention.
and removed
blocked: customer-responseWaiting for customer response, e.g. more information was requested.
on Feb 29, 2024

13 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleIssue with no recent activityblocked: customer-responseWaiting for customer response, e.g. more information was requested.platform: androidIssues / PRs which are specifically for Android.plugin: messagingtriageIssue is currently being triaged.type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @timukasr@Lyokone@bqubique@russellwheatley@google-oss-bot

        Issue actions

          🐛 [firebase_messaging] Latest foreground messages delivered again after app is closed and re-opened · Issue #12411 · firebase/flutterfire