Search code examples
androidreact-nativemobiledeep-linkingandroid-deep-link

Linking.getInitialURL() promise resolves always null only when app is not running


How I run deeplinks via adb:

adb shell am start -W -a android.intent.action.VIEW -d "https://my.site.com/my/path" com.my.app.package

In this AndroidManifest.xml:

<manifest ...>
  <queries>
    <intent>
      <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https"/>
    </intent>
  </queries>
   ...
  <application>
    <activity>
      <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data android:host="my.site.com"
            android:pathPrefix="/my/path"
            android:scheme="https" />
      </intent-filter>
    </activity>
 </application>
</manifest>

In MainActivity.java with App closed I get the deeplink correctly, but it seems to be not passed to the RN layer:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();

    System.out.println("[MainActivity][onCreate] action="+action);

    if(data != null) {
      System.out.println("[MainActivity][onCreate] data="+data.toString());
    }

    super.onCreate(savedInstanceState);
  }

React native version: 0.66

On the React Native code:

Linking.addEventListener('https', handler);
...
Linking.getInitialURL().then(url => {console.log(JSON.stringify(url));}

the handler related to event fires always when app is in background or opened and I get the url correctyl.

The Linking.getInitialURL() resolves always null and the event listener never fires when app is closed.

On iOS all works fine.

App state iOS Android
opened
background
closed

I need React Native handles the URL passed from the deep link when the App is closed on Android.

If more info are needed, please ask for them and I will update this question.


Solution

  • Add a fresh new Intent: for example my dynamic link is: https://myappname.link/k5uCqpw3R2Uh1E

    therefor the android:scheme would be https

       <activity
            android:name=".MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
            android:exported="true"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
            **<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" />
            </intent-filter>**
        </activity>