RN Deep Linking For Android

1. RN Deep Linking For Android

1.1. AndroidManifest.xml

添加 android:launchMode="singleTask"intent-filter:

<activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
  
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  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="http" android:host="www.galoiszhou.com" android:pathPrefix="/home" />
    
    
  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="galoiszhou" android:host="home" />
    
  intent-filter>
activity>

1.2. APP.js

<NavigationContainer
  onStateChange={(state) =>
    console.log('------------>>..>>..>>New state is', JSON.stringify(state))
  }
  linking={{
    prefixes: ['http://www.galoiszhou.com', 'galoiszhou://'],
    config: {
      screens: {
        HOME: 'home',
      },
    },
    async getInitialURL() {
      // Check if app was opened from a deep link
      const url = await Linking.getInitialURL();
      // 当 APP 没有启动, Deep Linking 触发此方法, 可以获取 url

      if (url != null) {
        return url;
      }

      // // Check if there is an initial firebase notification
      // const message = await messaging().getInitialNotification();

      // // Get the `url` property from the notification which corresponds to a screen
      // // This property needs to be set on the notification payload when sending it
      // return message?.notification.url;
    },
    subscribe(listener) {
      const onReceiveURL = ({url}) => {
        // 当 APP 启动后, Deep Linking 触发此方法, 可以获取 url
        listener(url);
      };

      // Listen to incoming links from deep linking
      Linking.addEventListener('url', onReceiveURL);

      // // Listen to firebase push notifications
      // const unsubscribeNotification = messaging().onNotificationOpenedApp(
      //   (message) => {
      //     const url = message.notification.url;

      //     if (url) {
      //       // Any custom logic to check whether the URL needs to be handled
      //       //...

      //       // Call the listener to let React Navigation handle the URL
      //       listener(url);
      //     }
      //   },
      // );

      return () => {
        // Clean up the event listeners
        Linking.removeEventListener('url', onReceiveURL);
        // unsubscribeNotification();
      };
    },
  }}>

1.3. 使用 uri-scheme 调用 Deep Linking

npx uri-scheme open http://www.galoiszhou.com/home --android
npx uri-scheme open galoiszhou://home --android

你可能感兴趣的:(React,react,native,深链接)