简体   繁体   English

深度链接在 Android 上的 React Native 应用程序中不起作用

[英]Deep Linking not working in React Native app on Android

I have setup my React Native app to use Deep Linking with expo-linking, but for some reason it just does not work on Android (yet to implement on iOS).我已经将我的 React Native 应用程序设置为使用带有 expo-linking 的 Deep Linking,但由于某种原因,它在 Android 上不起作用(尚未在 iOS 上实现)。 Opening the link just opens it in the web browser, and doesn't open the app as it should.打开链接只是在 web 浏览器中打开它,并没有按应有的方式打开应用程序。 Any idea why?知道为什么吗?

app.json app.json

"android": {
  "adaptiveIcon": {
    "foregroundImage": "./assets/adaptive-icon.png",
    "backgroundColor": "#FFFFFF"
  },
  "package": "com.example.myapp",
  "intentFilters": [
    {
      "action": "VIEW",
      "data": [
        {
          "scheme": "https",
          "host": "testlink.com",
        }
      ],
      "category": [
        "BROWSABLE",
        "DEFAULT"
      ]
    }
  ]
},

This did not update the AndroidManifest, so I manually edited it:这并没有更新 AndroidManifest,所以我手动编辑了它:

AndroidManifest.xml AndroidManifest.xml

<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="https" android:host="testlink.com"/>
</intent-filter>

App.js应用程序.js

const linking = {
    prefixes: ["https://testlink.com"],    
};

useEffect(() => {
    Linking.addEventListener("url", handleDeepLink);

    return () => {
        Linking.removeEventListener("url", handleDeepLink);
    };
}, []);

return (
    <NavigationContainer /*linking={linking}*/>
        ....
    </NavigationContainer>
);

This worked fine with just the normal expo links, but is not working now I want a custom URL so that it opens in web browser on a computer, or on the app if it is installed.这仅适用于普通的 expo 链接,但现在不起作用我想要一个自定义的 URL 以便它在 web 浏览器中打开,如果它安装在计算机上或应用程序上。

At the core, the iOS Safari browser has built-in deep linking which makes it possible to deep-link to custom app schema- myapp:///link-to-resources .在核心,iOS Safari 浏览器具有内置的深度链接,可以深度链接到自定义应用程序架构-myapp myapp:///link-to-resources Chromium-based browsers used mostly on android don't support custom app schema in URL input field .主要在 android 上使用的基于 Chromium 的浏览器不支持URL 输入字段中的自定义应用程序架构。

The work-around is a setup simple web page that can redirect your app custom schema using Browser DOM Window API.解决方法是设置简单的web 页面,该页面可以使用浏览器 DOM Window API 重定向您的应用程序自定义架构。

 const launchApp = (deepLink = "", fallBack = "") => {
  var now = new Date().valueOf();
  setTimeout(function () {
    if (new Date().valueOf() - now > 100) return;
    window.location = fallBack;
  }, 25);
  window.location = deepLink;
}


 const deepLink = "myapp:///path_to_ressource/";
 const fallbackLink = "http://play.google.com/store/apps/details?id=com.yourcompany.appname"

launchApp(deepLink,fallbackLink)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM