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.71k stars 3.97k forks source link

🐛 [firebase_messaging] Silent background push notifications not handled on Android #11843

Closed aaumond closed 10 months ago

aaumond commented 1 year ago

Bug report

Describe the bug In my Flutter application, I need to integrate silent push notifications on Android and iOS.

On iOS, I'm able to receive and process them via the FirebaseMessaging.onBackgroundMessage method.

On Android, however, I don't receive any notifications via the handler.

Is this feature supported for Android and iOS?

For your information, I use the following version of Firebase Messaging: firebase_messaging: 14.7.3

Steps to reproduce

  1. Have an application configured with the FirebaseMessaging.onBackgroundMessage handler.
  2. Send a silent push notification from a back-end.

Expected behavior

The notification must be intercepted by the background handler, and must be able to be processed.

Sample project

FirebaseMessaging.onBackgroundMessage(
      (RemoteMessage message) async => print(message),
    );

Additional context

The notification is sent by the backend as follows: image


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.10.6, on macOS 14.0 23A344 darwin-arm64, locale fr-FR) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2022.3) [✓] VS Code (version 1.83.1) [✓] Connected device (4 available) ! Error: Browsing on the local area network for iPhone Anthony. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) [✓] Network resources • No issues found! ```

Flutter dependencies

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

Click To Expand ``` Dart SDK 3.0.6 Flutter SDK 3.10.6 previa_et_moi 0.9.3+13 dependencies: - age_calculator 1.0.0 [flutter] - collection 1.18.0 - copy_with_extension 5.0.4 [meta] - dio 5.3.3 [async http_parser meta path] - firebase_analytics 10.6.3 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter] - firebase_core 2.21.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 3.4.3 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_messaging 14.7.3 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - flutter 0.0.0 [characters collection js material_color_utilities meta vector_math sky_engine] - flutter_file_dialog 3.0.2 [flutter] - flutter_local_notifications 16.1.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone] - flutter_localizations 0.0.0 [flutter intl characters clock collection js material_color_utilities meta path vector_math] - flutter_riverpod 2.4.5 [collection flutter meta riverpod state_notifier] - flutter_secure_storage 9.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_svg 2.0.9 [flutter vector_graphics vector_graphics_codec vector_graphics_compiler] - flutter_time_picker_spinner 2.0.0 [flutter] - flutter_widget_from_html 0.14.6 [flutter flutter_widget_from_html_core fwfh_cached_network_image fwfh_chewie fwfh_just_audio fwfh_svg fwfh_url_launcher fwfh_webview html] - go_router 12.0.0 [collection flutter flutter_web_plugins logging meta] - http_parser 4.0.2 [collection source_span string_scanner typed_data] - image_picker 1.0.4 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_linux image_picker_macos image_picker_platform_interface image_picker_windows] - intl 0.18.1 [clock meta path] - json_annotation 4.8.1 [meta] - jwt_decoder 2.0.1 - local_auth 2.1.7 [flutter local_auth_android local_auth_ios local_auth_platform_interface local_auth_windows] - maps_launcher 2.2.0 [flutter flutter_web_plugins url_launcher] - package_info_plus 4.2.0 [ffi flutter flutter_web_plugins http meta path package_info_plus_platform_interface win32] - path 1.8.3 - path_provider 2.1.1 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows] - pdf 3.10.4 [archive barcode bidi crypto image meta path_parsing vector_math xml] - pdfx 2.5.0 [flutter flutter_web_plugins plugin_platform_interface js device_info_plus uuid meta extension synchronized universal_platform photo_view vector_math] - permission_handler 11.0.1 [flutter meta permission_handler_android permission_handler_apple permission_handler_windows permission_handler_platform_interface] - reactive_forms 16.1.1 [flutter intl] - reactive_phone_form_field 2.0.1 [flutter reactive_forms phone_form_field] - reorderables 0.6.0 [flutter] - share_plus 7.2.1 [cross_file meta mime flutter flutter_web_plugins share_plus_platform_interface file url_launcher_web url_launcher_windows url_launcher_linux url_launcher_platform_interface ffi win32] - shared_preferences 2.2.2 [flutter shared_preferences_android shared_preferences_foundation shared_preferences_linux shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - shimmer 3.0.0 [flutter] - styled_text 8.1.0 [flutter xmlstream] - table_calendar 3.0.9 [flutter intl simple_gesture_detector] - uni_links 0.5.1 [flutter uni_links_platform_interface uni_links_web] - universal_html 2.2.4 [async csslib charcode collection html meta source_span typed_data universal_io] - universal_io 2.2.2 [collection meta typed_data] - url_launcher 6.1.14 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - url_strategy 0.2.0 [flutter_web_plugins] - version 3.0.2 - visibility_detector 0.4.0+2 [flutter] dev dependencies: - build_runner 2.4.6 [analyzer args async 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] - copy_with_extension_gen 5.0.4 [analyzer build source_gen copy_with_extension meta] - flutter_lints 3.0.1 [lints] - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection js matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph] - json_serializable 6.7.1 [analyzer async build build_config collection json_annotation meta path pub_semver pubspec_parse source_gen source_helper] dependency overrides: - collection 1.18.0 - intl 0.18.1 [clock meta path] transitive dependencies: - _fe_analyzer_shared 61.0.0 [meta] - _flutterfire_internals 1.3.11 [collection firebase_core firebase_core_platform_interface flutter meta] - analyzer 5.13.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - archive 3.3.7 [crypto path pointycastle] - args 2.4.2 - async 2.11.0 [collection meta] - audio_session 0.1.16 [flutter flutter_web_plugins rxdart meta] - barcode 2.2.4 [meta qr] - bidi 2.0.7 - boolean_selector 2.1.1 [source_span string_scanner] - build 2.4.1 [analyzer async convert crypto glob logging meta package_config path] - build_config 1.1.1 [checked_yaml json_annotation path pubspec_parse yaml] - build_daemon 4.0.0 [built_collection built_value http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.2.1 [analyzer async build collection crypto graphs logging package_config path pool pub_semver stream_transform yaml] - build_runner_core 7.2.10 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta package_config path pool timing watcher yaml] - built_collection 5.1.1 - built_value 8.6.1 [built_collection collection fixnum meta] - cached_network_image 3.3.0 [cached_network_image_platform_interface cached_network_image_web flutter flutter_cache_manager octo_image] - cached_network_image_platform_interface 3.0.0 [flutter flutter_cache_manager] - cached_network_image_web 1.1.0 [cached_network_image_platform_interface flutter flutter_cache_manager] - characters 1.3.0 - charcode 1.3.1 - checked_yaml 2.0.3 [json_annotation source_span yaml] - chewie 1.7.1 [cupertino_icons flutter provider video_player wakelock_plus] - circle_flags 3.0.1 [flutter flutter_svg] - clock 1.1.1 - code_builder 4.5.0 [built_collection built_value collection matcher meta] - convert 3.1.1 [typed_data] - cross_file 0.3.3+4 [js meta] - crypto 3.0.3 [typed_data] - csslib 0.17.3 [source_span] - cupertino_icons 1.0.6 - dart_style 2.3.2 [analyzer args path pub_semver source_span] - dbus 0.7.8 [args ffi meta xml] - device_info_plus 9.1.0 [device_info_plus_platform_interface ffi file flutter flutter_web_plugins meta win32 win32_registry] - device_info_plus_platform_interface 7.0.0 [flutter meta plugin_platform_interface] - diacritic 0.1.4 - extension 0.6.0 [meta] - fake_async 1.3.1 [clock collection] - ffi 2.0.2 - file 6.1.4 [meta path] - file_selector_linux 0.9.2+1 [cross_file file_selector_platform_interface flutter] - file_selector_macos 0.9.3+2 [cross_file file_selector_platform_interface flutter] - file_selector_platform_interface 2.6.1 [cross_file flutter http plugin_platform_interface] - file_selector_windows 0.9.3+1 [cross_file file_selector_platform_interface flutter] - firebase_analytics_platform_interface 3.7.5 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_analytics_web 0.5.5+5 [_flutterfire_internals firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 2.8.1 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.6.11 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 4.5.12 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 3.5.12 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - fixnum 1.1.0 - flutter_cache_manager 3.3.1 [clock collection file flutter http path path_provider rxdart sqflite uuid] - flutter_local_notifications_linux 4.0.0+1 [dbus ffi flutter flutter_local_notifications_platform_interface path xdg_directories] - flutter_local_notifications_platform_interface 7.0.0+1 [flutter plugin_platform_interface] - flutter_plugin_android_lifecycle 2.0.15 [flutter] - flutter_secure_storage_linux 1.1.3 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_macos 3.0.0 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_platform_interface 1.0.1 [flutter plugin_platform_interface] - flutter_secure_storage_web 1.1.1 [flutter flutter_secure_storage_platform_interface flutter_web_plugins js] - flutter_secure_storage_windows 3.0.0 [ffi flutter flutter_secure_storage_platform_interface path path_provider win32] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - flutter_widget_from_html_core 0.14.6 [csslib flutter html logging] - frontend_server_client 3.2.0 [async path] - fwfh_cached_network_image 0.14.2 [cached_network_image flutter flutter_cache_manager flutter_widget_from_html_core] - fwfh_chewie 0.14.2 [chewie flutter flutter_widget_from_html_core video_player] - fwfh_just_audio 0.14.2 [flutter flutter_widget_from_html_core just_audio] - fwfh_svg 0.8.0+4 [flutter flutter_svg flutter_widget_from_html_core] - fwfh_url_launcher 0.9.0+4 [flutter flutter_widget_from_html_core url_launcher] - fwfh_webview 0.14.2 [flutter flutter_widget_from_html_core logging webview_flutter webview_flutter_android webview_flutter_wkwebview] - glob 2.1.2 [async collection file path string_scanner] - graphs 2.3.1 [collection] - html 0.15.4 [csslib source_span] - http 1.1.0 [async http_parser meta] - http_multi_server 3.2.1 [async] - image 4.0.17 [archive meta xml] - image_picker_android 0.8.8 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface] - image_picker_for_web 3.0.1 [flutter flutter_web_plugins image_picker_platform_interface mime] - image_picker_ios 0.8.8+2 [flutter image_picker_platform_interface] - image_picker_linux 0.2.1+1 [file_selector_linux file_selector_platform_interface flutter image_picker_platform_interface] - image_picker_macos 0.2.1+1 [file_selector_macos file_selector_platform_interface flutter image_picker_platform_interface] - image_picker_platform_interface 2.9.1 [cross_file flutter http plugin_platform_interface] - image_picker_windows 0.2.1+1 [file_selector_platform_interface file_selector_windows flutter image_picker_platform_interface] - io 1.0.4 [meta path string_scanner] - js 0.6.7 [meta] - just_audio 0.9.35 [just_audio_platform_interface just_audio_web audio_session rxdart path path_provider async uuid crypto meta flutter] - just_audio_platform_interface 4.2.1 [flutter plugin_platform_interface] - just_audio_web 0.4.8 [just_audio_platform_interface flutter flutter_web_plugins] - lints 3.0.0 - local_auth_android 1.0.32 [flutter flutter_plugin_android_lifecycle intl local_auth_platform_interface] - local_auth_ios 1.1.3 [flutter intl local_auth_platform_interface] - local_auth_platform_interface 1.0.7 [flutter plugin_platform_interface] - local_auth_windows 1.0.9 [flutter local_auth_platform_interface] - logging 1.2.0 - matcher 0.12.15 [async meta stack_trace term_glyph test_api] - material_color_utilities 0.2.0 - meta 1.9.1 - mime 1.0.4 - nested 1.0.0 [flutter] - octo_image 2.0.0 [flutter] - package_config 2.1.0 [path] - package_info_plus_platform_interface 2.0.1 [flutter meta plugin_platform_interface] - path_parsing 1.0.1 [vector_math meta] - path_provider_android 2.2.0 [flutter path_provider_platform_interface] - path_provider_foundation 2.3.1 [flutter path_provider_platform_interface] - path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_platform_interface 2.1.1 [flutter platform plugin_platform_interface] - path_provider_windows 2.2.1 [ffi flutter path path_provider_platform_interface win32] - permission_handler_android 11.0.0 [flutter permission_handler_platform_interface] - permission_handler_apple 9.1.4 [flutter permission_handler_platform_interface] - permission_handler_platform_interface 3.11.5 [flutter meta plugin_platform_interface] - permission_handler_windows 0.1.3 [flutter permission_handler_platform_interface] - petitparser 5.4.0 [meta] - phone_form_field 8.1.0 [flutter flutter_localizations circle_flags phone_numbers_parser intl diacritic] - phone_numbers_parser 8.0.0 [meta] - photo_view 0.14.0 [flutter] - platform 3.1.0 - plugin_platform_interface 2.1.6 [meta] - pointycastle 3.7.3 [collection convert js] - pool 1.5.1 [async stack_trace] - process 4.2.4 [file path platform] - provider 6.0.5 [collection flutter nested] - pub_semver 2.1.4 [collection meta] - pubspec_parse 1.2.3 [checked_yaml collection json_annotation pub_semver yaml] - qr 3.0.1 [meta] - riverpod 2.4.5 [meta stack_trace state_notifier] - rxdart 0.27.7 - share_plus_platform_interface 3.3.1 [cross_file flutter meta mime plugin_platform_interface path_provider uuid] - shared_preferences_android 2.2.0 [flutter shared_preferences_platform_interface] - shared_preferences_foundation 2.3.3 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.3.0 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface] - shared_preferences_platform_interface 2.3.0 [flutter plugin_platform_interface] - shared_preferences_web 2.2.0 [flutter flutter_web_plugins shared_preferences_platform_interface] - shared_preferences_windows 2.3.0 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - shelf 1.4.1 [async collection http_parser path stack_trace stream_channel] - shelf_web_socket 1.0.4 [shelf stream_channel web_socket_channel] - simple_gesture_detector 0.2.0 [flutter] - sky_engine 0.0.99 - source_gen 1.4.0 [analyzer async build dart_style glob path source_span yaml] - source_helper 1.3.4 [analyzer collection source_gen] - source_span 1.9.1 [collection path term_glyph] - sprintf 7.0.0 - sqflite 2.2.8+4 [flutter sqflite_common path] - sqflite_common 2.4.5+1 [synchronized path meta] - stack_trace 1.11.0 [path] - state_notifier 0.7.2+1 [meta] - stream_channel 2.1.1 [async] - stream_transform 2.1.0 - string_scanner 1.2.0 [source_span] - synchronized 3.1.0 - term_glyph 1.2.1 - test_api 0.5.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - timezone 0.9.2 [path] - timing 1.0.1 [json_annotation] - typed_data 1.3.2 [collection] - uni_links_platform_interface 1.0.0 [flutter plugin_platform_interface] - uni_links_web 0.1.0 [flutter flutter_web_plugins uni_links_platform_interface] - universal_platform 1.0.0+1 - url_launcher_android 6.0.36 [flutter url_launcher_platform_interface] - url_launcher_ios 6.1.4 [flutter url_launcher_platform_interface] - url_launcher_linux 3.0.5 [flutter url_launcher_platform_interface] - url_launcher_macos 3.0.5 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.1.3 [flutter plugin_platform_interface] - url_launcher_web 2.0.18 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 3.0.7 [flutter url_launcher_platform_interface] - uuid 4.1.0 [crypto sprintf] - vector_graphics 1.1.9+1 [flutter vector_graphics_codec] - vector_graphics_codec 1.1.9+1 - vector_graphics_compiler 1.1.9+1 [args meta path_parsing xml vector_graphics_codec path] - vector_math 2.1.4 - video_player 2.7.2 [flutter html video_player_android video_player_avfoundation video_player_platform_interface video_player_web] - video_player_android 2.4.10 [flutter video_player_platform_interface] - video_player_avfoundation 2.4.11 [flutter video_player_platform_interface] - video_player_platform_interface 6.2.1 [flutter plugin_platform_interface] - video_player_web 2.0.17 [flutter flutter_web_plugins video_player_platform_interface] - wakelock_plus 1.1.2 [flutter flutter_web_plugins meta wakelock_plus_platform_interface win32 dbus package_info_plus js] - wakelock_plus_platform_interface 1.1.0 [flutter plugin_platform_interface meta] - watcher 1.1.0 [async path] - web_socket_channel 2.4.0 [async crypto stream_channel] - webview_flutter 4.4.1 [flutter webview_flutter_android webview_flutter_platform_interface webview_flutter_wkwebview] - webview_flutter_android 3.12.0 [flutter webview_flutter_platform_interface] - webview_flutter_platform_interface 2.6.0 [flutter meta plugin_platform_interface] - webview_flutter_wkwebview 3.9.1 [flutter path webview_flutter_platform_interface] - win32 5.0.5 [ffi] - win32_registry 1.1.1 [ffi win32] - xdg_directories 1.0.0 [meta path process] - xml 6.3.0 [collection meta petitparser] - xmlstream 1.0.0 - yaml 3.1.2 [collection source_span string_scanner] ```

kaetheI commented 1 year ago

We have the same problem. We also found, that with a debug apk the app receives notifications while its in the background.

darshankawar commented 1 year ago

@aaumond Android and iOS handle background messaging differently.

The notification is sent by the backend as follows: image

This seems to be a third party tool to send notifications to the app. Basically, 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. So I suggest you to try to send silent push notifications (data- only) via firebase console and check if using it, do you get same behavior or not.

Quen71 commented 1 year ago

@darshankawar After several tests, I'am not able to handle the silent notification with android while the app is on background state.

My background handler in my flutter project is as following:

FirebaseMessaging.onBackgroundMessage(
      (RemoteMessage message) async => print(message),
    );

And this is the data i send through firebase with postman:

{
"to": "my_fcm_token",
"content_available": true,
"priority": 10,
"android": {
        "priority": "high",
        "content-available": "1"
},
"data": {
    "key1": "abc",
    "key2": "123"
}
}

With this JSON data, android only manages foreground silent notification.

I tried with the firebase console too, but I didn't find a way to send a push notification with only data.

aaumond commented 12 months ago

@darshankawar I have the same behavior that @Quen71. By sending a silent push notification when the application is in background on an Android device, it is not intercepted.

JosephKalash commented 12 months ago

I have same issue here Flutter 3.13.6 Android 13

After some testing I have noticed several things

danagbemava-nc commented 11 months ago

Hi all, do the devices you are experiencing this issue on have some background restrictions enabled?

aaumond commented 11 months ago

Hi @danagbemava-nc, the devices don't have restrictions on background tasks.

danagbemava-nc commented 11 months ago

Hi all, are you experiencing this in scenarios where the app has been completely killed?

aaumond commented 11 months ago

@danagbemava-nc Hi, the app was on background, not completely killed.

darshankawar commented 11 months ago

Thanks for all the updates. I tried with plugin example on Android with which I did get similar behavior as reported, but I am not completely sure if this is a bug or intended functionality or a limitation of underlying sdk, as I couldn't find documentation related to how Android is suppose to handle silent or data-only push notification when app is in background.

I'll keep this issue open for team's input on the behavior reported. /cc @russellwheatley

russellwheatley commented 10 months ago

@aaumond - I've just tested using the nodejs Firebase admin SDK:

const admin = require('firebase-admin');
// Get service account key from firebase console
const serviceAccount = require('../ff-e2e-service.json');
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});
const androidToken = 'TOKEN';
admin
.messaging()
.send(
  {
    token: androidToken,
    data: {
      something:'test',
    },
    android: {
      // Required for background/terminated app state messages on Android
      priority: 'high',
    },
    apns: {
      payload: {
        aps: {
          // Required for background/terminated app state messages on iOS
          contentAvailable: true,
        }
      },
    },
  },

)
.then((res) => {
  if (res.failureCount) {
    console.log('Failed', res.results[0].error);
  } else {
    console.log('Success');
  }
})

I was able to get a silent notification in my background handler in the firebase messaging example app on my android device. Please use the firebase admin SDK to send messages and let me know if this works. Thanks.

google-oss-bot commented 10 months ago

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

google-oss-bot commented 10 months ago

Since there haven't been any recent updates here, I am going to close this issue.

@aaumond if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.