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

🐛 [firebase_messaging] flutter app cannot receive notification with data only on IOS #8914

Closed victorkwok97 closed 2 years ago

victorkwok97 commented 2 years ago

Bug report

Describe the bug

When only the data field is set but not the notification field, the flutter app cannot receive the notification on iOS devices. It works on Android devices.

I am using version 11.4.1 firebase_messaging

Steps to reproduce

In node.js:

import {getMessaging} from "firebase-admin/messaging"
function x() {
    const m = getMessaging()
    m.sendToDevice([
            tokenHere
        ],{
            "data": {
                "a": "b",
            }
        }, {
            "content_available": true,
            "priority": "high"
        }
    ).then(e=>console.log(JSON.stringify(e.results))).catch(e=>console.log(JSON.stringify(e.results)));
}
x()

In flutter:

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
     // Not firing
      print('Got a message whilst in the foreground!');
    });

Expected behavior

The callback should be fired on iOS app as well.

Sample project

None


Additional context

None


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` [✓] Flutter (Channel stable, 3.0.0, on macOS 12.3.1 21E258 darwin-x64, locale en-HK) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [✓] Xcode - develop for iOS and macOS (Xcode 13.3.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.1) [✓] Connected device (2 available) [✓] HTTP Host Availability • No issues found! ```

Flutter dependencies

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

Click To Expand ``` Dart SDK 2.17.0 Flutter SDK 3.0.0 afterschool 1.20.58+12058 dependencies: - appsflyer_sdk 6.5.2+2 [flutter] - cached_network_image 3.2.0 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web] - collection 1.16.0 - cupertino_icons 1.0.4 - device_info_plus 3.2.3 [flutter device_info_plus_platform_interface device_info_plus_macos device_info_plus_linux device_info_plus_web device_info_plus_windows] - equatable 2.0.3 [collection meta] - firebase_analytics 9.1.8 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter] - firebase_auth 3.3.18 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.17.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_messaging 11.4.0 [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.0.1 [flutter bloc provider] - flutter_easyloading 3.0.3 [flutter flutter_spinkit] - flutter_local_notifications 9.5.3+1 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone] - flutter_login_facebook 1.4.1 [flutter list_ext] - flutter_signin_button 2.0.0 [flutter font_awesome_flutter] - flutter_svg 1.0.3 [flutter meta path_drawing vector_math xml] - formz 0.4.1 - google_sign_in 5.3.1 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web] - http 0.13.4 [async http_parser meta path] - image 3.1.3 [archive meta xml] - image_picker 0.8.5+3 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface] - infinite_scroll_pagination 3.1.0 [flutter sliver_tools] - intl 0.17.0 [clock path] - json_annotation 4.5.0 [meta] - localstorage 4.0.0+1 [flutter path_provider] - path_provider 2.0.10 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows] - photo_view 0.13.0 [flutter] - sign_in_with_apple 3.3.0 [flutter meta sign_in_with_apple_platform_interface sign_in_with_apple_web] - timeago 3.2.2 - url_launcher 6.1.2 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - usage 4.0.2 [path] dev dependencies: - build_runner 2.1.11 [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_launcher_icons 0.9.2 [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] - json_serializable 6.2.0 [analyzer async build build_config collection json_annotation meta path pub_semver pubspec_parse source_gen source_helper] transitive dependencies: - _fe_analyzer_shared 39.0.0 [meta] - analyzer 4.0.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - archive 3.3.0 [crypto path] - args 2.3.1 - async 2.8.2 [collection meta] - bloc 8.0.3 [meta] - boolean_selector 2.1.0 [source_span string_scanner] - build 2.3.0 [analyzer async convert crypto glob logging meta path] - build_config 1.0.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.8 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml] - build_runner_core 7.2.3 [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.3.0 [built_collection collection fixnum meta] - 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] - characters 1.2.0 - charcode 1.3.1 - checked_yaml 2.0.1 [json_annotation source_span yaml] - clock 1.1.0 - code_builder 4.1.0 [built_collection built_value collection matcher meta] - convert 3.0.1 [typed_data] - cross_file 0.3.3 [js meta] - crypto 3.0.2 [typed_data] - dart_style 2.2.3 [analyzer args path pub_semver source_span] - dbus 0.7.3 [args ffi meta xml] - device_info_plus_linux 2.1.1 [device_info_plus_platform_interface file flutter meta] - device_info_plus_macos 2.2.3 [device_info_plus_platform_interface flutter] - device_info_plus_platform_interface 2.3.0+1 [flutter meta plugin_platform_interface] - device_info_plus_web 2.1.0 [device_info_plus_platform_interface flutter_web_plugins flutter] - device_info_plus_windows 2.1.1 [device_info_plus_platform_interface ffi flutter win32] - fake_async 1.3.0 [clock collection] - ffi 1.2.1 - file 6.1.2 [meta path] - firebase_analytics_platform_interface 3.1.6 [firebase_core flutter meta plugin_platform_interface] - firebase_analytics_web 0.4.0+13 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_auth_platform_interface 6.2.6 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 3.3.15 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.4.0 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.6.4 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_messaging_platform_interface 3.5.0 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 2.4.0 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - fixnum 1.0.1 - 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_local_notifications_linux 0.4.2 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories] - flutter_local_notifications_platform_interface 5.0.0 [flutter plugin_platform_interface] - flutter_plugin_android_lifecycle 2.0.6 [flutter] - flutter_spinkit 5.1.0 [flutter] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - font_awesome_flutter 9.2.0 [flutter] - frontend_server_client 2.1.3 [async path] - glob 2.0.2 [async collection file path string_scanner] - google_sign_in_android 5.2.7 [flutter google_sign_in_platform_interface] - google_sign_in_ios 5.2.7 [flutter google_sign_in_platform_interface] - google_sign_in_platform_interface 2.1.3 [flutter quiver] - google_sign_in_web 0.10.1+2 [flutter flutter_web_plugins google_sign_in_platform_interface js] - graphs 2.1.0 [collection] - http_multi_server 3.2.0 [async] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - image_picker_android 0.8.4+13 [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.5+4 [flutter image_picker_platform_interface] - image_picker_platform_interface 2.5.0 [cross_file flutter http plugin_platform_interface] - io 1.0.3 [meta path string_scanner] - js 0.6.4 - lints 1.0.1 - list_ext 1.0.4 [quiver collection] - logging 1.0.2 - matcher 0.12.11 [stack_trace] - material_color_utilities 0.1.4 - meta 1.7.0 - mime 1.0.2 - nested 1.0.0 [flutter] - octo_image 1.0.2 [flutter flutter_blurhash] - package_config 2.0.2 [path] - path 1.8.1 - path_drawing 1.0.0 [vector_math meta path_parsing flutter] - path_parsing 1.0.0 [vector_math meta] - path_provider_android 2.0.14 [flutter path_provider_platform_interface] - path_provider_ios 2.0.9 [flutter path_provider_platform_interface] - path_provider_linux 2.1.6 [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.0.6 [ffi flutter path path_provider_platform_interface win32] - pedantic 1.11.1 - petitparser 4.4.0 [meta] - platform 3.1.0 - plugin_platform_interface 2.1.2 [meta] - pool 1.5.0 [async stack_trace] - process 4.2.4 [file path platform] - provider 6.0.2 [collection flutter nested] - pub_semver 2.1.1 [collection meta] - pubspec_parse 1.2.0 [checked_yaml collection json_annotation pub_semver yaml] - quiver 3.1.0 [matcher] - rxdart 0.27.3 - shelf 1.3.0 [async collection http_parser path stack_trace stream_channel] - shelf_web_socket 1.0.1 [shelf stream_channel web_socket_channel] - sign_in_with_apple_platform_interface 1.0.0 [flutter plugin_platform_interface meta] - sign_in_with_apple_web 1.0.1 [flutter flutter_web_plugins sign_in_with_apple_platform_interface js] - sky_engine 0.0.99 - sliver_tools 0.2.6 [flutter] - source_gen 1.2.2 [analyzer async build dart_style glob meta path source_span yaml] - source_helper 1.3.2 [analyzer collection source_gen] - source_span 1.8.2 [collection path term_glyph] - sqflite 2.0.2+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+2 - 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] - timezone 0.8.0 [path] - timing 1.0.0 [json_annotation] - typed_data 1.3.0 [collection] - url_launcher_android 6.0.17 [flutter url_launcher_platform_interface] - url_launcher_ios 6.0.16 [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.0.5 [flutter plugin_platform_interface] - url_launcher_web 2.0.11 [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 - watcher 1.0.1 [async path] - web_socket_channel 2.2.0 [async crypto stream_channel] - win32 2.5.2 [ffi] - xdg_directories 0.2.0+1 [meta path process] - xml 5.3.1 [collection meta petitparser] - yaml 3.1.1 [collection source_span string_scanner] ```

maheshmnj commented 2 years ago

Hi @victorkwok97, Thanks for filing the issue. Using the same backend are the notifications received on Android device?

Additionally can you test if sending notifications from terminal works?

/// get the list of devices online
xcrun simctl list 'devices' 'booted'

/// send to all online devices
xcrun simctl push booted com.bundle.identifier payload.json 

/// send to a specific device by passing deviceId found in above list
xcrun simctl push <deviceId> com.bundle.identifier payload.json 

sample payload.json

{
   "aps": {
      "alert": {
         "title": "Game Request",
         "subtitle": "Five Card Draw",
         "body": "Bob wants to play poker"
      }
   }
}

This will help us figure out whether the issue is in the app configuration or the backend. Also ensure that you have approved notification permissions on ios.

rich-j commented 2 years ago

This issue is a continuation of closed and locked issue #8548 (with @victorkwok97 comment) which superseded #8378 and is specifically about data only notifications (the Apple terminology is silent).

@maheshmnj please use a silent notification such as this sample from Apple's documentation Pushing Background Updates to Your App

{
   "aps" : {
      "content-available" : 1
   },
   "acme1" : "bar",
   "acme2" : 42
}

For us data notifications always work for Android. We do receive notifications on iOS (i.e. setup is correct) when we use any user facing notifications: alert, sound, or badge. What doesn't work is silent notifications that on Apple requires none of the fore mentioned user facing payloads and only the content-available flag.

victorkwok97 commented 2 years ago

@maheshmnj Yes, the notifications are received on Android. Everything is working perfectly fine on Android.

For iOS, I have tried sending the notification from terminal, it works. Further, I have also tried sending the notification from the Firebase console as well. So I can ensure you that the configuration is fine.

The issue, as elaborated by @rich-j , is that it only does not work for the silent notifications. So for those cases only the data field is set.

Lyokone commented 2 years ago

Hello @victorkwok97, According to the typing on typescript, you should use

        /**
         * On iOS, use this field to represent `content-available` in the APNs payload.
         * When a notification or data message is sent and this is set to `true`, an
         * inactive client app is awoken. On Android, data messages wake the app by
         * default. On Chrome, this flag is currently not supported.
         *
         * **Default value:** `false`
         */
        contentAvailable?: boolean;
    await admin.messaging().sendToDevice(
      tokens,
      {
        data: {
          ...
        },
      },
      { contentAvailable: true }
    );

Not sure if the casing changes something?

rich-j commented 2 years ago

@Lyokone I believe you are conflating an API with the JSON message. We use the Firebase Java API which has an aps builder that takes a boolean parameter:

    /**
     * Specifies whether to configure a background update notification.
     *
     * @param contentAvailable True to perform a background update.
     * @return This builder.
     */
    public Builder setContentAvailable(boolean contentAvailable) {
      this.contentAvailable = contentAvailable;
      return this;
    }

But the code that builds the JSON message changes the API name and value to:

    if (builder.contentAvailable) {
      fields.put("content-available", 1);
    }

This field is then added to the JSON message with the name and value matching the Apple documentation.

Lyokone commented 2 years ago

Oh okay, maybe I didn't understood properly. @victorkwok97 sample in the original message is using node.js. I was responding to this 🤔

maheshmnj commented 2 years ago

Thanks for the clarification, I am able to reproduce the issue user facing notifications work but silent notifications do not.

sending this payload works

{
   "aps": {
      "alert": {
         "title": "Game Request",
         "subtitle": "Five Card Draw",
         "body": "Bob wants to play poker"
      },
      "content-available" : 1
   }
}

while this doesn't

{
   "aps" : {
      "content-available" : 1
   },
   "acme1" : "bar",
   "acme2" : 42
}

I do have background Notifications and remote notifications capabilities enabled in xcode.

flutter doctor -v (mac) ``` [✓] Flutter (Channel stable, 3.0.2, on macOS 12.4 21F79 darwin-arm, locale en-IN) • Flutter version 3.0.2 at /Users/mahesh/Documents/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision cd41fdd495 (5 days ago), 2022-06-08 09:52:13 -0700 • Engine revision f15f824b57 • Dart version 2.17.3 • DevTools version 2.12.2 [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4) • Android SDK at /Users/mahesh/Library/Android/sdk • Platform android-32, build-tools 33.0.0-rc4 • ANDROID_HOME = /Users/mahesh/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.2) • Android Studio at /Applications/Android Studio.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 • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840) [✓] IntelliJ IDEA Community Edition (version 2021.2.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 61.2.4 • Dart plugin version 212.5080.8 [✓] VS Code (version 1.67.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.42.0 [✓] Connected device (3 available) • sdk gphone arm64 (mobile) • emulator-5554 • android-arm64 • Android 11 (API 30) (emulator) • macOS (desktop) • macos • darwin-arm64 • macOS 12.4 21F79 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 102.0.5005.61 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ```
darshankawar commented 2 years ago

@victorkwok97 @rich-j The original issue seems to be https://github.com/firebase/flutterfire/issues/8277 and looking at below comments from that issue, it seem to be working:

https://github.com/firebase/flutterfire/issues/8277#issuecomment-1152535863 https://github.com/firebase/flutterfire/issues/8277#issuecomment-1152289514 https://github.com/firebase/flutterfire/issues/8277#issuecomment-1149698615

Can you try the same and see if above helps in your case ?

victorkwok97 commented 2 years ago

@darshankawar I have tried all of them but sadly none of the solutions of those comments worked.

for https://github.com/firebase/flutterfire/issues/8277#issuecomment-1152535863 , you can see that I am using "content_available" already from my example above. So it should be correct.

As an extra information, the device that I use for testing runs on iOS 14.8.1. I have tried on a iOS 15.5 simulator as well. Both of them didn't work.

darshankawar commented 2 years ago

/cc @russellwheatley . Similar / related issue for your reference: https://github.com/firebase/flutterfire/issues/8277

russellwheatley commented 2 years ago

This works. Follow this link for example payloads. Make sure you have manual setup instead of dart initialisation (i.e. GoogleService-info.plist from firebase console). If it works and stops working, it's because iOS throttles the amount of times it can be used in a certain period of time.

russellwheatley commented 2 years ago

I am closing this out as this issue was confirmed as resolved by me and users in this issue. Will reopen if you have a reproducible sample/repo. Thanks.

rich-j commented 2 years ago

@russellwheatley I will confirm that using manual installation instead of dart initialization makes the problem disappear. So if you're not considering this a code "bug" then it should be treated as a documentation issue. The documentation explicitly states

With Dart-only initialization now supported, manually configuring and installing platforms is no longer required.

We followed the current documentation and FCM did not work in this case.