简体   繁体   中英

Deep linking react native app on Android

I'm trying to get make a url open the app and to have some data with this url passed into the app, but it doesn't work.

My activity tag in AndroidManifest.xml:

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize"
    android:launchMode="singleTask">                         <-- added this
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="fcm.ACTION.HELLO" />
        <category android:name="android.intent.category.DEFAULT" />
    </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="conv"
        android:host="convid"/> <-- so urls of the form 'conv://convid/ will open the app
     </intent-filter>
  </activity>

And I added to the entry class of the app:

  componentDidMount() {
    Linking.addEventListener('url', (e) => {
      console.log("url", e);
    });

    Linking.getInitialURL().then((url) => {
      if (url) {
        console.log('Initial url is: ' + url);
      }
    })
  }

But when I open the browser and go to conv://convid nothing is being logged and the app doesn't open.

Of course I opened my app before the browser.

  • The app will not open if the link is entered into the browser's address bar. It has to be a web page <a/> tag like:

    <a href="http://example.com"></a>

link inside some page. ( https://developer.chrome.com/multidevice/android/intents )

As you probably do not have a web page to put tag inside, for your tests you can use adb command. Open the console and write the following line:

adb shell am start -W -a android.intent.action.VIEW -d "YOURHOST://YOURSCEME/" com.YOURAPPNAME

for example in your case it should be something like (change YOURAPPNAME with the name of your app):

adb shell am start -W -a android.intent.action.VIEW -d "convid://conv/" com.YOURAPPNAME
  • if you are using windows and, and if it says adb command is undefined you need to run the command from platform-toolsfolder inside your SDK folder such as:

    Android/Sdk/platform-tools/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM