React-native: Linking.getInitialURL() url value is null

Created on 18 Sep 2017  路  10Comments  路  Source: facebook/react-native

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

  1. react-native -v: 2.0.1
  2. node -v: 8.4.0
  3. npm -v: 4.6.1

Then, specify:

  • Target Platform: Android

  • Development Operating System: MacOs Sierra

  • Build tools: Application created using create-react-native-app and then detached with support of Expo SDK

Steps to Reproduce

(Write your steps here:)

  1. Create application using create-react-native-app
  2. Detach application with: npm run eject (keep support of Expo SDK)
  3. Update AndroidManifest.xml with respective deep links filters inside MainActivity
<intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https"
              android:host="www.mydomain.com"
              android:pathPrefix="/" />
        <data android:scheme="https"
              android:host="www.mydomain.com" />
</intent-filter>
  1. Update activity declaration with android:launchMode="singleTask"
  2. Update application component to load url on componentDidMount:
componentDidMount() {
    Linking.getInitialURL().then((url) => {
      console.log('Inside of the function is: ' + url);
      this._handleOpenURL(url);
    }).catch(err => console.error('An error occurred', err));
}
  1. Find url is always null

Expected Behavior

url variable populated with respective Url application was loaded with

Actual Behavior

Url is null

Stale

Most helpful comment

Same issue

All 10 comments

Experiencing this too with expo. Linking.getInitialURL works for apps created with react-native init though.

Duplicate of #15961 ?

@Minishlink , not really (similar yes, but not a duplicate). My issue has nothing similar to closing application with back button. For me it just never works at all. Regardless of the way app was killed or never started since installation...

I'm experiencing the same thing since updating from RN 0.46.4 to 0.48.4, it used to work fine before that.

Note: I'm using wix/react-native-navigation

Same issue

Solved it! I'll explain what the problem for us was, if anyone is facing the same issue as we did.

We had a custom activity called "SplashActivity" where we had added the

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    ...

In our SplashActivity.java we started the MainActivity like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

For RN to pick up the url from getInitialURL(), we need to pass the data and action to our MainActivity. So basically we just passed the required information to our MainActivity (in SplashActivity.java):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Uri data = null;
    String action = null;
    Intent currentIntent = getIntent();
    if (currentIntent != null) {
        Uri intentData = currentIntent.getData();
        if (intentData != null) {
            data = intentData;
        }

        // Get action as well.
        action = currentIntent.getAction();
    }

    Intent intent = new Intent(this, MainActivity.class);
    // Pass data and action (if available).
    if (data != null) {
        intent.setData(data);
    }
    if (action != null) {
        intent.setAction(action);
    }
    startActivity(intent);
    finish();
}

(don't forget to import android.content.Intent; and import android.net.Uri;)

This solved it for us.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

I am getting null value too but on iOS.

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

Was this page helpful?
0 / 5 - 0 ratings