iOS 异常:'必须在主线程上进行调用'

8
我正在开发一个React Native项目,通过Firebase Cloud Messaging接收推送通知。我已经实现了onNotification(),onNotificationOpened()和getInitialNotification()方法。尽管当应用程序在前台和被强制关闭时,通知运作正常,但是当应用程序在后台(没有被强制关闭)时打开通知时,出现错误。下面是错误截图。我尝试过更改/注释掉通知代码,但错误仍然存在。我对React Native非常新手,不确定为什么会出现这个特定问题。希望能在这方面得到任何帮助。我已经更新了与Firebase和云消息相关的所有Pods。下面也是React Native代码。我还没有在Xcode中编写任何代码,因此Pod文件保持原样。
async createNotificationListeners() {
    this.notificationListener =  firebase.notifications().onNotification((notification) => {
        if (notification) {
            //const {title, body, data} = notification;
            //this.displayNotification(title, body, data);
        }
    });

    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notification)  =>  {
        const {data} = notification.notification;
        if (data.url !== undefined) {
            this.navigate2URL(data.url);
        }else {
            this.setState({url: 'test'});
            this.webRef.reload();

        }
        //this.displayNotification(title, body + data);
    });

   const notificationOpen = await  firebase.notifications().getInitialNotification();
    if (notificationOpen) {
        const {data} = notificationOpen.notification;
        if (data.url !== undefined) {
            this.setState({url: data.url});
        } else {
            this.setState({url: 'test'});
        }
        this.webRef.reload();
        //this.displayNotification(title, body);
    }
}

你能添加一些代码吗...这样我们就可以帮助你了。 - Mahendra
1
我遇到了完全相同的问题...使用iOS 14和react-native-firebase 5.5.6。 - Null
这个问题解决了吗?我也遇到了同样的问题。 - iambinodstha
1个回答

19

尝试在./node_modules/react-native-firebase/ios/RNFirebase/notifications/RNFirebaseNotifications.m中使用此try-catch

RCT_EXPORT_METHOD(complete:(NSString*)handlerKey fetchResult:(UIBackgroundFetchResult)fetchResult) {
if (handlerKey != nil) {
    void (^fetchCompletionHandler)(UIBackgroundFetchResult) = fetchCompletionHandlers[handlerKey];
    if (fetchCompletionHandler != nil) {
        fetchCompletionHandlers[handlerKey] = nil;
        @try {
            fetchCompletionHandler(fetchResult);
        } 
        @catch (NSException * e) {
            NSLog(@"Exception fetchCompletionHandler: %@", e);
        };
    } else {
        void(^completionHandler)(void) = completionHandlers[handlerKey];
        if (completionHandler != nil) {
            completionHandlers[handlerKey] = nil;
            @try {
                completionHandler();
            }
            @catch (NSException * e) {
                NSLog(@"Exception completionHandler: %@", e);
            };

        }
    }
}}

使用此处描述的补丁包来处理rn firebase 5.X.X:https://github.com/invertase/react-native-firebase/issues/4610#issuecomment-755444001 或者更好的方法是升级rn firebase(推荐)。 - Fawaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接