深度链接在Android上响应本机应用 [英] Deep linking react native app on Android

查看:85
本文介绍了深度链接在Android上响应本机应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开应用程序的网址,并将与此网址相关的一些数据传递到应用程序中,但这是行不通的.

我在AndroidManifest.xml中的活动标签:

 <活动android:name =.MainActivity"android:label ="@ string/app_name"android:configChanges ="keyboard | keyboardHidden | orientation | screenSize"android:windowSoftInputMode ="adjustResize"android:launchMode ="singleTask"><-添加了<意图过滤器>< action android:name ="android.intent.action.MAIN"/>< category android:name ="android.intent.category.LAUNCHER"/></intent-filter><意图过滤器>< action android:name ="fcm.ACTION.HELLO"/>< category android:name ="android.intent.category.DEFAULT"/></intent-filter><意图过滤器>< action android:name ="android.intent.action.VIEW"/>< category android:name ="android.intent.category.DEFAULT"/>< category android:name ="android.intent.category.BROWSABLE"/><数据android:scheme ="conv"android:host ="convid"/><-格式为'conv://convid/的网址将打开该应用</intent-filter></activity> 

然后我将其添加到应用程序的入口类中:

  componentDidMount(){Linking.addEventListener('url',(e)=> {console.log("url",e);});Linking.getInitialURL().then((url)=> {如果(url){console.log('初始url为:'+ url);}})} 

但是,当我打开浏览器并转到 conv://convid 时,没有任何记录,并且该应用程序无法打开.

我当然在浏览器之前打开了我的应用程序.

解决方案

  • 如果将链接输入到浏览器的地址栏中,则该应用将无法打开.它必须是网页< a/> 标记,例如:

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

链接在某些页面内.( https://developer.chrome.com/multidevice/android/intents )

由于您可能没有网页可将标签放入其中,因此对于测试,可以使用adb命令.打开控制台并编写以下行:

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

例如,在您的情况下,应类似于(用您的应用程序名称更改YOURAPPNAME):

  adb shell开始-W -a android.intent.action.VIEW -d"convid://conv/" com.YOURAPPNAME 

  • 如果您使用的是Windows,并且如果说adb命令未定义,则需要从SDK文件夹内的platform-toolsfolder运行该命令,例如:

    Android/Sdk/platform-tools/

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/

这篇关于深度链接在Android上响应本机应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆