我已经在我的设备上本地开发了一个 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>
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>
或者,您也可以在意图过滤器中提供数据,如“常规”深度链接文档中所述(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>
编辑:添加图片以显示在哪里可以找到此链接
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>
如果您在设备上单击此网址:
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 中缺少该代码。
代替:
<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"/>
希望对其他人有帮助:)
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>