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.51k stars 3.92k forks source link

πŸ› [firebase_messaging] onBackgroundMessage is not called when a lot of notifications come in after killing the app [IOS-Android] #9667

Closed baraaaljabban closed 1 year ago

baraaaljabban commented 1 year ago

Bug report

Describe the bug the code inside the method of onBackgroundMessage is not being executed when the the mobile receives for example from 15 to 30 notifications after the app is killed, in this case you will see all the notifications in the tray, but onBackgroundMessage will not be triggered for each one of the notifications ( if you try to handle the notification message in anyway then not all of them are handled).

for example the server (using FCM service) sends 30 notifications in a raw with the messages from [1-30], and onBackgroundMessage you will be called for [1,4,6...30] so 2,3,5 will not be handled but you still can see it in the tray of the mobile (this is for example you might miss other numbers but most likely it will be from the first 10 notifications). this is happening for both IOS and Android

Steps to reproduce

Steps to reproduce the behavior:

  1. kill the app and wait for about 3 minutes.
  2. send about 15 to 30 notifications to your mobile.
  3. you can see all the notifications in the tray of the mobile phone
  4. add some logic in your onBackgroundMessage method and check if can handle all of the notifications
  5. for testing try to report to crashlytics ( inside onBackgroundMessage) and then check if you can see all the notifications are handled or not. ( you can try to save them locally and see the result as well)
  6. you will see that some notifications are missing and reset are reported to crashlytics

Expected behavior

all the notifications that come to the app should be handled if you send 15 then the 15 one should be handled

Sample project

Future<void> mainCommon(AppEnvironment env) async {
  WidgetsFlutterBinding.ensureInitialized();
  Isolate.current.addErrorListener(
    RawReceivePort(
      (pair) async {
        final List<dynamic> errorAndStacktrace = pair;
        await _reportError(errorAndStacktrace.first, errorAndStacktrace.last);
      },
    ).sendPort,
  );

  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
  await slInit();
  // rest of the code .......
}

Future<void> onBackgroundMessage(RemoteMessage message) async {
  await Firebase.initializeApp();
  FirebaseCrashlytics.instance.recordError(message, null);
}

Additional context

in my case I was saving the niotfications in the localDB then when I noticied that some notificaitons are missing

 @pragma('vm:entry-point')
Future<void> onBackgroundMessage(RemoteMessage message) async {
  await Firebase.initializeApp();
  late NotificationService _service;

  try {
    _service = sl<NotificationService>();
  } catch (e) {
    await sl.reset();
    await slInit();
    _service = sl<NotificationService>();
  }
  //save notifications in localDB
  _service.gotBackgroundNotification(message, syncServer: false);
}

Notification payload

{
  data: {
    data: '{"notificationType":"MESSAGE:INSERT","channelId":"63243103a4c87100078d8996","channelTitle":"Bears","userId":"632430fbe99b9900078c4701","userName":"Alice Owlman","messageId":"632428095723cb1d55efe8f5","message":"we need to have it","attachments":[],"date":"2022-09-16 08:17:09.096Z","workspaceId":"632427b7e99b9900078c44f5","isComplete":true}',
  },
  notification: {
    title: 'Alice Owlman',
    body: 'we need to have it',
    imageUrl: undefined,
  },
  tokens: ['XXXXXX', 'YYYYYYY'],
  android: {
    priority: 'high',
  },
  apns: {
    payload: {
      aps: {
        contentAvailable: true,
      },
    },
  },
}

Flutter doctor

Click To Expand ``` flutter doctor [√] Flutter (Channel stable, 3.3.3, on Microsoft Windows [Version 10.0.22623.730], locale en-US) [√] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc2) [√] Chrome - develop for the web [!] Visual Studio - develop for Windows (Visual Studio Community 2022 17.1.3) ! Unable to locate a Windows 10 SDK. If building fails, install the Windows 10 SDK in Visual Studio. [√] Android Studio (version 2021.1) [√] VS Code (version 1.70.0) [√] Connected device (3 available) [√] HTTP Host Availability ! Doctor found issues in 1 category. ```

Flutter dependencies

Click To Expand ``` flutter pub deps -- --style=compact Dart SDK 2.18.2 Flutter SDK 3.3.3 chatworld_app 1.0.0+1 dependencies: - any_link_preview 2.0.9 [flutter html http shared_preferences string_validator url_launcher] - bloc_test 9.1.0 [bloc diff_match_patch meta mocktail test] - cached_network_image 3.2.2 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web] - connectivity_plus 2.3.7 [flutter connectivity_plus_platform_interface connectivity_plus_linux connectivity_plus_macos connectivity_plus_web connectivity_plus_windows] - cupertino_icons 1.0.5 - dartz 0.10.1 - device_info_plus 4.1.2 [flutter device_info_plus_platform_interface device_info_plus_macos device_info_plus_linux device_info_plus_web device_info_plus_windows] - emoji_picker_flutter 1.3.1 [flutter shared_preferences] - equatable 2.0.5 [collection meta] - event_bus 2.0.0 - extended_image 6.3.1 [extended_image_library flutter meta] - firebase_analytics 9.3.6 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter] - firebase_core 1.23.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 2.8.11 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_messaging 13.0.3 [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] - flutter_bloc 8.1.1 [flutter bloc provider] - flutter_keyboard_visibility 5.3.0 [meta flutter_keyboard_visibility_platform_interface flutter_keyboard_visibility_web flutter] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math] - flutter_markdown 0.6.12 [flutter markdown meta path] - flutter_portal 1.1.1 [collection flutter vector_math] - flutter_secure_storage 6.0.0 [flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter_secure_storage_web flutter_secure_storage_windows meta] - flutter_spinkit 5.1.0 [flutter] - flutter_staggered_grid_view 0.6.2 [flutter] - flutter_svg 1.1.5 [flutter meta path_drawing vector_math xml] - get_it 7.2.0 [async collection] - http 0.13.5 [async http_parser meta path] - internet_connection_checker 1.0.0+1 - intl 0.17.0 [clock path] - markdown 6.0.1 [args meta] - objectbox 1.6.2 [collection flat_buffers ffi meta path] - objectbox_flutter_libs 1.6.2 [objectbox path_provider] - path_provider 2.0.11 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows] - photo_view 0.14.0 [flutter] - queue 3.1.0+1 - quiver 3.1.0 [matcher] - rxdart 0.27.5 - shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - sticky_grouped_list 3.1.0 [flutter scrollable_positioned_list meta] - swipe_to 1.0.2 [flutter] - url_launcher 6.1.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] dev dependencies: - build_runner 2.2.1 [args async analyzer build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - flutter_lints 2.0.1 [lints] - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph] - mocktail 0.3.0 [collection matcher test] - objectbox_generator 1.6.2 [objectbox analyzer build collection dart_style glob path source_gen pubspec_parse yaml] transitive dependencies: - _fe_analyzer_shared 47.0.0 [meta] - analyzer 4.7.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - args 2.3.1 - async 2.9.0 [collection meta] - bloc 8.1.0 [meta] - boolean_selector 2.1.0 [source_span string_scanner] - build 2.3.1 [analyzer async convert crypto glob logging meta path] - build_config 1.1.0 [checked_yaml json_annotation path pubspec_parse yaml] - build_daemon 3.1.0 [built_collection built_value http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.0.10 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml] - build_runner_core 7.2.4 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta path package_config pool timing watcher yaml] - built_collection 5.1.1 - built_value 8.4.1 [built_collection collection fixnum meta] - cached_network_image_platform_interface 2.0.0 [flutter flutter_cache_manager] - cached_network_image_web 1.0.2 [flutter flutter_cache_manager cached_network_image_platform_interface] - characters 1.2.1 - checked_yaml 2.0.1 [json_annotation source_span yaml] - clock 1.1.1 - code_builder 4.3.0 [built_collection built_value collection matcher meta] - collection 1.16.0 - connectivity_plus_linux 1.3.1 [flutter connectivity_plus_platform_interface meta nm] - connectivity_plus_macos 1.2.4 [connectivity_plus_platform_interface flutter] - connectivity_plus_platform_interface 1.2.1 [flutter meta plugin_platform_interface] - connectivity_plus_web 1.2.3 [connectivity_plus_platform_interface flutter_web_plugins flutter] - connectivity_plus_windows 1.2.2 [connectivity_plus_platform_interface flutter] - convert 3.0.2 [typed_data] - coverage 1.6.1 [args logging package_config path source_maps stack_trace vm_service] - crypto 3.0.2 [typed_data] - csslib 0.17.2 [source_span] - dart_style 2.2.4 [analyzer args path pub_semver source_span] - dbus 0.7.8 [args ffi meta xml] - device_info_plus_linux 3.0.0 [device_info_plus_platform_interface file flutter meta] - device_info_plus_macos 3.0.0 [device_info_plus_platform_interface flutter] - device_info_plus_platform_interface 3.0.0 [flutter meta plugin_platform_interface] - device_info_plus_web 3.0.0 [device_info_plus_platform_interface flutter_web_plugins flutter] - device_info_plus_windows 4.1.0 [device_info_plus_platform_interface ffi flutter win32] - diff_match_patch 0.4.1 - extended_image_library 3.4.0 [crypto flutter http_client_helper path path_provider] - fake_async 1.3.1 [clock collection] - ffi 2.0.1 - file 6.1.4 [meta path] - firebase_analytics_platform_interface 3.3.5 [firebase_core flutter meta plugin_platform_interface] - firebase_analytics_web 0.4.2+5 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_core_platform_interface 4.5.1 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 1.7.2 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.2.17 [collection firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 4.1.5 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 3.1.5 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - fixnum 1.0.1 - flat_buffers 2.0.5 - flutter_blurhash 0.7.0 [flutter] - flutter_cache_manager 3.3.0 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid] - flutter_keyboard_visibility_platform_interface 2.0.0 [flutter meta plugin_platform_interface] - flutter_keyboard_visibility_web 2.0.0 [flutter_keyboard_visibility_platform_interface flutter_web_plugins flutter] - flutter_secure_storage_linux 1.1.1 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_macos 1.1.1 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_platform_interface 1.0.0 [flutter plugin_platform_interface] - flutter_secure_storage_web 1.0.2 [flutter flutter_web_plugins flutter_secure_storage_platform_interface js] - flutter_secure_storage_windows 1.1.2 [flutter flutter_secure_storage_platform_interface] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - frontend_server_client 2.1.3 [async path] - glob 2.1.0 [async collection file path string_scanner] - graphs 2.1.0 [collection] - html 0.15.0 [csslib source_span] - http_client_helper 2.0.3 [http] - http_multi_server 3.2.1 [async] - http_parser 4.0.1 [collection source_span string_scanner typed_data] - io 1.0.3 [meta path string_scanner] - js 0.6.4 - json_annotation 4.7.0 [meta] - lints 2.0.0 - logging 1.0.2 - matcher 0.12.12 [stack_trace] - material_color_utilities 0.1.5 - meta 1.8.0 - mime 1.0.2 - nested 1.0.0 [flutter] - nm 0.5.0 [dbus] - node_preamble 2.0.1 - octo_image 1.0.2 [flutter flutter_blurhash] - package_config 2.1.0 [path] - path 1.8.2 - path_drawing 1.0.1 [vector_math meta path_parsing flutter] - path_parsing 1.0.1 [vector_math meta] - path_provider_android 2.0.20 [flutter path_provider_platform_interface] - path_provider_ios 2.0.11 [flutter path_provider_platform_interface] - path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_macos 2.0.6 [flutter path_provider_platform_interface] - path_provider_platform_interface 2.0.4 [flutter platform plugin_platform_interface] - path_provider_windows 2.1.3 [ffi flutter path path_provider_platform_interface win32] - pedantic 1.11.1 - petitparser 5.0.0 [meta] - platform 3.1.0 - plugin_platform_interface 2.1.3 [meta] - pool 1.5.1 [async stack_trace] - process 4.2.4 [file path platform] - provider 6.0.3 [collection flutter nested] - pub_semver 2.1.1 [collection meta] - pubspec_parse 1.2.1 [checked_yaml collection json_annotation pub_semver yaml] - scrollable_positioned_list 0.3.4 [flutter collection] - shared_preferences_android 2.0.13 [flutter shared_preferences_platform_interface] - shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface] - shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface] - shared_preferences_platform_interface 2.1.0 [flutter plugin_platform_interface] - shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface] - shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - shelf 1.4.0 [async collection http_parser path stack_trace stream_channel] - shelf_packages_handler 3.0.1 [path shelf shelf_static] - shelf_static 1.1.1 [convert http_parser mime path shelf] - typed_data 1.3.1 [collection] - url_launcher_android 6.0.19 [flutter url_launcher_platform_interface] - url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface] - url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface] - url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.1.0 [flutter plugin_platform_interface] - url_launcher_web 2.0.13 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface] - uuid 3.0.6 [crypto] - vector_math 2.1.2 - vm_service 9.4.0 - watcher 1.0.1 [async path] - web_socket_channel 2.2.0 [async crypto stream_channel] - webkit_inspection_protocol 1.2.0 [logging] - win32 3.0.0 [ffi] - xdg_directories 0.2.0+2 [meta path process]- xml 6.1.0 [collection meta petitparser] - yaml 3.1.1 [collection source_span string_scanner] ```

FDhunterz commented 1 year ago

same here im waiting for it,

im using appbundle but its delayed notification, notification showing when i open apps

SyedUmerHasan commented 1 year ago

Same I am waiting for this issue to be resolved...

twdhruvlokapure commented 1 year ago

Hi, eagerly waiting for this one! Thank you πŸ™πŸ½

shubh-damodar commented 1 year ago

Same here. Any new update from firebase?

vaibhavkhandelwal7 commented 1 year ago

Same here. Anyone found a fix yet?

FDhunterz commented 1 year ago

Same here. Anyone found a fix yet?

back to old version, thats work

darshankawar commented 1 year ago

@baraaaljabban Looking at the code, I am assuming you are using IsolateNameServer class. When you are doing so, when the app is not in the foreground, are you removing the registered port ? Then in background isolate check if it exists first and handle the method, something like:

`_foregroundReceivePort!.listen((message) {
_fcmMessageHandlerForeground(message);
}

Also, does the same behavior occur without using IsolateNameServer logic ?

in my case I was saving the niotfications in the localDB then when I noticied that some notificaitons are missing

Does the same behavior occur without this part ? ie, without localDB implementation and usage ?

I am trying to narrow down external factors you have mentioned, so as to see if the behavior is solely occuring using plugin and handler method.

darshankawar commented 1 year ago

For rest of the comments saying facing same issue, or are there any updates, please elaborate on the use case and specific issue you are facing. Just commenting as above will not help much to properly analyze the issue.

If you are seeing the issue on iOS for onBackgroundMessage handler, please see this issue comment and use the template specified so that we can help in you proper way.

baraaaljabban commented 1 year ago

@darshankawar last time in this issue we talked about the Isolate and tried with that solution suggested, is it the same thing or you want me to try different thing? and just to answer that question yes it's happening in both cases

darshankawar commented 1 year ago

last time in this issue we talked about the Isolate and tried with that solution suggested, is it the same thing or you want me to try different thing?

Yes, I am aware of that conversation, but I wanted to know if using IsolateNameServer, is there any conflict with firebase_messaging that might be causing the behavior you are seeing.

It would be great if you could provide a complete demo app that we can clone and run at our end, so as to properly go through this behavior.

baraaaljabban commented 1 year ago

Alright I'll do the demo of what's happening in the next day, please allow some delay @darshankawar

baraaaljabban commented 1 year ago

Hi @darshankawar sorry for the late demo, but you can see that after killing the app, the notifications 2,3 are missing (not being saved in the local and the rest were okay and being saved) (so the sequance was like 1,4..9 and for known reason I don't know why some was not saved) but we can see all the notifications in the tray please let me know if something more is needed video (1).webm

darshankawar commented 1 year ago

Thanks for these details. Couple of things:

  1. Video shows you testing on emulator which may not correctly represent an actual state as that running on a physical device, so please confirm if same behavior occurs using a physical device too.
  2. A complete replicable code sample that we can directly use would be of great help.
baraaaljabban commented 1 year ago

for the first point yes it's happening for all real devices that we have in the company, for the second point I'll try to create a repo and add the logic there share the link here. but not sure when I can deliver that one hehe

google-oss-bot commented 1 year ago

Hey @baraaaljabban. 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!

baraaaljabban commented 1 year ago

Hi @darshankawar! sorry for the delay in response, however, please find the repo to the background notification & this one we use to send the notification from. you will notice that (most likely) the missing notifications will be in the top 10 notifications that you send. just a small reminder that in order to repeat this issue you need to send like 15 notification in a raw to be able to replicate it. thanks.

danagbemava-nc commented 1 year ago

Hi @shubh66, some updates have been made across a lot of the packages. Can you upgrade to the latest versions of all the dependencies to see if you still experience the issue?

baraaaljabban commented 1 year ago

I got that this issue is blocked: customer-response Waiting for customer response, but I don't have much to add

danagbemava-nc commented 1 year ago

I can reproduce the bug using the repo and API provided in https://github.com/firebase/flutterfire/issues/9667#issuecomment-1289831543.

I ran the app several times and I got varying results each time. A few have been put below.

screenshots
baraaaljabban commented 1 year ago

Hello @danagbemava-nc, may I ask please about the update on this issue, or when I can expect a fix for it. Thanks.

baraaaljabban commented 1 year ago

@darshankawar can you please help me a bit to give me some glimpse about this issue update's or when I can expect this issue to be addressed by the team, thanks!.

danagbemava-nc commented 1 year ago

/cc @russellwheatley

baraaaljabban commented 1 year ago

Hello @danagbemava-nc, is there anything regarding this issue please!

baraaaljabban commented 1 year ago

Hello there! any update for this issue thanks. @danagbemava-nc @darshankawar

danagbemava-nc commented 1 year ago

Hi @shubh66, there are no updates atm

russellwheatley commented 1 year ago

iOS background message handler will never be called when the app is in a terminated state. See documentation. If the user also quits the app on android, the behaviour is the same.

On iOS, if the user swipes away the application from the app switcher, it must be manually reopened for background messages to start working again. On Android, if the user force-quits the app from device settings, it must be manually reopened for messages to start working.

As far as reliability goes, I'm afraid that is the nature of messages. They won't always make it to the device. On top of that, the operating system (iOS and android) will throttle your messages if you send a lot to a device. There is nothing we can do about this except ask that you design your app with these considerations in mind. Thanks.