简体   繁体   English

Firebase 动态链接打开特定活动

[英]Firebase Dynamic Link open specific Activity

I implemented Firebase Dynamic links (which are great.) on my iOS app and am now doing the same job on Android, I managed to launch my Android app by clicking the dynamic URL. but I can't open it on another activity than my launcher activity.我在我的 iOS 应用程序上实现了 Firebase 动态链接(很棒。),现在我正在 Android 上做同样的工作,我设法通过单击动态 URL 启动我的 Android 应用程序。但我无法在除我的活动之外的其他活动中打开它启动器活动。

Here is my manifest.xml file:这是我的 manifest.xml 文件:

    <activity android:name=".Activity.SplashActivity"
        android:theme="@style/SplashTheme"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Activity.RouteListActivity"
        android:screenOrientation="portrait">
        <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:host="mywebsite.com" android:scheme="http"/>
            <data android:host="mywebsite.com" android:scheme="https"/>
            <data
                android:host="myapp.app.goo.gl/"
                android:scheme="https" />
        </intent-filter>
    </activity>

When I click the URL, browser opens and redirects to my app but it opens on SplashActivity and not on RouteListActivity as expected.当我单击 URL 时,浏览器会打开并重定向到我的应用程序,但它会按预期在 SplashActivity 而不是 RouteListActivity 上打开。

Do I miss something?我想念什么吗?

Thank you谢谢

Well, I guess I found a simple workaround that's able to overcome this problem. 好吧,我想我找到了一个能够解决这个问题的简单解决方法。

I reminded of an app I developed in which the dynamic links work well in previous versions and I just tested them again to confirm ( here is a sample dynamic link to demonstrate it really works ); 我提醒我开发的一个应用程序,其中动态链接在以前的版本中运行良好,我只是再次测试它们以确认( 这是一个示例动态链接,以证明它确实有效 ); so I went to its manifest to know what I did back then. 所以我去了它的清单,知道我当时做了什么。

Basically, it's important to add an android:autoVerify=true to the intent filter like this: 基本上,将intent android:autoVerify=true到intent过滤器是很重要的,如下所示:

        <intent-filter
            android:autoVerify="true">

It'll make a request to verify app links as you can read here in order to understand it better. 它会向您提供验证应用程序链接的请求,因为您可以阅读此处以便更好地理解它。

As this solution only works from API 23 on, I suggest to add tools:targetApi="23" too as " this tells the tools that you believe this element (and any children) will be used only on the specified API level or higher " and to avoid an unwanted Lint highlight then. 由于此解决方案仅适用于API 23,我建议添加tools:targetApi="23"因为“ 这告诉工具您认为此元素(以及任何子级)将仅在指定的API级别或更高级别上使用 ”并避免不必要的Lint突出显示。

So, the best way to deal with this is by adding the following code: 因此,处理此问题的最佳方法是添加以下代码:

        <intent-filter
            android:autoVerify="true"
            tools:targetApi="23">

It's important to mention that "set as default" options from the user end might overlook this option depending on the way users use the link because it may appear a popup offering choices about how to handle the link and it can be somehow misleading (the two options look the same considering my app and device at least). 重要的是要提到用户端的“设置为默认”选项可能会忽略此选项,具体取决于用户使用链接的方式,因为它可能会显示一个弹出窗口,提供有关如何处理链接的选择,它可能会以某种方式误导(两者)考虑到我的应用程序和设备,选项看起来一样)。

In the sample manifest file present in this question, it would look like this: 在此问题中出现的示例清单文件中,它看起来像这样:

<activity android:name=".Activity.SplashActivity"
    android:theme="@style/SplashTheme"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".Activity.RouteListActivity"
    android:screenOrientation="portrait">
        <intent-filter
            android:autoVerify="true"
            tools:targetApi="23">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="mywebsite.com" android:scheme="http"/>
        <data android:host="mywebsite.com" android:scheme="https"/>
        <data
            android:host="myapp.app.goo.gl/"
            android:scheme="https" />
    </intent-filter>
</activity>

And don't forget to add xmlns:tools="http://schemas.android.com/tools" in the beginning of the manifest. 并且不要忘记在清单的开头添加xmlns:tools="http://schemas.android.com/tools"

Following are the steps I followed to handle deep links in my application. 以下是我在应用程序中处理深层链接时所遵循的步骤。

First 第一

Add this to the activity (in manifest file) which will handle the Deep links for your app. 将其添加到活动(在清单文件中),该活动将处理您应用的深层链接。 For me it is my home screen activity 对我来说这是我的主屏幕活动

<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:host="yourDomain"
                android:scheme="http" />
            <data
                android:host="yourDomain"
                android:scheme="https" />
</intent-filter>

Next 下一个

Go to the activity to which you added the above code. 转到您添加上述代码的活动。 In the onCreate method of that activity you'll handle the received data. 在该活动的onCreate方法中,您将处理接收的数据。

  • Create a dynamic link instance in the activity like this - 在活动中创建一个动态链接实例,如下所示 -

The code is in Kotlin 代码在Kotlin中

    FirebaseDynamicLinks.getInstance().getDynamicLink(intent).addOnSuccessListener{pendingDynamicLinkData ->

                    var deepLink: Uri? = null
                    if (pendingDynamicLinkData != null) {
                        deepLink = pendingDynamicLinkData.link
                    }

                    val path = deepLink?.path
                    val query = deepLink?.encodedQuery

                    //You can log the path here
                    //Generally the path consists of the link extracted 
                    //from the deep link

                    val parts = path?.split("/")

                    if (parts?.get(2) == "something") {
                        val intent = Intent(this, YouActivity::class.java)
                        intent.putExtra("extra1", parts[3])
                                .putExtra("extra2", parts[1])
                        startActivity(intent)
                    } 
                }.addOnFailureListener { e -> //You can log the error here }

What have I done here? 我在这做了什么?

First I created the instance of FirebaseDynamicLinks. 首先,我创建了FirebaseDynamicLinks的实例。 Next, I used the fetched dynamic link data to extract the link (deepLink?.path) which is the link that opens a specific page on my website (say an article). 接下来,我使用获取的动态链接数据来提取链接(deepLink?.path),这是在我的网站上打开特定页面的链接(比如一篇文章)。 Then, I split the link into pieces and used them as the information (the extras in the intent) to send to my activity and load the required data. 然后,我将链接拆分为多个部分,并将它们用作发送到我的活动并加载所需数据的信息(意图中的附加内容)。

You can do this for all kinds of links. 您可以为各种链接执行此操作。 Receive the data on a receiving activity and redirect from there to any activity depending upon the data received. 接收有关接收活动的数据,并根据收到的数据从那里重定向到任何活动。

I hope this helped to resolve your issue. 我希望这有助于解决您的问题。 Let me know if there is any confusion. 如果有任何混淆,请告诉我。

Thanks 谢谢

To avoid being redirected to your launch activity, you should add an intent-filter with the same host as configured in your dynamic link.为避免被重定向到您的启动活动,您应该添加一个 intent-filter,其主机与动态链接中配置的主机相同。

我在 Firebase 中的动态链接

For the image above, the intent-filter I configured is below.对于上图,我配置的 intent-filter 如下。 Note, this Activity is not my launcher.请注意,此活动不是我的启动器。 A point of attention is to add "www" and your domain name space (com.br) in the host field at xml code.一个注意点是在xml代码处的主机字段中添加“www”和您的域名空间(com.br)。

意图过滤器配置

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

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