Listening for incoming links on Android with React-Native

≡放荡痞女 提交于 2019-12-21 02:46:14

问题


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?


回答1:


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

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>.

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!

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

Example of manifest:

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


来源:https://stackoverflow.com/questions/37653723/listening-for-incoming-links-on-android-with-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!