You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS14.x生产环境Bugsnag主线程调用随机异常求助

Solution for iOS 14.x NSInternalInconsistencyException with react-native-firebase 5.5.5

Hey there, let's break down how to fix this random main-thread crash tied to Firebase push notifications on iOS 14.0/14.0.1. This issue stems from older versions of react-native-firebase not properly handling thread safety for iOS 14's notification system—some Firebase callbacks fire on background threads but require main-thread execution for React Native operations, triggering that folly-related exception.

1. Upgrade Dependencies (Long-Term, Most Reliable Fix)

Version 5.5.5 of react-native-firebase is outdated and doesn't account for iOS 14's threading changes. Upgrading to a supported version will resolve the root cause:

  • Uninstall the old package: npm uninstall react-native-firebase
  • Install a compatible newer version (v6.x+ works with React Native 0.61.4): npm install @react-native-firebase/app @react-native-firebase/messaging
  • Reinstall iOS pods: cd ios && pod install && cd ..
  • Update your GoogleService-Info.plist to match the latest Firebase SDK requirements
  • Adjust your notification setup code to align with the v6.x API (most changes are minor, like importing from @react-native-firebase/messaging instead of the old react-native-firebase path)

2. Manual Thread Safety Fix (Temporary Workaround)

If upgrading isn't feasible right now, you can force Firebase notification callbacks to run on the main thread. Here's how to implement this:

iOS Native Code Adjustment

Open your AppDelegate.m and wrap Firebase notification handling in a main-thread dispatch:

// AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // Force Firebase processing to run on main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        [[RNFirebaseMessaging instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
    });
}

// Handle foreground notifications the same way
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[RNFirebaseMessaging instance] willPresentNotification:notification withCompletionHandler:completionHandler];
    });
}

React Native JS Adjustment

In your JS code, ensure any UI or state updates triggered by notifications are wrapped to run on the main thread:

import { InteractionManager, Alert } from 'react-native';
import messaging from 'react-native-firebase/messaging'; // For v5.x

messaging().onNotification((remoteMessage) => {
    // Use InteractionManager to guarantee main-thread execution for UI updates
    InteractionManager.runAfterInteractions(() => {
        Alert.alert('New Notification', remoteMessage.notification.body);
        // Add your state update or navigation logic here
    });
});

3. Additional Debugging & Mitigation

  • Capture Thread Context in Bugsnag: Modify your crash reporting to log the current thread name when the exception occurs. This will help you pinpoint exactly which Firebase callback is firing off-thread.
  • Disable Firebase App Delegate Proxy: Add FirebaseAppDelegateProxyEnabled to your Info.plist with a value of NO. This forces manual handling of all notification logic, avoiding automatic thread mismanagement from the proxy. Just make sure you implement all required notification delegate methods in your AppDelegate.

内容的提问来源于stack exchange,提问作者Sonam Maniar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 18:12:37