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.64k stars 3.95k forks source link

🐛 [firebase_messaging] Handler is not called when a push notification is received on iOS #6966

Closed maysamsh closed 3 years ago

maysamsh commented 3 years ago

On iOS devices the message handler will not get triggered on receiving a push notification, either when the app is in foreground or background. The device shows the notification banner when the app is not in foreground.

This is my push notification service:

typedef ListenFunction = void Function(RemoteMessage message);
typedef BackgroundListenFunction = Future<void> Function(RemoteMessage message);

class PushNotificationService {
  final FirebaseMessaging _fcm = FirebaseMessaging.instance;
  PushNotificationService._();
  static PushNotificationService? _instance;
  factory PushNotificationService() => _instance ??= new PushNotificationService._();

  DeviceInfoPlugin _deviceInfo = DeviceInfoPlugin();
  NotificationSettings? _settings;

static Future<void> listen({required ListenFunction handler, required BackgroundListenFunction backgroundHandler}) async {
    final _firebaseMessaging = PushNotificationService()._fcm;
    await _firebaseMessaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );
    FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) async {
      if (message != null) {
        handler(message);
      }
    });

    FirebaseMessaging.onMessageOpenedApp.listen(handler);
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      handler(message);
    });

    FirebaseMessaging.onBackgroundMessage(backgroundHandler);
  }
}

I call it on main.dart, inside initState() after calling await Firebase.initializeApp();:

PushNotificationService.listen(handler: _handleMessage, backgroundHandler: _firebaseMessagingBackgroundHandler);

In console I receive this warning upon receiving a push notification:

Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.

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, 2.2.3, on macOS 11.5.2 20G95 darwin-x64, locale en) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) [✓] Xcode - develop for iOS and macOS [✓] Chrome - develop for the web [✓] Android Studio (version 2020.3) [✓] VS Code (version 1.59.1) [✓] Connected device (3 available) • No issues found! ```

Flutter dependencies

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

Click To Expand ``` Dart SDK 2.13.4 Flutter SDK 2.2.3 dependencies: - audioplayers 0.19.1 [uuid path_provider http flutter flutter_web_plugins] - auto_size_text 3.0.0-nullsafety.0 [flutter] - cupertino_icons 1.0.3 - device_info_plus 2.1.0 [flutter device_info_plus_platform_interface device_info_plus_linux device_info_plus_macos device_info_plus_web device_info_plus_windows] - dio 4.0.0 [http_parser path] - dots_indicator 2.0.0 [flutter] - firebase_analytics 8.3.1 [firebase_analytics_platform_interface firebase_analytics_web firebase_core flutter meta] - firebase_core 1.6.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 2.2.1 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_messaging 10.0.6 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - fl_chart 0.36.4 [flutter equatable pedantic] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - flutter_bloc 7.1.0 [flutter bloc provider] - flutter_branch_sdk 3.4.0 [flutter flutter_web_plugins plugin_platform_interface js] - flutter_dotenv 5.0.2 [flutter] - flutter_google_places 0.3.0 [flutter google_api_headers google_maps_webservice http rxdart] - flutter_inapp_purchase 5.0.2 [http meta flutter platform] - flutter_secure_storage 4.2.1 [meta flutter] - flutter_typeahead 3.2.0 [flutter flutter_keyboard_visibility] - flutter_web_auth 0.3.1 [flutter] - get 4.3.8 [flutter] - google_fonts 2.1.0 [flutter http path_provider crypto] - hexcolor 2.0.5 [flutter flutter_web_plugins] - in_app_purchase 1.0.8 [flutter in_app_purchase_platform_interface in_app_purchase_android in_app_purchase_ios] - intl 0.17.0 [clock path] - jiffy 4.1.0 [intl] - json_annotation 4.1.0 [meta] - json_serializable 4.1.4 [analyzer build build_config collection json_annotation meta path source_gen] - just_the_tooltip 0.0.7+2 [flutter] - keyboard_actions 3.4.4 [flutter] - package_info 2.0.2 [flutter] - pin_code_fields 7.3.0 [flutter] - provider 5.0.0 [collection flutter nested] - retrofit 2.0.1 [dio meta] - shake_flutter 14.1.2 [flutter path] - share 2.0.4 [meta mime flutter] - shared_preferences 2.0.7 [flutter meta shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - sleek_circular_slider 2.0.1 [flutter] - transparent_image 2.0.0 - uni_links 0.5.1 [flutter uni_links_platform_interface uni_links_web] - url_launcher 6.0.9 [flutter meta url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - validators 3.0.0 - video_player 2.1.14 [flutter meta video_player_platform_interface video_player_web] - wakelock 0.5.3+3 [flutter meta wakelock_macos wakelock_platform_interface wakelock_web wakelock_windows] - youtube_explode_dart 1.10.6 [collection freezed_annotation html http http_parser json_annotation meta xml] - youtube_player_flutter 8.0.0 [flutter flutter_inappwebview] dev dependencies: - build_runner 2.1.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 pedantic pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] - retrofit_generator 2.0.1+1 [dio source_gen built_collection code_builder tuple retrofit analyzer dart_style build] - test 1.16.8 [analyzer async boolean_selector collection coverage http_multi_server io js node_preamble package_config path pedantic pool shelf shelf_packages_handler shelf_static shelf_web_socket source_span stack_trace stream_channel typed_data web_socket_channel webkit_inspection_protocol yaml test_api test_core] dependency overrides: - analyzer 1.7.0 [_fe_analyzer_shared cli_util collection convert crypto glob meta package_config path pub_semver source_span watcher yaml pedantic] - meta 1.4.0 transitive dependencies: - _fe_analyzer_shared 22.0.0 [meta] - args 2.2.0 - async 2.6.1 [meta collection] - bloc 7.1.0 [meta] - boolean_selector 2.1.0 [source_span string_scanner] - build 2.1.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.0.0 [built_collection built_value http_multi_server logging pedantic path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.0.4 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml] - build_runner_core 7.1.0 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta path package_config pedantic pool timing watcher yaml] - built_collection 5.1.0 - built_value 8.1.2 [built_collection collection fixnum meta] - characters 1.1.0 - charcode 1.2.0 - checked_yaml 2.0.1 [json_annotation source_span yaml] - cli_util 0.3.3 [meta path] - clock 1.1.0 - code_builder 4.1.0 [built_collection built_value collection matcher meta] - collection 1.15.0 - convert 3.0.1 [typed_data] - coverage 1.0.3 [args logging package_config path source_maps stack_trace vm_service] - crypto 3.0.1 [collection typed_data] - csslib 0.17.0 [source_span] - dart_style 2.0.3 [analyzer args path pub_semver source_span] - device_info_plus_linux 2.1.0 [device_info_plus_platform_interface file flutter meta] - device_info_plus_macos 2.1.0 [device_info_plus_platform_interface flutter] - device_info_plus_platform_interface 2.1.0 [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.0 [device_info_plus_platform_interface ffi flutter win32] - equatable 2.0.3 [collection meta] - fake_async 1.2.0 [clock collection] - ffi 1.1.2 - file 6.1.2 [meta path] - firebase 9.0.1 [http http_parser js] - firebase_analytics_platform_interface 2.0.1 [flutter meta] - firebase_analytics_web 0.3.0+1 [firebase firebase_analytics_platform_interface flutter flutter_web_plugins meta] - firebase_core_platform_interface 4.0.1 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.1.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.1.2 [collection firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 3.0.5 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 2.0.5 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - fixnum 1.0.0 - flutter_inappwebview 5.3.2 [flutter] - flutter_keyboard_visibility 5.0.3 [meta flutter_keyboard_visibility_platform_interface flutter_keyboard_visibility_web flutter] - 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_web_plugins 0.0.0 [flutter js characters collection meta typed_data vector_math] - freezed_annotation 0.14.3 [collection json_annotation meta] - frontend_server_client 2.1.2 [async path] - glob 2.0.1 [async collection file path pedantic string_scanner] - google_api_headers 1.1.1 [flutter package_info_plus] - google_maps_webservice 0.0.20-nullsafety.5 [http meta json_annotation] - graphs 2.0.0 - html 0.15.0 [csslib source_span] - http 0.13.3 [async http_parser meta path pedantic] - http_multi_server 3.0.1 [async] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - in_app_purchase_android 0.1.4+6 [collection flutter in_app_purchase_platform_interface json_annotation meta] - in_app_purchase_ios 0.1.3+3 [collection flutter in_app_purchase_platform_interface json_annotation meta] - in_app_purchase_platform_interface 1.2.0 [flutter plugin_platform_interface] - io 1.0.3 [meta path string_scanner] - js 0.6.3 - logging 1.0.1 - matcher 0.12.10 [stack_trace] - mime 1.0.0 - nested 1.0.0 [flutter] - node_preamble 2.0.1 - package_config 2.0.0 [path] - package_info_plus 1.0.6 [flutter package_info_plus_platform_interface package_info_plus_linux package_info_plus_macos package_info_plus_windows package_info_plus_web] - package_info_plus_linux 1.0.3 [package_info_plus_platform_interface flutter path] - package_info_plus_macos 1.1.1 [flutter] - package_info_plus_platform_interface 1.0.2 [flutter meta plugin_platform_interface] - package_info_plus_web 1.0.4 [flutter flutter_web_plugins http meta package_info_plus_platform_interface] - package_info_plus_windows 1.0.3 [package_info_plus_platform_interface ffi flutter win32] - path 1.8.0 - path_provider 2.0.2 [flutter path_provider_platform_interface path_provider_macos path_provider_linux path_provider_windows] - path_provider_linux 2.0.2 [flutter path path_provider_platform_interface xdg_directories] - path_provider_macos 2.0.2 [flutter] - path_provider_platform_interface 2.0.1 [flutter meta platform plugin_platform_interface] - path_provider_windows 2.0.3 [ffi flutter meta path path_provider_platform_interface win32] - pedantic 1.11.1 - petitparser 4.2.0 [meta] - platform 3.0.2 - plugin_platform_interface 2.0.1 [meta] - pool 1.5.0 [async stack_trace] - process 4.2.3 [file path platform] - pub_semver 2.0.0 [collection] - pubspec_parse 1.0.0 [checked_yaml collection json_annotation pub_semver yaml] - quiver 3.0.1 [matcher] - rxdart 0.26.0 - shared_preferences_linux 2.0.2 [file meta flutter path path_provider_linux shared_preferences_platform_interface] - shared_preferences_macos 2.0.2 [flutter shared_preferences_platform_interface] - shared_preferences_platform_interface 2.0.0 [flutter] - shared_preferences_web 2.0.2 [flutter flutter_web_plugins meta shared_preferences_platform_interface] - shared_preferences_windows 2.0.2 [flutter file meta path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - shelf 1.2.0 [async collection http_parser path stack_trace stream_channel] - shelf_packages_handler 3.0.0 [path shelf shelf_static] - shelf_static 1.1.0 [convert http_parser mime path shelf] - shelf_web_socket 1.0.1 [shelf stream_channel web_socket_channel] - sky_engine 0.0.99 - source_gen 1.1.0 [analyzer async build dart_style glob meta path pedantic source_span yaml] - source_map_stack_trace 2.1.0 [path stack_trace source_maps] - source_maps 0.10.10 [source_span] - source_span 1.8.1 [collection path term_glyph] - 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] - term_glyph 1.2.0 - test_api 0.3.0 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - test_core 0.3.19 [analyzer async args boolean_selector collection coverage glob io meta package_config path pedantic pool source_map_stack_trace source_maps source_span stack_trace stream_channel vm_service yaml matcher test_api] - timing 1.0.0 [json_annotation] - tuple 2.0.0 [quiver] - typed_data 1.3.0 [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] - url_launcher_linux 2.0.1 [flutter] - url_launcher_macos 2.0.1 [flutter] - url_launcher_platform_interface 2.0.4 [flutter plugin_platform_interface] - url_launcher_web 2.0.4 [flutter flutter_web_plugins meta url_launcher_platform_interface] - url_launcher_windows 2.0.2 [flutter] - uuid 3.0.4 [crypto] - vector_math 2.1.0 - video_player_platform_interface 4.1.0 [flutter meta flutter_test] - video_player_web 2.0.3 [flutter flutter_web_plugins meta video_player_platform_interface] - vm_service 6.2.0 - wakelock_macos 0.1.0+2 [flutter flutter_web_plugins wakelock_platform_interface] - wakelock_platform_interface 0.2.1+2 [flutter meta] - wakelock_web 0.2.0+2 [flutter flutter_web_plugins js wakelock_platform_interface] - wakelock_windows 0.1.0+1 [flutter wakelock_platform_interface win32] - watcher 1.0.0 [async path pedantic] - web_socket_channel 2.1.0 [async crypto stream_channel] - webkit_inspection_protocol 1.0.0 [logging] - win32 2.2.8 [ffi] - xdg_directories 0.2.0 [meta path process] - xml 5.2.0 [collection meta petitparser] - yaml 3.1.0 [collection source_span string_scanner] ```

jasnicaa commented 3 years ago

There is an open issue with this https://github.com/FirebaseExtended/flutterfire/issues/6290, we are all waiting/working on the solution. So far no workarounds.

markusaksli-nc commented 3 years ago

Hi @maysamsh Could you try running the official example https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging/firebase_messaging/example/lib Not sure how dart features like aliases will mesh with the requirement of the background handler being a top-level static function.

Secondly what handlers are you providing to check that they work? Could you check if you have any relevant device logs for when the messages are received?

@jasnicaa since #6290 is about background messages I don't think this is a duplicate. Thank you

maysamsh commented 3 years ago

This is the worst possible way to handle errors but I figured out that my payload didn't have notification key and because of it the SDK could not call completion handler on application:didReceiveRemoteNotification:fetchCompletionHandler:

markusaksli-nc commented 3 years ago

Could you clarify? You are saying even the onMessage handler couldn't be called if your payload didn't have a notification key?

maysamsh commented 3 years ago

Correct

markusaksli-nc commented 3 years ago

Could you give an example payload that demonstrates this issue? I wasn't able to reproduce it with the official example on iOS.

maysamsh commented 3 years ago

Send the payload from another source than FMC

markusaksli-nc commented 3 years ago

You mean from an api call or something? But what was the payload json?

maysamsh commented 3 years ago
{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
        },
        "badge" : 5
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}

Something like this

markusaksli-nc commented 3 years ago

I sent just

{
    "apns": {
        "payload": {
            "aps": {
                "content-available": 1
            }
        }
    }
}

and still onMessage fired as expected.

Since we can't reproduce the issue and you seem to have solved it I'm going to close it as not actionable.

hrabkin commented 3 years ago

I sent just

{
    "apns": {
        "payload": {
            "aps": {
                "content-available": 1
            }
        }
    }
}

and still onMessage fired as expected.

Since we can't reproduce the issue and you seem to have solved it I'm going to close it as not actionable.

but this is still valid for Android platform, notifications not handled properly if there's no notification field

maysamsh commented 3 years ago

Did you send it via FCM console?

hrabkin commented 3 years ago

Did you send it via FCM console?

I tried multiple ways, using curl through ConnectyCube service and using FCM console. It works fine for FCM console because it sends pushes with notification field. So I am not aware now should I fill issue on the ConnectyCube service for sending notifications without notification field or should I fill issue on FirebaseMessaging for not handling alternative payload format of pushes.

The problem is that I like firebase messaging because it shows pushes in status bar for me but in case of other format I have to add external dependency as flutter_local_notifications to show pushes on screen

maysamsh commented 3 years ago

try it without notification and you will see it is not firing onMessage on iOS

hrabkin commented 3 years ago

try it without notification and you will see it is not firing onMessage on iOS

I believe you, it doesn't work on Android as well without notification field.