Firebase 动态链接无法打开应用程序

问题描述 投票:0回答:6

我已经在我的设备上本地开发了一个 Android 应用程序(应用程序尚未在 Android Play 商店上发布)。我有以下逻辑来在 MainActivity 中获取深层链接。

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, null)
            .addApi(AppInvite.API)
            .build();

    // Check if this app was launched from a deep link. Setting autoLaunchDeepLink to true
    // would automatically launch the deep link if one is found.
    boolean autoLaunchDeepLink = false;
    AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
            .setResultCallback(
                    new ResultCallback<AppInviteInvitationResult>() {
                        @Override
                        public void onResult(@NonNull AppInviteInvitationResult result) {
                            if (result.getStatus().isSuccess()) {
                                // Extract deep link from Intent
                                Intent intent = result.getInvitationIntent();
                                String deepLink = AppInviteReferral.getDeepLink(intent);

                                Toast.makeText(getApplicationContext(), deepLink, Toast.LENGTH_LONG).show();
                                // Handle the deep link. For example, open the linked
                                // content, or apply promotional credit to the user's
                                // account.

                                // ...
                            } else {
                                Log.d(TAG, "getInvitation: no deep link found.");
                            }
                        }
                    });

我使用 Firebase 控制台构建了一些动态链接并在移动浏览器中打开。但它没有打开我的应用程序并到达行 String deepLink = AppInviteReferral.getDeepLink(intent);

它会在移动浏览器本身中打开 URL。

如何在使用 Firebase 动态链接时打开应用程序并处理 Activity 中的深层链接??

编辑:

我在清单文件中有意图过滤器。

<activity android:name="MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="example.com" android:scheme="http"/>
            <data android:host="example.com" android:scheme="https"/>
        </intent-filter>
    </activity>
android firebase firebase-dynamic-links
6个回答
13
投票

Action View 和 Action Main 都属于不同的 Intent 类别。因此,您需要将它们放在不同的块中,如下所示:

 <activity android:name=".DynamicLinkActivity">
        <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:host="example.com"
                android:scheme="http" />
            <data
                android:host="example.com"
                android:scheme="https" />
        </intent-filter>
    </activity>

9
投票

或者,您也可以在意图过滤器中提供数据,如“常规”深度链接文档中所述(https://developer.android.com/training/app-indexing/deep-linking.html

意图过滤器将如下所示:

        <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="https://XXYYZZ42.app.goo.gl/"  // <- Replace with your deep link from the console
                android:scheme="https"/>
        </intent-filter>

可以在您的控制台中获取该链接,就在这里:

编辑:添加图片以显示在哪里可以找到此链接


3
投票

2019 年 1 月更新

确保您已将应用的 SHA256 证书指纹添加到 Firebase 控制台中的项目中。

在您想要动态链接到午餐的活动下的 AndroidManifest.xml 中添加以下代码:

    <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:host="mydomainname.page.link" android:scheme="http"/>
       <data android:host="mydomainname.page.link" android:scheme="https"/>
   </intent-filter>
    如果您使用的是 Android 6.0(API 级别 23)及以下版本,请删除
  • android:autoVerify="true",否则应用程序将崩溃。

3
投票
正如另一个答案中所解释的,您的意图过滤器似乎有一些问题。另外您的网址可能有一些问题。当我玩这些的时候,我在没有注意到的情况下创建了 FireBase 网站的错误 URL。可以通过在应用程序中打开整个 url 来测试您的代码。我将所有要测试的网址写入电子邮件并发送给自己,在设备中打开并开始单击。之后,您可以在 FireBase 中创建您想要的 url。以下是一些示例(可能存在拼写错误和其他错误):

如果您在设备上单击此网址:

https://<myapp>.app.goo.gl/?link=https://mysite.fi/112972&apn=com.mydomain.myapp
并且在你的清单中有这个:

<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="mysite.fi" android:pathPattern=".*" /> </intent-filter>
它应该在您的应用程序 (com.mydomain.myapp) 中打开 

https://mysite.fi/112972,如果您在 PC 上打开该链接,它将在以下位置打开 https://mysite.fi/112972浏览器。

如果您在设备中打开此网址:

https://<myapp>.app.goo.gl/?link=https://mysite.fi/112972&apn=com.mydomain.myapp&al=myscheme://mydeeplink/112972&afl=http://fallback.fi
并且在你的清单中有这个:

<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="myscheme" android:host="mydeeplink" android:pathPattern=".*" /> </intent-filter>
它将在您的应用程序 (com.mydomain.myapp) 中打开 myscheme://mydeeplink/112972。您需要有处理它的代码。如果未安装该应用程序,它将在您的浏览器中打开 

http://fallback.fi。在 PC 上,它仍然会打开 https://mysite.fi/112972

(编辑 19.3.2018)Firebase 似乎不再完全支持 'al=' 。该代码有效,但文档和 Firebase 控制台生成的 url 中缺少该代码。


2
投票
我遇到了同样的问题,深层链接不起作用,以前的答案没有帮助。像这样分割“数据”标签的技巧是什么:

代替:

<data android:host="example.com" android:scheme="http"/> <data android:host="example.com" android:scheme="https"/>

这样做:

<data android:host="example.com"/> <data android:scheme="http"/> <data android:scheme="https"/>

希望对其他人有帮助:)


1
投票
我最近发现上面所有答案中缺少一件事。请确保

exported=true

参加
AndroidManifest.xml
中的活动。浪费了我2个小时的时间来调试同样的问题。该域名将是您网站的域名,在我的例子中,该网站是 
Upride - upride.in(不带 https://)

<activity android:name=".Home" android:configChanges="orientation" android:exported="true" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" tools:ignore="AppLinkUrlError"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- the host should contain your domain. The pattern should be same as it's mentioned without any special char --> <data android:host="example.com" android:scheme="https" /> <data android:host="example.com" android:scheme="http" /> </intent-filter> </activity>
    
© www.soinside.com 2019 - 2024. All rights reserved.