Search code examples
iosflutterdartfirebase-cloud-messagingpyfcm

Firebase messaging problems under iOS


Please see new findings below

Despite following the configuration steps in the Firebase_messaging docs, I cannot get FCM to work properly on iOS.

DATA messages work as intended. NOTIFICATION messages do not show up at all, whether app is in foreground or background.

Android notifications work as intended. This problem only occurs on iOS, and only for notification messages.

I am testing on a physical device (iPhone SE) and continue to have the same issue even when the app is deployed using TestFlight.

void configureFirebase(prefs) async {
  try {
    await _firebaseMessaging.requestNotificationPermissions();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print(message);
        onMessage(message);
      },
      //  onBackgroundMessage: myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
        onMessage(message);
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
        onMessage(message);
      },
    );
  } catch (e) {
    print(e);
  }
}

When the App has compiled, I get the following:

2020-02-09 11:39:34.414043+0100 Runner[236:3651] Configuring the default Firebase app...
2020-02-09 11:39:34.439940+0100 Runner[236:3782] 6.16.0 - [Firebase/Core][I-COR000004] App with name __FIRAPP_DEFAULT does not exist.
2020-02-09 11:39:34.462336+0100 Runner[236:3651] Configured the default Firebase app __FIRAPP_DEFAULT.
2020-02-09 11:39:34.509495+0100 Runner[236:3797] flutter: Observatory listening on http://127.0.0.1:49413/sW_P16TvXjA=/
2020-02-09 11:39:34.530782+0100 Runner[236:3782] 6.16.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
2020-02-09 11:39:34.535881+0100 Runner[236:3783] 6.16.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60202000 started
2020-02-09 11:39:34.536403+0100 Runner[236:3783] 6.16.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see https://help.apple.com/xcode/mac/8.0/#/dev3ec8a1cb4)
2020-02-09 11:39:34.562674+0100 Runner[236:3783] 6.16.0 - [Firebase/Analytics][I-ACS031025] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist
2020-02-09 11:39:34.567991+0100 Runner[236:3782] 6.16.0 - [Firebase/Analytics][I-ACS023012] Analytics collection enabled

If I disable FirebaseAppDelegateProxyEnabled in info.plist, below is the stack:

2020-02-09 12:23:03.053017+0100 Runner[290:10393] Configuring the default Firebase app...
2020-02-09 12:23:03.054198+0100 Runner[290:10584] 6.16.0 - [Firebase/Core][I-COR000004] App with name __FIRAPP_DEFAULT does not exist.
2020-02-09 12:23:03.099604+0100 Runner[290:10393] Configured the default Firebase app __FIRAPP_DEFAULT.
2020-02-09 12:23:03.142294+0100 Runner[290:10596] flutter: Observatory listening on http://127.0.0.1:49543/dD-x0XTr2qQ=/
2020-02-09 12:23:03.151748+0100 Runner[290:10583] 6.16.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60202000 started
2020-02-09 12:23:03.152298+0100 Runner[290:10583] 6.16.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see https://help.apple.com/xcode/mac/8.0/#/dev3ec8a1cb4)
2020-02-09 12:23:03.153920+0100 Runner[290:10599] 6.16.0 - [Firebase/Analytics][I-ACS025036] App Delegate Proxy is disabled
2020-02-09 12:23:03.166991+0100 Runner[290:10599] 6.16.0 - [Firebase/Analytics][I-ACS031025] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist
2020-02-09 12:23:03.172381+0100 Runner[290:10580] 6.16.0 - [Firebase/Analytics][I-ACS023012] Analytics collection enabled

Any idea what could be happening here? It's an extremely weird behavior which I have no idea from where to even start debugging.

Any help or leads onto potential solutions would be greatly appreciated.


Adding message configuration used in PyFCM just in case that was the source of the issue:

push_service = FCMNotification(api_key=fcmServerKey)

testDATA = {
  'title': 'Test title',
  'body': 'Test body',
  'click_action': 'FLUTTER_NOTIFICATION_CLICK',
  'otherFields': 'Other fields here',
}

result = push_service.notify_single_device(
  registration_id = token,
  message_title='Test title',
  message_body='Test body',
  data_message = testDATA,
  time_to_live=0,
  delay_while_idle=False,
  extra_notification_kwargs=extraKWARGS,
  content_available=True  
)

New findings:

NOTIFICATIONS are received in the iOS simulator. However, they only show up when the app is in the foreground. Push notifications are NOT working. However, I've read that push notifications do not work on iOS simulator, so that might be why.

Considering I have no apparent bugs, and have been debugging this to no avail for around 30 hours now, how likely is it that my issue with notifications is isolated to this one physical device (the iPhone SE in my possession) possibly due to FCM considering it offline or its registration ID invalid?.

I have no other iPhones to test on.

The application is fully functional and ready in all other regards... Should I consider this an isolated case and submit my app?


Solution

  • I've finally solved this issue.

    I'm running firebase_messaging 6.0.9 and Flutter 1.12.13+8, MacOS Catalina with iOS 13 on iPhone SE.

    Let's get one thing out of the way: The behavior of firebase_messaging on iOS is buggy.

    What I essentially did was:

    1) Clean the code and ensure the FCM implementation was exactly like the docs.

    2) Have FCM permissions run alone, without any other permissions happening at the same time

    3) Keep creating new projects and copying the lib inside them until one of them works[*]

    So yeah, it works now. An unsatisfying solution, but a solution nonetheless.

    [*] Once I was sure the code had practically no reasons of failing, FCM was still not running. I did everything within my power to clean the project and Flutter packages; yet FCM did not run on iOS. So I created another project, and copied my code inside it. And then I started getting different bugs which I never managed to solve. So I kept creating new projects, each resulting in a new, exotic, and unsolveable bug on which I'd spend a few hours trying to debug; sometimes getToken was not detected, other times the token was null; other times requestNotifications was not detected; others firebase_messaging was not detected... etc. Then one of the installations worked. And now notifications seem to work as intended on iOS.


    Edit:

    It went back to not working after making some changes to the code and recompiling (even though I made no changes to the FCM module or XCode scripts).

    Resetting the iPhone fixed it this time around. Running this in production will be a nightmare.