【问题标题】:Deep linking react native app on Android深度链接在 Android 上反应原生应用
【发布时间】:2017-08-28 20:47:38
【问题描述】:

我正在尝试让一个 url 打开应用程序并将一些带有此 url 的数据传递到应用程序中,但它不起作用。

我在 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>

并且我在app的入口类中添加了:

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

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

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

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

【问题讨论】:

  • 你查看了linking 上的 React Native 文档吗?
  • @alexdriedger 是的,我编辑了我的问题。

标签: android react-native deep-linking


【解决方案1】:
  • 如果在浏览器的地址栏中输入链接,应用程序将无法打开。它必须是一个网页 &lt;a/&gt; 标签,例如:

    &lt;a href="http://example.com"&gt;&lt;/a&gt;

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

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

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

例如,在您的情况下,它应该类似于(将 YOURAPPNAME 更改为您的应用名称):

adb shell am start -W -a android.intent.action.VIEW -d "convid://conv/" com.YOURAPPNAME
  • 如果您使用的是 windows,并且如果它说 adb 命令未定义,您需要从 SDK 文件夹中的 platform-toolsfolder 运行该命令,例如:

    Android/Sdk/platform-tools/

【讨论】:

  • 我将链接放在 jsfiddle.net 并从模拟器中打开它,它可以工作。为什么他们在 react native 文档中没有提到这一点...
猜你喜欢
  • 2021-12-16
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
相关资源
最近更新 更多