RCTEventEmitter payload is null in React Na

411 Views Asked by At

I have the following that emits an event from my iOS app to React Native.

iOS

- (void)sendBrazeDeepLinkUrlToReactNative: (NSString *) payload {
  if (hasListeners) {
    [self sendEventWithName:@"incomingRNAppEvent" body:@{@"url":payload}];
  }
}

And then I listen for the event in my React Native App.

const AppNotificationEmitter = new NativeEventEmitter(AppNotification);
      const subscription = AppNotificationEmitter.addListener(
        "incomingRNAppEvent",
        (event: any) => {
          console.log("InAppMessage Recieved: ", JSON.stringify(event));

          console.log("AppNotification: ", event);
        }
      );

When the event occurs, I am getting the event triggered and everything works as expected EXCEPT...

The event object received in React Native contains an empty object???

e.g. the output from `console.log("AppNotification", event) is:

AppNotification: {"url": null}

and I was able to confirm that the URL was definitely set by debugging the iOS native module, and checking the value when the event is triggered. i.e. payload was a valid url.

1

There are 1 best solutions below

0
On

Ok, turns out my knowledge of Objective-C was the issue. The object coming into my sendBrazeDeepLinkUrlToReactNative was getting a NSURL * rather than NSString

- (void)sendBrazeDeepLinkUrlToReactNative: (NSString *) payload {
  if (hasListeners) {
    [self sendEventWithName:@"incomingRNAppEvent" body:@{@"url":payload}];
  }
}

This code works.

- (void)sendBrazeDeepLinkUrlToReactNative: (NSURL *) payload {
  if (hasListeners) {
    [self sendEventWithName:@"incomingRNAppEvent" body:@{@"url":payload.absoluteString}];
  }
}