Skip to content

iOS background handler not always triggered #4104

Closed
@compojoom

Description

@compojoom

I've spend more time debugging this than I'm comfortable admitting, but well that's life.

If you were to search this repo for "ios background" -> you'll find around 30 open issues. Which makes 30% of all open issues. To those of you who think that react-native-firebase doesn't work with ios background notifications - you are wrong. It works! I've been in that boat and I can tell you - it works. Make sure that you compose the right message, with the correct headers. If your app is still not woken up, then use the console.app on your Mac to find out if iOS is blocking the push notifications for some reason.

Putting those things aside. I could see that our app was being woken up, but the JS backgroundHandler was not being triggered. Initially I thought that we have a bug in our JS code as the moment I commented some lines out - I could see the handler working. After some time - I noticed that it's not always the same commented out lines that make it work. It was all kind of random. I'll comment a line out - and it would work. Then I'll re-enable this line and it will continue to work... So I thought that we must be having some race condition in our code that provokes this. But after few days I realized that the part that causes this is not in our JS code, but rather on the native side. More precisely here:

      // TODO investigate later - RN bridge gets invalidated at start when in background and a new bridge created - losing all events
      // TODO   so we just delay sending the event for a few seconds as a workaround
      // TODO   most likely Remote Debugging causing bridge to be invalidated
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[RNFBRCTEventEmitter shared] sendEventWithName:@"messaging_message_received_background" body:[RNFBMessagingSerializer remoteMessageUserInfoToDict:userInfo]];
      });

https://github.com/invertase/react-native-firebase/blob/master/packages/messaging/ios/RNFBMessaging/RNFBMessaging%2BAppDelegate.m#L122

In our app the background task actually needs to load the redux store and the redux-saga code. We are talking about a lot of imports. So it turned out that despite setting the listener as one of the first things in index.js we apparently need more than 2s. I increased this to 8s and I can confirm that we are now receiving the message all the time.

So with this knowledge I think that some of the users who have opened a "ios background data message" issue actually suffer from the above problem. The OS actually delivers the message to the app, but the native code is executed before the bundle is ready and they are left with no message.

Environment

Click To Expand

react-native info output:

System:
OS: macOS 10.15.6
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Memory: 247.13 MB / 32.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 14.4.0 - /usr/local/bin/node
Yarn: 1.17.3 - /usr/local/bin/yarn
npm: 6.14.4 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.9.1 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
Android SDK:
API Levels: 28, 29
Build Tools: 28.0.3, 29.0.0, 29.0.2, 29.0.3
System Images: android-24 | Google Play Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
Android NDK: 19.0.5232133
IDEs:
Android Studio: 4.0 AI-193.6911.18.40.6514223
Xcode: 11.6/11E708 - /usr/bin/xcodebuild
Languages:
Java: 1.8.0_222 - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.11.0 => 16.11.0
react-native: 0.62.2 => 0.62.2

  • Platform that you're experiencing the issue on:

    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you're using that has this issue:
    "8.3.0",

  • Firebase module(s) you're using that has the issue:

    • "@react-native-firebase/messaging": "7.6.1",
  • Are you using TypeScript?

    • N

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions