Flutterfire: πŸ› [FIREBASE_MESSAGING] iOS: message callbacks do not fire when push notification is sent via APNS token

Created on 2 Dec 2020  Β·  7Comments  Β·  Source: FirebaseExtended/flutterfire

Bug report

Describe the bug
On iOS devices,
I upgraded to Firebase Messaging v8.0.0-dev.9
With it, I was able to use getAPNSToken() and successfully send this 64bit hexadecimal to a third party chat service.
When I send a push notification from the third party chat service console and app is on the background, iOS shows the notification banner.
My code looks like this:

      Future<void> func(message) async => print("got message in background");
      .....
      .....
      FirebaseMessaging.onMessage.listen((message) {
          print("message received");
      });
      FirebaseMessaging.onBackgroundMessage(func);
      FirebaseMessaging.onMessageOpenedApp.listen((message) {
          print("opened message");
      });

The onMessageOpenedApp and onBackgroundMessage functions do not get triggered when a notification is sent through the third party chat service while the app is in the background.
However onMessageOpenedApp triggers when I send a message from Firebase Cloud Messaging. Note that onBackgroundMessage does not fire even with Firebase Cloud Messaging.

Also the onMessage function does not get triggered when a notification is sent through the third party chat service while the app is in the foreground.

Steps to reproduce

Steps to reproduce the behavior:

  1. use getAPNSToken() to get the native token for iOS.
  2. send this APNS token to the third party chat service.
  3. send a notification from the third party chat service
  4. take the app to the background
  5. observe that the notification banner is received successfully while onBackgroundMessage is not being triggered.
  6. take the app to the foreground
  7. send a notification from the third party chat service
  8. onMessage is not triggered and the notification is totally omitted by Firebase Messaging

Expected behavior

We should receive the notifications send to our app thorugh onMessage and onBackgroundMessage no matter if they are sent using APNS or FCM token.


Additional context

Add any other context about the problem here.


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand

[βœ“] Flutter (Channel stable, 1.22.4, on macOS 11.0.1 20B29 darwin-x64, locale en-TR)
    β€’ Flutter version 1.22.4 at /Users/berat/Documents/Development/flutter
    β€’ Framework revision 1aafb3a8b9 (3 weeks ago), 2020-11-13 09:59:28 -0800
    β€’ Engine revision 2c956a31c0
    β€’ Dart version 2.10.4

[βœ“] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    β€’ Android SDK at /Users/berat/Library/Android/sdk
    β€’ Platform android-30, build-tools 30.0.2
    β€’ Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    β€’ Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    β€’ All Android licenses accepted.

[βœ“] Xcode - develop for iOS and macOS (Xcode 12.2)
    β€’ Xcode at /Applications/Xcode.app/Contents/Developer
    β€’ Xcode 12.2, Build version 12B45b
    β€’ CocoaPods version 1.10.0

[!] Android Studio (version 4.1)
    β€’ Android Studio at /Applications/Android Studio.app/Contents
    βœ— Flutter plugin not installed; this adds Flutter specific functionality.
    βœ— Dart plugin not installed; this adds Dart specific functionality.
    β€’ Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[βœ“] VS Code (version 1.51.1)
    β€’ VS Code at /Applications/Visual Studio Code.app/Contents
    β€’ Flutter extension version 3.16.0

[βœ“] Connected device (1 available)
    β€’ Ibrahim Berat’s iPhone (mobile) β€’ 4908a07faafc82e9575c22f23c588d81e74ed3c9 β€’ ios β€’ iOS 14.2

! Doctor found issues in 1 category.


Flutter dependencies

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

Click To Expand

Dart SDK 2.10.4
Flutter SDK 1.22.4
bare_bones 1.0.0+1

dependencies:
- auto_route 0.6.9 [flutter meta]
- bloc 6.1.0 [meta]
- camera 0.5.8+11 [flutter]
- cloud_firestore 0.14.3+1 [flutter meta quiver firebase_core firebase_core_platform_interface cloud_firestore_platform_interface cloud_firestore_web]
- cloud_functions 0.7.1 [meta flutter firebase_core firebase_core_platform_interface cloud_functions_platform_interface cloud_functions_web]
- connectivity 2.0.2 [flutter meta connectivity_platform_interface connectivity_macos connectivity_for_web]
- connectycube_sdk 1.0.0 [flutter http http_parser crypto package_info uuid intl mime xmpp_stone device_id crossplat_objectid path flutter_webrtc web_socket_channel]
- cupertino_icons 1.0.0
- dartz 0.9.2
- dash_chat 1.1.15 [flutter flutter_parsed_text uuid intl transparent_image]
- firebase_auth 0.18.3+1 [meta firebase_core firebase_core_platform_interface firebase_auth_platform_interface firebase_auth_web flutter]
- firebase_core 0.5.2+1 [firebase_core_platform_interface flutter quiver meta firebase_core_web]
- firebase_messaging 8.0.0-dev.9 [meta flutter firebase_core firebase_core_platform_interface firebase_messaging_platform_interface]
- firebase_storage 5.1.0 [flutter firebase_core firebase_core_platform_interface firebase_storage_web firebase_storage_platform_interface quiver]
- flushbar 1.10.4 [flutter]
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]
- flutter_bloc 6.1.1 [flutter bloc provider]
- flutter_ffmpeg 0.3.0 [flutter]
- flutter_local_notifications 3.0.1+6 [flutter platform flutter_local_notifications_platform_interface timezone]
- flutter_localizations 0.0.0 [flutter intl characters collection meta path typed_data vector_math]
- flutter_signin_button 1.1.0 [flutter font_awesome_flutter]
- freezed 0.12.2 [analyzer build build_config meta source_gen freezed_annotation]
- gallery_saver 2.0.1 [flutter path_provider http path]
- geocoding 1.0.5 [flutter geocoding_platform_interface url_launcher]
- geolocator 6.1.8+1 [flutter geolocator_platform_interface]
- get_it 5.0.1 [async meta]
- google_sign_in 4.5.6 [google_sign_in_platform_interface flutter meta google_sign_in_web]
- injectable 1.0.5 [get_it]
- lamp 0.0.6 [flutter]
- logger 0.9.4
- path 1.8.0-nullsafety.1
- path_provider 1.6.24 [flutter path_provider_platform_interface path_provider_macos path_provider_linux path_provider_windows]
- permission_handler 5.0.1+1 [flutter meta permission_handler_platform_interface]
- rxdart 0.24.1
- screen 0.0.5 [flutter]
- shared_preferences 0.5.12+4 [meta flutter shared_preferences_platform_interface shared_preferences_linux shared_preferences_macos shared_preferences_web shared_preferences_windows]
- sign_in_with_apple 2.5.4 [meta flutter]
- uuid 2.2.2 [crypto convert]
- video_player 1.0.1 [meta video_player_platform_interface video_player_web flutter]

dev dependencies:
- auto_route_generator 0.6.10 [build source_gen analyzer path build_runner auto_route]
- bloc_test 7.1.0 [bloc test meta mockito]
- build_runner 1.10.4 [args async build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style glob graphs http_multi_server io js logging meta mime 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]
- freezed_annotation 0.12.0 [collection json_annotation meta]
- injectable_generator 1.0.6 [build source_gen path glob analyzer injectable collection]
- json_serializable 3.5.0 [analyzer build build_config json_annotation meta path source_gen]
- lint 1.3.0

dependency overrides:
- analyzer 0.40.4 [_fe_analyzer_shared args charcode cli_util collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]

transitive dependencies:
- _fe_analyzer_shared 11.0.0 [meta]
- archive 2.0.13 [crypto args path]
- args 1.6.0
- async 2.5.0-nullsafety.1 [collection]
- boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner]
- build 1.5.0 [analyzer async convert crypto logging meta path glob]
- build_config 0.4.2 [checked_yaml json_annotation meta path pubspec_parse yaml]
- build_daemon 2.1.4 [built_collection built_value http_multi_server logging pedantic path pool shelf shelf_web_socket stream_transform watcher web_socket_channel]
- build_resolvers 1.4.3 [analyzer build crypto graphs logging meta path package_config pool pub_semver]
- build_runner_core 6.0.3 [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 4.3.2 [collection quiver]
- built_value 7.1.0 [built_collection collection fixnum quiver]
- characters 1.1.0-nullsafety.3
- charcode 1.2.0-nullsafety.1
- checked_yaml 1.0.2 [json_annotation source_span yaml]
- cli_util 0.2.0 [path]
- clock 1.1.0-nullsafety.1
- cloud_firestore_platform_interface 2.2.0 [flutter meta collection firebase_core plugin_platform_interface]
- cloud_firestore_web 0.2.1+1 [flutter flutter_web_plugins http_parser meta firebase_core firebase_core_web cloud_firestore_platform_interface js]
- cloud_functions_platform_interface 3.0.2 [flutter meta firebase_core plugin_platform_interface]
- cloud_functions_web 3.1.0 [firebase_core firebase_core_web cloud_functions_platform_interface flutter flutter_web_plugins http_parser meta js]
- code_builder 3.5.0 [built_collection built_value collection matcher meta]
- collection 1.15.0-nullsafety.3
- connectivity_for_web 0.3.1+4 [connectivity_platform_interface flutter_web_plugins flutter]
- connectivity_macos 0.1.0+7 [flutter]
- connectivity_platform_interface 1.0.6 [flutter meta plugin_platform_interface]
- console 3.1.0 [vector_math]
- convert 2.1.1 [charcode typed_data]
- coverage 0.14.1 [args logging package_config path source_maps stack_trace vm_service]
- crossplat_objectid 2.1.4
- crypto 2.1.5 [collection convert typed_data]
- cryptoutils 0.4.0 [collection]
- dart_style 1.3.9 [analyzer args path source_span]
- device_id 0.2.0 [flutter]
- fake_async 1.2.0-nullsafety.1 [clock collection]
- ffi 0.1.3
- file 5.2.1 [intl meta path]
- firebase_auth_platform_interface 2.1.3 [flutter meta firebase_core plugin_platform_interface]
- firebase_auth_web 0.3.2+1 [flutter flutter_web_plugins meta http_parser intl firebase_core firebase_core_web firebase_auth_platform_interface js]
- firebase_core_platform_interface 2.1.0 [flutter meta plugin_platform_interface quiver]
- firebase_core_web 0.2.1+1 [firebase_core_platform_interface flutter flutter_web_plugins meta js]
- firebase_messaging_platform_interface 1.0.0-dev.6 [flutter meta firebase_core plugin_platform_interface]
- firebase_storage_platform_interface 1.0.1 [flutter meta collection firebase_core plugin_platform_interface]
- firebase_storage_web 0.1.1 [async crypto firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta]
- fixnum 0.10.11
- flutter_local_notifications_platform_interface 2.0.0+1 [flutter plugin_platform_interface]
- flutter_parsed_text 1.2.5 [flutter]
- flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math]
- flutter_webrtc 0.4.0 [flutter]
- font_awesome_flutter 8.10.0 [flutter]
- geocoding_platform_interface 1.0.1+1 [flutter meta plugin_platform_interface]
- geolocator_platform_interface 1.0.8 [flutter meta plugin_platform_interface vector_math]
- glob 1.2.0 [async collection node_io path pedantic string_scanner]
- google_sign_in_platform_interface 1.1.2 [flutter meta quiver]
- google_sign_in_web 0.9.2 [google_sign_in_platform_interface flutter flutter_web_plugins meta js]
- graphs 0.2.0
- http 0.12.2 [http_parser path pedantic]
- http_multi_server 2.2.0 [async]
- http_parser 3.1.4 [charcode collection source_span string_scanner typed_data]
- image 2.1.19 [archive xml meta]
- intl 0.16.1 [path]
- io 0.3.4 [charcode meta path string_scanner]
- js 0.6.3-nullsafety.1
- json_annotation 3.1.0
- logging 0.11.4
- matcher 0.12.10-nullsafety.1 [stack_trace]
- meta 1.3.0-nullsafety.3
- mime 0.9.7
- mockito 4.1.3 [analyzer build code_builder collection dart_style matcher meta source_gen test_api]
- nested 0.0.4 [flutter]
- node_interop 1.1.1 [js]
- node_io 1.1.1 [node_interop path]
- node_preamble 1.4.12
- package_config 1.9.3 [path charcode]
- package_info 0.4.3+2 [flutter]
- path_provider_linux 0.0.1+2 [path xdg_directories path_provider_platform_interface flutter]
- path_provider_macos 0.0.4+4 [flutter]
- path_provider_platform_interface 1.0.3 [flutter meta platform plugin_platform_interface]
- path_provider_windows 0.0.4+1 [path_provider_platform_interface meta path flutter ffi win32]
- pedantic 1.10.0-nullsafety.1
- permission_handler_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- petitparser 3.1.0 [meta]
- platform 2.2.1
- plugin_platform_interface 1.0.3 [meta]
- pool 1.5.0-nullsafety.1 [async stack_trace]
- process 3.0.13 [file intl meta path platform]
- provider 4.3.2+2 [flutter nested collection]
- pub_semver 1.4.4 [collection]
- pubspec_parse 0.1.5 [checked_yaml json_annotation pub_semver yaml]
- quiver 2.1.3 [matcher meta]
- shared_preferences_linux 0.0.2+4 [file flutter meta path path_provider_linux shared_preferences_platform_interface]
- shared_preferences_macos 0.0.1+11 [shared_preferences_platform_interface flutter]
- shared_preferences_platform_interface 1.0.4 [meta flutter]
- shared_preferences_web 0.1.2+7 [shared_preferences_platform_interface flutter flutter_web_plugins meta]
- shared_preferences_windows 0.0.1+3 [shared_preferences_platform_interface flutter ffi file meta path path_provider_platform_interface path_provider_windows]
- shelf 0.7.9 [async collection http_parser path stack_trace stream_channel]
- shelf_packages_handler 2.0.0 [path shelf shelf_static]
- shelf_static 0.2.8 [convert http_parser mime path shelf]
- shelf_web_socket 0.2.3 [shelf web_socket_channel stream_channel]
- sky_engine 0.0.99
- source_gen 0.9.7+1 [analyzer async build dart_style glob meta path pedantic source_span]
- source_map_stack_trace 2.1.0-nullsafety.2 [path stack_trace source_maps]
- source_maps 0.10.10-nullsafety.1 [source_span]
- source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph]
- stack_trace 1.10.0-nullsafety.1 [path]
- stream_channel 2.1.0-nullsafety.1 [async]
- stream_transform 1.2.0
- string_scanner 1.1.0-nullsafety.1 [charcode source_span]
- synchronized 2.2.0+2
- term_glyph 1.2.0-nullsafety.1
- test 1.16.0-nullsafety.5 [analyzer async boolean_selector coverage http 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]
- test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher]
- test_core 0.3.12-nullsafety.5 [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]
- timezone 0.5.9 [path]
- timing 0.1.1+2 [json_annotation]
- transparent_image 1.0.0
- tuple 1.0.3 [quiver]
- typed_data 1.3.0-nullsafety.3 [collection]
- unorm_dart 0.1.2
- url_launcher 5.7.10 [flutter url_launcher_platform_interface url_launcher_web url_launcher_linux url_launcher_macos url_launcher_windows]
- url_launcher_linux 0.0.1+4 [flutter]
- url_launcher_macos 0.0.1+9 [flutter]
- url_launcher_platform_interface 1.0.9 [flutter meta plugin_platform_interface]
- url_launcher_web 0.1.5+1 [url_launcher_platform_interface flutter flutter_web_plugins meta]
- url_launcher_windows 0.0.1+3 [flutter]
- vector_math 2.1.0-nullsafety.3
- video_player_platform_interface 2.2.0 [flutter meta]
- video_player_web 0.1.4 [flutter flutter_web_plugins meta video_player_platform_interface]
- vm_service 5.2.0 [meta]
- watcher 0.9.7+15 [async path pedantic]
- web_socket_channel 1.1.0 [async crypto stream_channel]
- webkit_inspection_protocol 0.7.3 [logging]
- win32 1.7.3 [ffi]
- xdg_directories 0.1.2 [meta path process]
- xml 4.5.1 [collection convert meta petitparser]
- xmpp_stone 0.3.1 [xml cryptoutils crypto synchronized tuple unorm_dart image quiver console intl]
- yaml 2.2.1 [charcode collection string_scanner source_span]


messaging bug

Most helpful comment

I also have a similar issue. I apologize if this is already mentioned if the documentation and maybe I have missed it, but do onMessage and onBackgroundMessage not get triggered when a message is sent with APNS, and does onBackgroundMessage not work with iOS? I have no issues on Android but on iOS onMessage does not get triggered when an APNS message is sent via the Admin SDK, and onBackgroundMessage does not get triggered at all with both APNS or FCM.

All 7 comments

Hi @aytunch
On iOS you would need to add "content_available": true, to the message payload to temporarily wake the app and trigger a background message callback.
Are you including this in your payload?
Thank you

@markusaksli-nc from the console of the chat service I check the content_available flag but nothing changes.
E710FE24-7F55-4266-9646-365C2273C95A

Does the background callback work if you send a notification through FCM?

Such as

{
  "notification":{
    "title": "This is the title",
    "body": "This is the body",
  },
  "content_available": true,
  "priority": "high",
  "data": {
    "id": 15,
    "type": "comment"
  },
  "to": "FCM token"
}

@markusaksli-nc Hi again:)

I made several tests and below are the summary of the situation:

When the client iOS app is on background, no matter what the content-available is set to, we only see the notification banner. onBackgroundMessage is never triggered. This is same with both FCM and 3rd party chat service(using APNS token)

When the client iOS app is on foreground;
Push notification through FCM console triggers onMessage
But 3rd party chat service APNS console does not trigger onMessage

image (1)

My suspicion is that in this case contentAvailable is not being set correctly. In this case, it will seem like it is doing nothing.

The payload you sent seems to be meant for Firebase Admin SDK for Node.js. The FCM HTTP ref describes the property content_available as:

content_available | Optional, boolean | On iOS, use this field to represent content-available in the APNs payload.

The payload that I commented is what works for me when sending background messages.

curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d @send.json -H "Authorization: key=<Cloud Messaging key>"

With this it should at least work through FCM, however, I'm not sure how the third-party notification payload is handled so I can't offer any advice there.

I also have a similar issue. I apologize if this is already mentioned if the documentation and maybe I have missed it, but do onMessage and onBackgroundMessage not get triggered when a message is sent with APNS, and does onBackgroundMessage not work with iOS? I have no issues on Android but on iOS onMessage does not get triggered when an APNS message is sent via the Admin SDK, and onBackgroundMessage does not get triggered at all with both APNS or FCM.

I'm facing the same issue on iOS.
Android is working fine with both onMessage and onBackgroundMessage.
iOS isn't triggering onMessage or onBackgroundMessage when the app is terminated.

(when the app is in background or foreground it's working fine)

Was this page helpful?
0 / 5 - 0 ratings