1

I have set up an android app link for my react native application...But whenever I tap the link and choose my application to open it, system does try to open my app but my app crashes...I have set up the android linking properly and the activity which I want to start when the link is tapped does open..But I am not able to set up the RCTDeviceEventEmitter in the activity...I was confused about how to provide react context here..So I created a custom class in ReactContextFetcher.java like this:

public class ReactContextFetcher extends ReactContextBaseJavaModule {
    @Override
    public String getName() {
        return "ReactContextFetcher";
    }

    public ReactContext fetchReactContext() {
        return getReactApplicationContext();
    }

}

And I am using this context in the activity started by android app link like this:

public class NewFriendActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(null);
        ReactContext rContext = new ReactContextFetcher().fetchReactContext();
        Intent intent = getIntent();
        String data = intent.getData().toString();
        String user = data.split("/add-friend/")[1];
        Log.d("obscure_tag", data.toString());
        sendEvent(rContext, "NewFriend", user);

    }

    private void sendEvent(ReactContext reactContext,
                           String eventName, String user) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, user);
    }
}

This is how I am receiving this event in javascript:

import {NativeEventEmitter, NativeModules} from 'react-native';

class NewFriendReceiver extends React.Component {
  componentDidMount() {
    const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
    this.eventListener = eventEmitter.addListener('NewFriend', event => {
      console.log(event.eventProperty); // "someValue"
    });
  }
  componentWillUnmount() {
    this.eventListener.remove(); //Removes the listener
  }
}

What am I doing wrong? Why does my app crash when I tap on the android link? Does it have anything to do with how I use react context?

Pro
  • 445
  • 4
  • 14

0 Answers0