异常“必须在主线程上进行调用”iOS

use*_*430 7 ios react-native firebase-cloud-messaging react-native-ios

我正在开发一个通过 Firebase Cloud Messaging 获取推送通知的 React Native 项目。已经实现了 onNotification()、onNotificationOpened() 和 getInitialNotification() 的方法。虽然当应用程序在前台并强制关闭时通知正常工作,但当应用程序在后台(不是强制关闭)时打开通知时出现错误。错误图像如下。我尝试更改/注释通知代码,但错误仍然存​​在。我对 React Native 很陌生,不知道为什么会发生这个特殊问题。这方面的任何帮助都会非常有帮助。我已经更新了与 Firebase 和云消息传递相关的所有 pod。下面也是 react-native 代码。我没有在 Xcode 中做过任何编码,所以 Pod 文件是原样的。[在此处输入图片说明][1]

![1]: https://i.stack.imgur.com/Bg7Us.png

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 17

在 ./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);
            };

        }
    }
}}
Run Code Online (Sandbox Code Playgroud)