Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception 'Call must be made on main thread' iOS

I am working on a react native project that gets push notifications via Firebase Cloud Messaging. Have implemented methods for onNotification(), onNotificationOpened() and getInitialNotification(). While notifications are working correctly when app is in the foreground and forced closed, I am getting an error when opening the notification when app is in the background (not force closed). The error image is below. I have tried changing/ commenting the notification code, but the error is still present. I am quite new to React Native and not sure why is this particular issue happening. Any help in this regard would be really helpful.I have updated all pods related to firebase and cloud messaging. Below is also the react-native code. I have not done any coding in Xcode, so the Pod files is as is. [enter image description here][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);
    }
}
like image 883
user14532430 Avatar asked Oct 28 '20 01:10

user14532430


Video Answer


1 Answers

Try this try-catch in ./node_modules/react-native-firebase/ios/RNFirebase/notifications/RNFirebaseNotifications.m

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);
            };

        }
    }
}}
like image 157
Julio Cesar Avatar answered Oct 22 '22 21:10

Julio Cesar