flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [firebase_messaging] Silent notifications not handled on iOS

Open Johandrex opened this issue 4 years ago • 119 comments

Bug report

The issue is regarding silent notifications that are sent from firebase on iOS. When a silent notification is sent through firebase, the android app can handle the notification both in the foreground and background. Silent notifications can however not be handled on iOS, neither in the foreground or background, the app doesn't get any indication of the app receiving the notification.

Steps to reproduce

Steps to reproduce the behavior:

  1. From Postman, test to POST the following content to https://fcm.googleapis.com/fcm/send
    "to": "_token",
    "priority": "high",
    "data": {
        "content-available": 1
    },
  1. This results in the iOS app not receiving the notification, only the android app can receive the silent notification. However when adding the code down below to the Body, the iOS app will receive the notification. It's however not a silent notification that only can be received when the app is in the foreground "notification": { "body": "1" }

Expected behavior

The iOS app should be able to handle silent notifications, just like the android app currently can.


Additional context

The bug occurs when using 'firebase_messaging' 11.2.10, flutter 12.2.1 on iOS 15.3.1


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
/Users/user/Development/flutter/bin/flutter doctor --verbose
[✓] Flutter (Channel stable, 2.10.3, on macOS 12.2.1 21D62 darwin-x64, locale en-SE)
    • Flutter version 2.10.3 at /Users/user/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7e9793dee1 (13 days ago), 2022-03-02 11:23:12 -0600
    • Engine revision bd539267b4
    • Dart version 2.16.1
    • DevTools version 2.9.2

[!] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    ✗ 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.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Android Studio (version 2021.1)
    • 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.11+0-b60-7590822)

[✓] Connected device (1 available)
    • Snow (mobile) • XXXX-XXXX• ios • iOS 15.3.1 19D52

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.
Process finished with exit code 0

Flutter dependencies

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

Click To Expand
dependencies:
- app_settings 4.1.1 [flutter]
- connectivity_plus 2.2.1 [flutter connectivity_plus_platform_interface connectivity_plus_linux connectivity_plus_macos connectivity_plus_web connectivity_plus_windows]
- cupertino_icons 1.0.4
- device_info_plus 3.2.2 [flutter device_info_plus_platform_interface device_info_plus_macos device_info_plus_linux device_info_plus_web device_info_plus_windows]
- dio 4.0.4 [http_parser path]
- enum_to_string 2.0.1
- firebase_analytics 9.1.2 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_core 1.13.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 2.5.3 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_messaging 11.2.10 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- firebase_performance 0.8.0+7 [firebase_core firebase_core_platform_interface firebase_performance_platform_interface firebase_performance_web flutter]
- flutter 0.0.0 [characters collection material_color_utilities meta typed_data vector_math sky_engine]
- flutter_spinbox 0.8.0 [cupertino_icons flutter meta]
- flutter_svg 1.0.3 [flutter meta path_drawing vector_math xml]
- hand_signature 2.1.1 [flutter flutter_svg]
- http 0.13.4 [async http_parser meta path]
- image_picker 0.8.4+10 [flutter flutter_plugin_android_lifecycle image_picker_for_web image_picker_platform_interface]
- intl 0.17.0 [clock path]
- maps_launcher 2.0.1 [flutter flutter_web_plugins url_launcher]
- provider 6.0.2 [collection flutter nested]
- qr_code_scanner 0.7.0 [js flutter flutter_web_plugins]
- shared_preferences 2.0.13 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
- timer_builder 2.0.0 [flutter]
- uuid 3.0.6 [crypto]

dev dependencies:
- 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 typed_data]

transitive dependencies:
- args 2.3.0
- async 2.8.2 [collection meta]
- boolean_selector 2.1.0 [source_span string_scanner]
- characters 1.2.0
- charcode 1.3.1
- clock 1.1.0
- collection 1.15.0
- connectivity_plus_linux 1.3.0 [flutter connectivity_plus_platform_interface meta nm]
- connectivity_plus_macos 1.2.1 [connectivity_plus_platform_interface flutter]
- connectivity_plus_platform_interface 1.2.0 [flutter meta plugin_platform_interface]
- connectivity_plus_web 1.2.0 [connectivity_plus_platform_interface flutter_web_plugins flutter]
- connectivity_plus_windows 1.2.0 [connectivity_plus_platform_interface flutter]
- cross_file 0.3.2 [flutter js meta]
- crypto 3.0.1 [collection typed_data]
- dbus 0.7.1 [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.2 [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.2.0 [clock collection]
- ffi 1.1.2
- file 6.1.2 [meta path]
- firebase 9.0.2 [http http_parser js]
- firebase_analytics_platform_interface 3.1.1 [firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.4.0+8 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_core_platform_interface 4.2.5 [collection flutter meta plugin_platform_interface]
- firebase_core_web 1.6.1 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_crashlytics_platform_interface 3.2.1 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_platform_interface 3.2.1 [firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 2.2.9 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- firebase_performance_platform_interface 0.1.1+1 [firebase_core flutter plugin_platform_interface]
- firebase_performance_web 0.1.0+7 [firebase firebase_core firebase_core_web firebase_performance_platform_interface flutter flutter_web_plugins js]
- flutter_plugin_android_lifecycle 2.0.5 [flutter]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta typed_data vector_math]
- http_parser 4.0.0 [charcode collection source_span string_scanner typed_data]
- image_picker_for_web 2.1.6 [flutter flutter_web_plugins image_picker_platform_interface]
- image_picker_platform_interface 2.4.4 [cross_file flutter http plugin_platform_interface]
- js 0.6.3
- lints 1.0.1
- matcher 0.12.11 [stack_trace]
- material_color_utilities 0.1.3
- meta 1.7.0
- nested 1.0.0 [flutter]
- nm 0.5.0 [dbus]
- path 1.8.0
- path_drawing 1.0.0 [vector_math meta path_parsing flutter]
- path_parsing 1.0.0 [vector_math meta]
- path_provider_linux 2.1.5 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_platform_interface 2.0.3 [flutter platform plugin_platform_interface]
- path_provider_windows 2.0.5 [ffi flutter path path_provider_platform_interface win32]
- petitparser 4.4.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.2 [meta]
- process 4.2.4 [file path platform]
- shared_preferences_android 2.0.11 [flutter shared_preferences_platform_interface]
- shared_preferences_ios 2.1.0 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.1.0 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_macos 2.0.3 [flutter shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.0.0 [flutter]
- shared_preferences_web 2.0.3 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.1.0 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- source_span 1.8.1 [collection path term_glyph]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.0 [charcode source_span]
- term_glyph 1.2.0
- test_api 0.4.8 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.0 [collection]
- url_launcher 6.0.20 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]
- url_launcher_android 6.0.15 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.0.15 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.0.0 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.0.0 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.0.5 [flutter plugin_platform_interface]
- url_launcher_web 2.0.9 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.0.0 [flutter url_launcher_platform_interface]
- vector_math 2.1.1
- win32 2.4.1 [ffi]
- xdg_directories 0.2.0+1 [meta path process]
- xml 5.3.1 [collection meta petitparser]

Johandrex avatar Mar 15 '22 08:03 Johandrex

@Johandrex Can you try by setting following:

content-available: true apns-priority: 5 (for e.g)

And see if it helps ?

darshankawar avatar Mar 15 '22 11:03 darshankawar

apns-priority

Sadly, neither

{
    "to": "_token",
    "priority": "high",
    "content-available": true,
    "apns-priority": 5,
    "data": {
        "content-available": 1
    }
}

nor

{
    "to": "_token",
    "priority": "high",
    "data": {
        "content-available": true,
        "apns-priority": 5
    }
}

works.

Johandrex avatar Mar 15 '22 12:03 Johandrex

@Johandrex Since you are trying on iOS, can you try using getAPNSToken() ?

Also, do make sure you are handling foreground notifications as mentioned in the document: https://firebase.flutter.dev/docs/messaging/notifications#foreground-notifications

darshankawar avatar Mar 16 '22 11:03 darshankawar

@Johandrex Since you are trying on iOS, can you try using getAPNSToken() ?

Also, do make sure you are handling foreground notifications as mentioned in the document: https://firebase.flutter.dev/docs/messaging/notifications#foreground-notifications

Thanks for the answer, getAPNSToken() does indeed result in a token. But this can't be sent to firebase through postman as it returns the following error "error": "InvalidRegistration"

The foreground notifications are handled in the same way as the link explains.

Also, I just want to reiterate that the notifications does reach the phone when "notification": { "body": "1" } is set. However, when removing notification it's not handled by the plugin on iOS, only Android.

Johandrex avatar Mar 16 '22 12:03 Johandrex

However, when removing notification it's not handled by the plugin on iOS, only Android.

Ok, this is a new update for me based on our conversation so far :-) which has given a new angle to the issue, probably I was trying to give you solutions from a different perspective.

Also, how is Android behavior when it comes to handling this behavior ?

darshankawar avatar Mar 17 '22 10:03 darshankawar

However, when removing notification it's not handled by the plugin on iOS, only Android.

Ok, this is a new update for me based on our conversation so far :-) which has given a new angle to the issue, probably I was trying to give you solutions from a different perspective.

Also, how is Android behavior when it comes to handling this behavior ?

Sorry, might not've explained it clearly enough. The Android behavior works as expected. When receiving the silent notification on Android it's being handled by the app in both the foreground and background.

But when receiving the exact same type of silent notification on iOS there's no indication of the app receiving the silent notification.

See the notification service I have created down below.


import 'dart:async';
import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';

class NotificationService {
  static final _firebaseMessaging = FirebaseMessaging.instance;

  final notificationController = StreamController<RemoteMessage>.broadcast();

  NotificationService() {
    FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
    FirebaseMessaging.onMessage.listen(
      (message) async {
        print("Notification Received");
        notificationController.sink.add(message);
      },
    );
  }

  dispose() {
    notificationController.close();
  }
}

Future<void> onBackgroundMessage(RemoteMessage message) async {
  await Firebase.initializeApp();

  print("Notification Received");

  // handle the notification
}

Johandrex avatar Mar 17 '22 12:03 Johandrex

Thanks for the update. I am labeling this for further insights from the team.

/cc @russellwheatley

darshankawar avatar Mar 18 '22 11:03 darshankawar

I'm having the same issues on firebase_messaging: ^11.2.11 Though downgrading to 9.0.0 doesn't help either

Upgrading my Flutter SDK didn't help either.

Code:

FirebaseMessaging.onMessage.listen((message) async {
  Log.info(message);
  Log.info(message.notification?.title);
  Log.info(message.notification?.body);
  Log.info(message.data);

  // ...
});

It works with non-data-only notifications. But data-only never trigger the .listen callback

VKBobyr avatar Mar 18 '22 22:03 VKBobyr

I had to downgrade to firebase_messaging: 11.2.8 (The previous version I was on) which worked for me. I haven't tested the other versions.

Faaatman avatar Mar 20 '22 11:03 Faaatman

I had to downgrade to firebase_messaging: 11.2.8 (The previous version I was on) which worked for me. I haven't tested the other versions.

Will try this shortly, how did the notification you sent to firebase look like? Did it contain a 'data' field etc? Does the silent notification work in both the background and foreground?

Johandrex avatar Mar 20 '22 12:03 Johandrex

I had to downgrade to firebase_messaging: 11.2.8 (The previous version I was on) which worked for me. I haven't tested the other versions.

Will try this shortly, how did the notification you sent to firebase look like? Did it contain a 'data' field etc? Does the silent notification work in both the background and foreground?

Did it contain a 'data' field etc?

Yes.

Does the silent notification work in both the background and foreground?

Yes for background I used FirebaseMessaging.onBackgroundMessage(MyBackgroundHandler); And for the foreground, I listened to the notifications as follows:

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (notification == null ) { // this is a silent notification
        // handle silent notification here
      }  else {
       // handle normal notifications
      }
}

The notification options are the following:

options = {
      data: {
        mydata: "data",
      },
      apns: {
        headers: {
          apns_push_type: "background",
          apns_priority: 5, # Must be `5` when `contentAvailable` is set to true.
        },
      },
      android: {
        priority: "high",
        notification: {
            "channel_id": "all_notifications",
          },
      },
      content_available: true,
    }

Faaatman avatar Mar 20 '22 12:03 Faaatman

I have the exact same issue. This is urgent!

I have content available set to true.

The cases:

Notification AND data field: Android:

  • Activates onMessage in foreground (as expected)
  • Activates onBackgroundMessage in background (as expected)

iOS:

  • Activates onMessage in foreground (as expected)
  • Does NOT activate onBackground message

Data field only: Android:

  • Activates onMessage field (as expected)
  • Activates onBackgroundMessage (as expected)

iOS:

  • Does NOT activate onMessage in foreground
  • does NOT activate onBackgroundMessage

Summary: I’m experiencing the same issue. Data only notifications do not call any handlers on iOS. The only way data is received is when in foreground AND with the notification field. Android works perfectly fine as expected for all cases.

This is critical, as practically all push notification functionality is gone in iOS!

ps-tech avatar Mar 20 '22 23:03 ps-tech

@ps-tech Did downgrading to 11.2.8 help?

VKBobyr avatar Mar 21 '22 02:03 VKBobyr

No, it did not.

I have push notification capability enabled, background fetch, processing, and remote notifications capabilities enabled. Like I said, sending on android works perfectly fine, and sending on iOS works if there is a 'notification' field (I see the notifications pop up).

I am absolutely at a loss! I have even tried the firebase_messaging example project with switching it to my firebase_options.dart. It exhibits the same behavior– receives notifications just fine unless they are data-only / silent notifications.

I am going to try myself to make an absolutely bare bones project, even more basic than the example, and I will get back to you.

ps-tech avatar Mar 21 '22 04:03 ps-tech

@ps-tech Could you show us your pubspec file?

Faaatman avatar Mar 21 '22 05:03 Faaatman

I made the most barebones possible example. It worked with background notifications, but did not work receiving data only notifications in foreground.

On my original app, my onMessage is only called if contentAvailable is NOT set. If I set contentAvailable to true, the notification never triggers onMessage OR onBackgroundMessage.

Then, on my new barebones app, which is literally only firebase configuration (including a new project in firebase console), it triggers both onMessage and onBackgroundMessage, but only if 'notification' is set will it trigger onMessage.

In neither app nor in all of my testing have I seen a case when onBackgroundMessage is called WITHOUT disabling swizzling. To be clear, the only way even my barebones app receives notifications is by setting FirebaseAppDelegateProxyEnabled to false in my info.plist and adding to my AppDelegate:

  override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
    super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
  }

Here is my pubspec on my new barebones:

dependencies:
  flutter:
    sdk: flutter
  firebase_messaging: ^11.2.11
  firebase_core: ^1.13.1

So, in my barebones app, onBackgroundMessage works perfectly, then onMessage only works if contentAvilable is false. In my real app, onBackgroundMessage never works, and onMessage does not work on data-only messages.

What factors could be influencing these shocking differences across projects?

ps-tech avatar Mar 21 '22 06:03 ps-tech

@ps-tech in your project only get firebase_messaging: 11.2.8 Without the ^ Since the issue is in 11.2.10 or higher the ^ means get this version or higher in 11.2 Ex: firebase_messaging: ^11.2.8 This means get 11.2.x where x >= 8 And in your case it will always get the version that has the issue we're all facing.

Faaatman avatar Mar 21 '22 06:03 Faaatman

Sorry, yes I tried that. I copy pasted my pubspec after it didn’t work and I switched it back.

ps-tech avatar Mar 21 '22 06:03 ps-tech

@ps-tech in your project only get firebase_messaging: 11.2.8 Without the ^ Since the issue is in 11.2.10 or higher the ^ means get this version or higher in 11.2 Ex: firebase_messaging: ^11.2.8 This means get 11.2.x where x >= 8 And in your case it will always get the version that has the issue we're all facing.

Same, also tested this. I've cleaned flutter and ran a new 'pod install' with firebase_messaging 11.2.8. The FCM payload you recommended

{
    "to": "token",
    "priority": "high",
    "data": {
        "mydata": "data"
    },
    "apns": {
        "headers": {
            "apns_push_type": "background",
            "apns_priority": 5
        },
        "content_available": true
    },
}

Didn't work, until I added "notification": { "body": "1" }, meaning the notification no longer is silent.

So, in my barebones app, onBackgroundMessage works perfectly, then onMessage only works if contentAvilable is false. In my real app, onBackgroundMessage never works, and onMessage does not work on data-only messages.

What factors could be influencing these shocking differences across projects?

Indeed, why does it work for some?

Which iOS version does the phone you're testing on have, and which flutter version are you using? @Faaatman

Johandrex avatar Mar 21 '22 09:03 Johandrex

@Johandrex as I recall when I was implementing it having content_available in apns didn't work unless I moved it to be next to apns. (I have no idea why it worked like that)

Which iOS version does the phone you're testing on have, and which flutter version are you using?

iOS version 15.4, Flutter version 2.10.3

Faaatman avatar Mar 21 '22 10:03 Faaatman