简体   繁体   English

无法获取 Android 应用程序链接以在 Flutter 编写的 Android 应用程序中工作

[英]Unable to get Android App Link to work in an Android app written in Flutter

I am trying to implement Moneris hosted pay page from a flutter mobile app but this question is relevant to any app that wants to implement Android App Links .我正在尝试从 flutter 移动应用程序实现 Moneris 托管的支付页面,但这个问题与任何想要实现 Android App Links 的应用程序有关 In order to redirect back from the hosted pay page, I need to implement Android App Link (haven't gotten to do iOS version yet).为了从托管支付页面重定向回来,我需要实现 Android 应用链接(还没有做 iOS 版本)。 Based on this article, in order for App Links to work, one needs:根据 这篇文章,为了使 App Links 工作,需要:

  1. Identify a redirect URL on the Moneris hosted pay page (or on the page where a redirect might take place, in case of a generic example);在 Moneris 托管的付费页面(或在一般示例的情况下,在可能发生重定向的页面上)识别重定向 URL;
  2. Modify AndroidManifest.xml to associate redirects with activities;修改 AndroidManifest.xml 将重定向与活动关联;
  3. Implement a way in Flutter to intercept App Links;在Flutter中实现一个拦截App Links的方法;
  4. Place the Digital Asset Links JSON file on the web site (same domain as redirect URL) in the.well-known folder.将 Digital Asset Links JSON 文件放在 web 站点(与重定向 URL 相同的域)的 .well-known 文件夹中。

For #1, I have identified 2 URLs, one for approved payments ( https://example.com/moneris_approved ) and one for declined payments ( https://example.com/moneris_declined ).对于 #1,我确定了 2 个 URL,一个用于已批准的付款( https://example.com/moneris_approved ),一个用于拒绝付款( https://example.com/moneris_declined )。 URLs have to be in https for the whole thing to work. URL 必须在 https 中才能使整个工作正常进行。 Moneris can reply with a POST with XML, POST with parms or GET with parms. Moneris 可以使用带有 XML 的 POST、带有 parms 的 POST 或带有 parms 的 GET 回复。 Tried all three.三个都试过了。

For #2, I have modified AndroidManifest.xml both by hand and with help of Android Studio.对于 #2,我手动修改了 AndroidManifest.xml,并在 Android Studio 的帮助下进行了修改。 Relevant pieces look like this:相关部分如下所示:

<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="<label>"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        ...              
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:autoVerify="true">
            <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="example.com"
                android:pathPattern="/moneris_approved" />
        </intent-filter>    
        <intent-filter android:autoVerify="true">
            <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="example.com"
                android:pathPattern="/moneris_declined" />
        </intent-filter>    
    </activity>
</application>

For #3, I am using uni_links package which takes care of channeling incoming App Links to my app.对于#3,我使用 uni_links package 负责将传入的应用链接引导到我的应用。

For #4, I have created and uploaded the assetlinks.json file and placed in the right folder.对于#4,我创建并上传了assetlinks.json 文件并放置在正确的文件夹中。 Included both debug and release SHA256 fingerprints.包括调试和发布 SHA256 指纹。 The file looks like this:该文件如下所示:

[
    {
        "relation": ["delegate_permission/common.handle_all_urls"],
        "target": {
            "namespace": "android_app",
            "package_name": "com.example.<name>",
            "sha256_cert_fingerprints":
            ["........"]
        }
    }
]

Now, after doing all that, when Moneris redirects to provided URLs, I am seeing a 404 page coming from my hosting site.现在,在完成所有这些之后,当 Moneris 重定向到提供的 URL 时,我看到来自我的托管站点的 404 页面。

To verify the setup, I have:为了验证设置,我有:

At this point, I don't know how else to test it or what the problem is.在这一点上,我不知道如何测试它或问题是什么。 I don't think the problem is with my app but rather soemthing is not closing the loop between Moneris redirect and it coming back to my app.我不认为问题出在我的应用程序上,而是某些东西没有关闭 Moneris 重定向和它返回到我的应用程序之间的循环。

Update 1更新 1

I devised a testing approach like this:我设计了一个这样的测试方法:

  1. Created a link-test page on my site with links mimicking redirects (ie same host/path as in the app's AndroidManifest.xml;在我的网站上创建了一个链接测试页面,其中包含模仿重定向的链接(即与应用程序的 AndroidManifest.xml 中的主机/路径相同;
  2. Created a simple Java Android App using Android Studio.使用 Android Studio 创建了一个简单的 Java Android 应用程序。 Using App Link Assistant I updated its AndroidManifest, created intents and reacted to those coming in. Also updated the assetlinks.json file on my website.使用 App Link Assistant,我更新了它的 AndroidManifest、创建意图并对进来的意图做出反应。还更新了我网站上的assetlinks.json 文件。 Tested it with the link-test page running on my emulator and everything worked as expected.使用在我的模拟器上运行的链接测试页面对其进行了测试,一切都按预期工作。 App opened up ok.应用打开正常。
  3. Repeated #2 but this time with a Flutter app using the uni_links package.重复 #2,但这次是使用 uni_links package 的 Flutter 应用程序。 Went throught he same steps of creating slightly different links on my link-test page, updating AndroidManifest and assetlinks.json file.完成了在我的链接测试页面上创建略有不同的链接的相同步骤,更新了 AndroidManifest 和assetlinks.json 文件。 Tested it with the link-test page running on my emulator and everything worked as expected.使用在我的模拟器上运行的链接测试页面对其进行了测试,一切都按预期工作。 App opened up ok.应用打开正常。
  4. Run the app I am working on on the emulator and put the links I am expecting to get from Moneris on my link-test page and tsted on the emulator.在模拟器上运行我正在开发的应用程序,并将我希望从 Moneris 获得的链接放在我的链接测试页面上,并在模拟器上进行测试。 Everything worked as expected.一切都按预期工作。 App opened up ok.应用打开正常。

So now my question is: what is the difference between me clicking on a link on my page (HTTP GET) vs. Moneris redirecting to the same link using HTTP GET?所以现在我的问题是:我点击页面上的链接(HTTP GET)与 Moneris 使用 HTTP GET 重定向到同一链接有什么区别? Me clicking on the link works.我点击链接有效。 Moneris redirect does not. Moneris 重定向没有。

This soution is not working for this case♀️此解决方案不适用于这种情况♀️

thanks for clarification.感谢您的澄清。 Unfortunately, Moneris only supports http and https schemas so using com.googleusercontent.apps as schema will not work for me.不幸的是,Moneris 仅支持 http 和 https 架构,因此使用 com.googleusercontent.apps 作为架构对我不起作用。 And while I get that it works for you, your implementation contradicts my understanding of how Android App Links should work虽然我知道它对你有用,但你的实现与我对 Android App Links 应该如何工作的理解相矛盾

01.The problem 01.问题

Failed to lanch the app after Redirect重定向后无法启动应用

02.The analysis of the problem 02.问题分析

=> (1) App (lanch browser) => (1) 应用程序(浏览器)

==> (2) browser ( Moneris.com) ==> (2) 浏览器 (Moneris.com)

===> (3) browser (redirct to example.com) ===> (3) 浏览器(重定向到 example.com)

=====> (4) browser (404 page)❌ =====>(4)浏览器(404页面)❌

======> (5) failed because the app is not launched❌ ======> (5) 应用未启动失败❌

03.Why Step 4 failed 03.为什么第四步失败

  • because you used a real domain schema因为您使用了真实的域模式

  • the browser used the DNS not ask the OS to lanch the app浏览器使用 DNS 不要求操作系统启动应用程序

04.Solution 04.解决方案

  • Use com.googleusercontent.apps Url使用 com.googleusercontent.apps Url

05.Example returnUrl solution 05.示例returnUrl解决方案

String returnUrl =
    "com.googleusercontent.apps.932931520452-buv2dnhgo7jjjjv5fckqltn367psbrlb:/approved)";
String cancelUrl =
    "com.googleusercontent.apps.932931520452-buv2dnhgo7jjjjv5fckqltn367psbrlb:/declined)";

06.Example manifest 06.示例清单

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.e042_flutter_paypal_f">
        <application android:name="io.flutter.app.FlutterApplication" android:label="e042_flutter_paypal_f" android:icon="@mipmap/ic_launcher">
            <activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">

                <meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true" />
                <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" />
                    <!-- Custom Path data -->
                    <data android:path="/auth" android:scheme="com.googleusercontent.apps.932931520452-buv2dnhgo7jjjjv5fckqltn367psbrlb"/>
                </intent-filter>
            </activity>
        </application>
    </manifest>

⚠ Warning ⚠ If there is a Client Secret stored in the App (i didn't use Moneris, but i asume there is a Client Secret ), and the app is not inted for the internal use of the company, it's very very bad ⚠ 警告 ⚠ 如果 App 中存储了Client Secret (我没有使用 Moneris,但我认为有Client Secret ),并且该应用程序不是供公司内部使用的,这是非常非常糟糕的


Ref.参考。

I can see that you want to handle deep link right in the flutter code.我可以看到您想在 flutter 代码中处理深层链接。 That is possible but you have to use one of the libraries for that - for example this .这是可能的,但您必须为此使用其中一个库 - 例如this

If you want to do it by yourself you have to create a platform specific Method Channels for that.如果你想自己做,你必须为此创建一个特定于平台的方法通道 This answer shows how to do it.这个答案显示了如何做到这一点。

Also you can find a more detailed explanation here你也可以在这里找到更详细的解释

Hope it helps.希望能帮助到你。

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

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