使用React-Native监听Android上的传入链接 [英] Listening for incoming links on Android with React-Native

查看:90
本文介绍了使用React-Native监听Android上的传入链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用链接库使用react-native侦听和处理IOS上的传入链接: https://facebook.github.io/react-native/docs/linking.html ,但是它显示了为URL添加事件侦听器的功能是IOS平台特定的。还有其他方法可以监听到我在android上应用程序的传入链接并在Javascript端进行处理吗?

I am able to listen and handle incoming links on IOS with react-native using the linking library: https://facebook.github.io/react-native/docs/linking.html, but it shows the function for adding an event listener for a url is IOS platform specific. Are there any other ways of listening for incoming links to my app on android and handling it on the Javascript side?

推荐答案

我只是得到它的工作!您只需遵循这些说明

I just got it working! You just have to follow these instructions.

基本上,在现有的之一下添加< intent-filter> android / app / src / main / AndroidManifest.xml ,其中包含 VIEW 操作, DEFAULT BROWSABLE 类别,以及至少< data>

Basically, add an <intent-filter> under the existing one of your android/app/src/main/AndroidManifest.xml, containing the VIEW action, the DEFAULT and BROWSABLE categories, and at least a <data>.

然后只需重建并重新安装APK( react-native run-android )就可以了!现在,与您的< data> 标记匹配的链接将在您的应用中打开!

Then simply rebuild and reinstall your APK (react-native run-android), that's it! Links matching your <data> tags will now open in your app!

现在只需使用< 中的href = https://facebook.github.io/react-native/docs/linking.html#getinitialurl rel = nofollow noreferrer> Linking.getInitialURL()您的主要Javascript类的componentDidMount()

Now just catch this URL with Linking.getInitialURL() in the componentDidMount() of your main Javascript class!

清单示例:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="net.yourapp"
  android:versionCode="1"
  android:versionName="0.1">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

  <uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="22" />

  <application
    android:name=".MainApplication"
    android:allowBackup="true"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">

    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:screenOrientation="portrait"
      android:configChanges="keyboard|keyboardHidden|screenSize">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

      <!-- HERE: -->
      <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="yoursite.net" />
        <data android:scheme="https" android:host="yoursite.com" />
        <data android:scheme="https" android:host="yoursite" />
        <data android:scheme="customscheme" android:host="yourpath" />
      </intent-filter>

    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
  </application>

</manifest>

这篇关于使用React-Native监听Android上的传入链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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