flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [firebase_messaging] onBackgroundMessage is not invoked when user switches to different app

Open manas-raj-shrestha opened this issue 3 years ago • 8 comments

Bug report

Describe the bug I am facing this weird issue only in iOS where the onBackgroundMessage is not called if the user switches to a different app. onBackgroundMessage works perfectly fine when my app is in background and it's the last app that they used. If the user opens any other app, onBackgroundMessage is invoked for the first 2-3 times then is not invoked at all.

Background Fetch, Background Processing and Remote Notifications are enabled in capabilities.

Steps to reproduce

Steps to reproduce the behavior:

  1. Send the notification with app in background
  2. Observer the logs, onBackgroundMessage should be called properly
  3. Send the notification after opening any other application
  4. onBackgroundMessage is called few time and stop invoking after few calls

Expected behavior

onBackgroundMessage should be called when the app is in background irrespective of where in the app switcher stack it is located.

Sample project

iOS version: 15.6.1 firebase_messaging: ^12.0.0

Here's how my main function looks like

void main() async {
 
  var configuredApp = const AppConfig(
      appTitle: 'App-Dev',
      buildFlavor: "Development",
      widget: AppWrapper());
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  
  runZonedGuarded(() {
    runApp(configuredApp);
  }, FirebaseCrashlytics.instance.recordError);
}

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  debugPrint("Handling a background message: ${message.messageId}");
}

Notification payload

{
     "condition": "'channel_prefecture_1' in topics || 'channel_prefecture_9' in topics",
    "notification":{
      "title":"Hey",
      "body":"Testing",
    },
    "data" : {
    "type": "LOST_FOUND",
    },
    "content_available": true,
    "priority": "high"
}

Flutter doctor

[✓] Flutter (Channel stable, 3.0.4, on macOS 12.0.1 21A559 darwin-arm, locale en-NP)
[!] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[!] Android Studio
    ✗ Unable to find bundled Java version.
[✓] VS Code (version 1.70.2)
[✓] Connected device (2 available)
[✓] HTTP Host Availability


Flutter dependencies

Click To Expand
- app_tracking_transparency 2.0.2+4 [flutter]
- badges 2.0.3 [flutter]
- cached_network_image 3.2.1 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web]
- camera 0.9.8+1 [camera_android camera_avfoundation camera_platform_interface camera_web flutter flutter_plugin_android_lifecycle quiver]
- chewie 1.3.5 [cupertino_icons flutter provider video_player wakelock]
- cloud_firestore 3.4.7 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- cupertino_icons 1.0.5
- dio 4.0.6 [http_parser path]
- dots_indicator 2.1.0 [flutter]
- firebase_analytics 9.3.4 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_auth 3.8.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 1.22.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 2.8.10 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_messaging 12.0.3 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- firebase_remote_config 2.0.17 [firebase_core firebase_core_platform_interface firebase_remote_config_platform_interface firebase_remote_config_web flutter]
- firebase_storage 10.3.8 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_app_badger 1.4.0 [flutter]
- flutter_bloc 8.1.1 [flutter bloc provider]
- flutter_blurhash 0.6.8 [flutter]
- flutter_cache_manager 3.3.0 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid]
- flutter_colorpicker 1.0.3 [flutter]
- flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math]
- flutter_screenutil 5.5.4 [flutter]
- flutter_switch 0.3.2 [flutter]
- flutter_video_info 1.2.0 [flutter path]
- get_it 7.2.0 [async collection]
- image_picker 0.8.5+3 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface]
- intl 0.17.0 [clock path]
- mime 1.0.2
- mobile_scanner 2.0.0 [flutter flutter_web_plugins js]
- package_info_plus 1.4.3+1 [flutter package_info_plus_platform_interface package_info_plus_linux package_info_plus_macos package_info_plus_windows package_info_plus_web]
- 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]
- provider 6.0.3 [collection flutter nested]
- qr_flutter 4.0.0 [flutter qr]
- share_plus 4.1.0 [meta mime flutter share_plus_platform_interface share_plus_linux share_plus_macos share_plus_windows share_plus_web]
- 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]
- 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]
- uuid 3.0.6 [crypto]
- video_compress 3.1.0 [flutter]
- video_player 2.4.7 [flutter html video_player_android video_player_avfoundation video_player_platform_interface video_player_web]

dev dependencies:
- flutter_launcher_icons 0.9.3 [args image path yaml]
- flutter_lints 1.0.4 [lints]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]

transitive dependencies:
- archive 3.3.1 [crypto path]
- args 2.3.1
- async 2.8.2 [collection meta]
- bloc 8.1.0 [meta]
- boolean_selector 2.1.0 [source_span string_scanner]
- cached_network_image_platform_interface 1.0.0 [flutter flutter_cache_manager]
- cached_network_image_web 1.0.1 [flutter flutter_cache_manager cached_network_image_platform_interface]
- camera_android 0.9.8+3 [camera_platform_interface flutter flutter_plugin_android_lifecycle stream_transform]
- camera_avfoundation 0.9.8+5 [camera_platform_interface flutter stream_transform]
- camera_platform_interface 2.2.0 [cross_file flutter plugin_platform_interface stream_transform]
- camera_web 0.2.1+6 [camera_platform_interface flutter flutter_web_plugins stream_transform]
- characters 1.2.0
- charcode 1.3.1
- clock 1.1.0
- cloud_firestore_platform_interface 5.7.4 [collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 2.8.7 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- collection 1.16.0
- cross_file 0.3.3+1 [js meta]
- crypto 3.0.2 [typed_data]
- csslib 0.17.2 [source_span]
- fake_async 1.3.0 [clock collection]
- ffi 2.0.1
- file 6.1.4 [meta path]
- firebase_analytics_platform_interface 3.3.4 [firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.4.2+4 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_auth_platform_interface 6.7.0 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 4.4.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta]
- 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.16 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_platform_interface 4.1.4 [firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.1.4 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- firebase_remote_config_platform_interface 1.1.16 [firebase_core flutter meta plugin_platform_interface]
- firebase_remote_config_web 1.1.5 [firebase_core firebase_core_web firebase_remote_config_platform_interface flutter flutter_web_plugins js]
- firebase_storage_platform_interface 4.1.16 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_storage_web 3.3.6 [async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta]
- flutter_plugin_android_lifecycle 2.0.7 [flutter]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- html 0.15.0 [csslib source_span]
- http 0.13.5 [async http_parser meta path]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- image 3.2.0 [archive meta xml]
- image_picker_android 0.8.5+3 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface]
- image_picker_for_web 2.1.8 [flutter flutter_web_plugins image_picker_platform_interface]
- image_picker_ios 0.8.6 [flutter image_picker_platform_interface]
- image_picker_platform_interface 2.6.1 [cross_file flutter http plugin_platform_interface]
- js 0.6.4
- lints 1.0.1
- matcher 0.12.11 [stack_trace]
- material_color_utilities 0.1.4
- meta 1.7.0
- nested 1.0.0 [flutter]
- octo_image 1.0.2 [flutter flutter_blurhash]
- package_info_plus_linux 1.0.5 [package_info_plus_platform_interface flutter path]
- package_info_plus_macos 1.3.0 [flutter]
- package_info_plus_platform_interface 1.0.2 [flutter meta plugin_platform_interface]
- package_info_plus_web 1.0.5 [flutter flutter_web_plugins http meta package_info_plus_platform_interface]
- package_info_plus_windows 2.1.0 [package_info_plus_platform_interface ffi flutter win32]
- path 1.8.1
- 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.2 [meta]
- process 4.2.4 [file path platform]
- qr 2.1.0 [meta]
- quiver 3.1.0 [matcher]
- rxdart 0.27.5
- share_plus_linux 3.0.0 [share_plus_platform_interface file flutter meta url_launcher]
- share_plus_macos 3.0.1 [share_plus_platform_interface flutter]
- share_plus_platform_interface 3.0.3 [flutter meta mime plugin_platform_interface]
- share_plus_web 3.0.1 [share_plus_platform_interface url_launcher flutter flutter_web_plugins meta]
- share_plus_windows 3.0.1 [share_plus_platform_interface flutter meta url_launcher]
- 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]
- sky_engine 0.0.99
- source_span 1.8.2 [collection path term_glyph]
- sqflite 2.0.3+1 [flutter sqflite_common path]
- sqflite_common 2.2.1+1 [synchronized path meta]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- stream_transform 2.0.0
- string_scanner 1.1.0 [charcode source_span]
- synchronized 3.0.0+3
- term_glyph 1.2.0
- test_api 0.4.9 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.1 [collection]
- url_launcher_android 6.0.18 [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]
- vector_math 2.1.2
- video_player_android 2.3.9 [flutter video_player_platform_interface]
- video_player_avfoundation 2.3.5 [flutter video_player_platform_interface]
- video_player_platform_interface 5.1.4 [flutter plugin_platform_interface]
- video_player_web 2.0.12 [flutter flutter_web_plugins video_player_platform_interface]
- wakelock 0.6.2 [flutter meta wakelock_macos wakelock_platform_interface wakelock_web wakelock_windows]
- wakelock_macos 0.4.0 [flutter flutter_web_plugins wakelock_platform_interface]
- wakelock_platform_interface 0.3.0 [flutter meta]
- wakelock_web 0.4.0 [flutter flutter_web_plugins js wakelock_platform_interface]
- wakelock_windows 0.2.0 [flutter wakelock_platform_interface win32]
- win32 2.7.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]

manas-raj-shrestha avatar Sep 14 '22 15:09 manas-raj-shrestha

Thanks for the report @manas-raj-shrestha I verified this scenario using plugin's example on iPhone 6s device (OS 15.3.1) with latest master version of the framework and observed that onBackgrounMessage handler works as expected when app is in background and we open another app, send notification, which is received everytime it is triggered.

See below video for your reference:

https://user-images.githubusercontent.com/67046386/190340296-bed2c203-1a60-499f-a361-31a54dde48c5.MP4

The handler is triggered and logged everytime in the console as below:

flutter: A new onMessage event was published!
flutter: Handling a background message 1663225956300870
flutter: Handling a background message 1663225981676988
[BackgroundTask] Background Task 28 ("Flutter debug task"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.
flutter: Handling a background message 1663225990984734
flutter: A new onMessage event was published!
flutter: Handling a background message 1663226041647053
flutter: Handling a background message 1663226050724398
flutter: A new onMessage event was published!
flutter: Handling a background message 1663226064961826

I suggest you to try the same plugin example at your end and see using it, you get same behavior or not. Since the handler is triggered properly, it indicates that the messaging api is working as expected and in your case, it might be related to a config or something related to outside of scope of the plugin.

darshankawar avatar Sep 15 '22 07:09 darshankawar

@darshankawar I tried using the latest version 13.0.1, the issue is still there for me.

manas-raj-shrestha avatar Sep 15 '22 10:09 manas-raj-shrestha

I tried with same plugin version, but have not noticed the behavior reported. The handler is invoked properly when I switch to different app.

flutter: A new onMessage event was published!
flutter: Handling a background message 1663238375161672
flutter: Handling a background message 1663238383770306
flutter: Handling a background message 1663238389933648
flutter: Handling a background message 1663238394325778
flutter: Handling a background message 1663238398539849
[BackgroundTask] Background Task 21 ("Flutter debug task"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.
flutter: Handling a background message 1663238412130084
flutter: Handling a background message 1663238438498088

Can you provide a short video and logs that shows the behavior ?

darshankawar avatar Sep 15 '22 11:09 darshankawar

@darshankawar

In this test session, it took about 6 mins for this bug to re occur. For 6 mins notification was shown and onBackground was also called, but after 6 mins, only the notification was shown.

In the video, there are 4 notifications: 1 - 00:05 [Notification shown, onBackgroundMessage logged] 2 - 00:32 [Notification shown, onBackgroundMessage logged] 3 - 00:54 [Only Notification shown] 4 - 01:01 [Only Notification shown]

https://user-images.githubusercontent.com/11496664/190446927-e4d1c82a-43ab-42b9-b261-f9bed383d0b4.mov

manas-raj-shrestha avatar Sep 15 '22 15:09 manas-raj-shrestha

Thanks for these details. I am unable to replicate this behavior using the plugin example and also per my earlier verification, keeping this issue open for further insights from the team.

/cc @russellwheatley

darshankawar avatar Sep 16 '22 05:09 darshankawar

@russellwheatley Hello Russell, saw this issue is more appropriate to share our situation.

For us, data-only notifications work under all conditions/platforms but iOS when “app is terminated” and “user switches to another app” (see vid below).

https://user-images.githubusercontent.com/83366073/190661758-5a641a24-2bcf-41f7-811a-a90b66234e17.MOV

Basically, notifications work when app is killed initially but onbackgroundMessage gets no longer invoked after we switch to another app.

We are testing with your example code, ios 15.6.1 and flutter 3.05.

We would also love to know if “remote notifications + mutable-content:1 + app service extention” feature will land anytime soon. Infact, even if we get data-only notifications working on iOS, they should not be used for a messaging app (due to the many restrictions that Apple impose to them).

Any help and clarification on this would be much appreciated.

iosephmagno avatar Sep 16 '22 14:09 iosephmagno

Forgot to mention, in our case notifications stop to work as soon as we open a different app (like shown in our vid). We have never experienced them working for some time as @manas-raj-shrestha has.

iosephmagno avatar Sep 16 '22 19:09 iosephmagno

i have good news it works in the native side (swift) there are bug with firebase_messageing package #9563

hatemragab avatar Sep 18 '22 18:09 hatemragab

Just reporting that #9581 is not fixing this. A further investigation is wip.

iosephmagno avatar Sep 22 '22 06:09 iosephmagno

I'm facing the same issue. It's really annoying. Hope new updates come soon.

duykhanhhs1 avatar Sep 27 '22 03:09 duykhanhhs1

Hello everyone, we’ve reviewed the issues opened for iOS background messages and we want to make our position clear.

Android and iOS handle background messaging differently. The decision made by the iOS operating system whether messages reaches the relevant iOS event handler (and subsequently received by the Dart background messaging handler) is based on a number of criteria, such as: CPU usage, priority level (data-only messages are considered “low priority” by iOS, android does not), battery level, amount of messages being received by the app, background/terminated application state, etc. This is a fundamental difference between the platforms, and you need to be aware of them when designing your application.

One solution suggested for iOS data-only messages unreliability has been to add a NotificationExtension. But this will still not create parity with Android as you will still have to include a notification with your message (therefore not data-only). It will still render the original notification if you do not update/mutate the notification that comes through the system in a timely manner. We are not currently considering this as a solution due to these limitations.

We also will not support non-FCM messages from third party packages. We specifically only support messages received from the Firebase APIs since we cannot guarantee that messages received from third party packages will not have any unintended side-effects on other Firebase products such as messaging delivery reporting and Analytics data.

To help us triage and locate genuine issues that need to be addressed we have created a specific issue template for iOS background messages. If you believe you still have an issue that needs to be addressed, please create a new issue following this template.

I will be closing this issue in favor of raising a new issue with the new template above. This template will help you provide us with all the information we need to investigate a potential issue with background messaging on iOS.

russellwheatley avatar Sep 27 '22 10:09 russellwheatley