繁体   English   中英

Firebase 动态链接打开特定活动

[英]Firebase Dynamic Link open specific Activity

我在我的 iOS 应用程序上实现了 Firebase 动态链接(很棒。),现在我正在 Android 上做同样的工作,我设法通过单击动态 URL 启动我的 Android 应用程序。但我无法在除我的活动之外的其他活动中打开它启动器活动。

这是我的 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>

当我单击 URL 时,浏览器会打开并重定向到我的应用程序,但它会按预期在 SplashActivity 而不是 RouteListActivity 上打开。

我想念什么吗?

谢谢

好吧,我想我找到了一个能够解决这个问题的简单解决方法。

我提醒我开发的一个应用程序,其中动态链接在以前的版本中运行良好,我只是再次测试它们以确认( 这是一个示例动态链接,以证明它确实有效 ); 所以我去了它的清单,知道我当时做了什么。

基本上,将intent android:autoVerify=true到intent过滤器是很重要的,如下所示:

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

它会向您提供验证应用程序链接的请求,因为您可以阅读此处以便更好地理解它。

由于此解决方案仅适用于API 23,我建议添加tools:targetApi="23"因为“ 这告诉工具您认为此元素(以及任何子级)将仅在指定的API级别或更高级别上使用 ”并避免不必要的Lint突出显示。

因此,处理此问题的最佳方法是添加以下代码:

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

重要的是要提到用户端的“设置为默认”选项可能会忽略此选项,具体取决于用户使用链接的方式,因为它可能会显示一个弹出窗口,提供有关如何处理链接的选择,它可能会以某种方式误导(两者)考虑到我的应用程序和设备,选项看起来一样)。

在此问题中出现的示例清单文件中,它看起来像这样:

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

并且不要忘记在清单的开头添加xmlns:tools="http://schemas.android.com/tools"

以下是我在应用程序中处理深层链接时所遵循的步骤。

第一

将其添加到活动(在清单文件中),该活动将处理您应用的深层链接。 对我来说这是我的主屏幕活动

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

下一个

转到您添加上述代码的活动。 在该活动的onCreate方法中,您将处理接收的数据。

  • 在活动中创建一个动态链接实例,如下所示 -

代码在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 }

我在这做了什么?

首先,我创建了FirebaseDynamicLinks的实例。 接下来,我使用获取的动态链接数据来提取链接(deepLink?.path),这是在我的网站上打开特定页面的链接(比如一篇文章)。 然后,我将链接拆分为多个部分,并将它们用作发送到我的活动并加载所需数据的信息(意图中的附加内容)。

您可以为各种链接执行此操作。 接收有关接收活动的数据,并根据收到的数据从那里重定向到任何活动。

我希望这有助于解决您的问题。 如果有任何混淆,请告诉我。

谢谢

为避免被重定向到您的启动活动,您应该添加一个 intent-filter,其主机与动态链接中配置的主机相同。

我在 Firebase 中的动态链接

对于上图,我配置的 intent-filter 如下。 请注意,此活动不是我的启动器。 一个注意点是在xml代码处的主机字段中添加“www”和您的域名空间(com.br)。

意图过滤器配置

暂无
暂无

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

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