【问题标题】:Firebase Dynamic Link open specific ActivityFirebase 动态链接打开特定 Activity
【发布时间】:2018-05-27 09:47:25
【问题描述】:

我在我的 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 上按预期打开。

我错过了什么吗?

谢谢

【问题讨论】:

  • 从android:host属性中删除最后一个/
  • @SimonMarquis 好的,我已经删除了它,但没有任何变化。我的应用程序仍在由我的启动器活动打开,而不是 RouteListActivity
  • 您不需要 myapp.app.goo.gl 条目 - 您可以尝试删除它吗? (并检查 mywebsite 条目上的任何其他尾随数据。
  • @IanBarber 好的,我删除了它,但没有成功。尾随数据是什么意思?谢谢
  • @JorgeAmVF :不,我必须通过创建一个调用所需活动的意图来解决这个问题。这是通过 FirebaseDynamicLinks OnSuccessListener 接口在我的启动器活动中完成的。

标签: android firebase deep-linking firebase-dynamic-links


【解决方案1】:

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

我想起了我开发的一个应用程序,其中动态链接在以前的版本中运行良好,我只是再次测试它们以确认(here is a sample dynamic link to demonstrate it really works);所以我去了它的清单,想知道我当时做了什么。

基本上,向 Intent 过滤器添加 android:autoVerify=true 很重要,如下所示:

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

它会请求验证应用链接,您可以阅读here 以便更好地理解它。

由于此解决方案仅适用于 API 23,因此我建议将 tools:targetApi="23" 也添加为“this tells the tools that you believe this element (and any children) will be used only on the specified API level or higher”,以避免出现不需要的 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"。

【讨论】:

  • 干得好豪尔赫,行得通!但是我猜因为我们的目标 API >=23,这不适用于以前的 API,是吗?
  • 糟糕,我没有覆盖我之前的所有代码。不幸的是,在使用 API 25 时,这段代码在我的应用程序中不起作用
  • 是的,我猜这是 Lint 所说的 API 23 之后提供的功能。这真的很奇怪,因为它在测试 2/3 应用程序时很容易工作,但仍然会出错。我不确定,但我认为它与主页有关,但我仍然需要测试。我正在使用替代页面来运行主要活动并添加另一个活动来加载主页,我认为主页可能与应用程序本身混淆。我会尽快进行更多测试,如果找到解决方案,我会尝试编辑答案。在我的一个应用程序中,我必须先清除“设置为默认值”才能工作。
  • 我已经对使用我在答案中提到的动态链接的同一个应用程序进行了快速测试,并且主页的动态链接忽略了特定的意图过滤器,并且只运行默认的应用程序主要活动 Here您有两个链接可以查看:e5kmd.app.goo.gl/zvpW 和 e5kmd.app.goo.gl/Zy4N;第一个用于页面并且运行良好并提供选项,但第二个被定向到清晰的主页只是像往常一样运行应用程序。如果这也是您的情况,我建议创建一个与您的主页类似的页面,但要使用 slug 并避免复制。
  • @JorgeAmVF 我遵循了确切的步骤,但它对我不起作用。我仍然面临这个问题。该应用程序始终在启动页面上启动,而不是在单击动态链接时的预期动态链接页面上启动。您是否对代码进行了任何改进?
【解决方案2】:

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

第一

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

<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 该活动的方法,您将处理接收到的数据。

  • 像这样在 Activity 中创建动态链接实例 -

代码在 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) 是在我的网站上打开特定页面的链接(比如一篇文章)。 然后,我将链接分成几部分并将它们用作信息 (意图中的额外内容)发送到我的活动并加载 所需的数据。

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

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

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多