使用 Android SDK 为 Facebook 登录启用 Chrome 自定义标签
Enabling Chrome Custom Tabs for Facebook login using Android SDK
我在我的应用程序中使用 Facebook SDK 版本 4.11.0。
按照 Official docs page 中概述的步骤,我在清单文件中添加了以下内容以启用 Chrome 自定义选项卡。
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<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="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
fb_login_protocol_scheme
添加到 strings.xml
,值为 'fb+app_id'
身份验证过程工作正常,没有任何问题。
唯一担心的是,当我点击登录按钮时,登录对话框不会在 Chrome 自定义选项卡中打开,而是以通常的 webview 对话框格式打开。
这里是否缺少要添加到项目中以启用 Chrome 自定义选项卡的内容?
您可以查看为什么 Chrome 自定义标签不适用于此代码段:
private static final String CUSTOM_TABS_SERVICE_ACTION = "android.support.customtabs.action.CustomTabsService";
private static final String CHROME_PACKAGE = "com.android.chrome";
private boolean isCustomTabsAllowed(Context context) {
boolean isCustomTabsAllowed = true;
if (!isCustomTabsEnabled(context)) {
Log.d(TAG, "isCustomTabsEnabled false");
isCustomTabsAllowed = false;
}
if (!isChromeCustomTabsSupported(context)) {
Log.d(TAG, "isChromeCustomTabsSupported false");
isCustomTabsAllowed = false;
}
if (!Validate.hasCustomTabRedirectActivity(context)) {
Log.d(TAG, "hasCustomTabRedirectActivity false");
isCustomTabsAllowed = false;
}
return isCustomTabsAllowed;
}
private boolean isCustomTabsEnabled(Context context) {
final String appId = Utility.getMetadataApplicationId(context);
final Utility.FetchedAppSettings settings = Utility.getAppSettingsWithoutQuery(appId);
return settings != null && settings.getCustomTabsEnabled();
}
private boolean isChromeCustomTabsSupported(final Context context) {
Intent serviceIntent = new Intent(CUSTOM_TABS_SERVICE_ACTION);
serviceIntent.setPackage(CHROME_PACKAGE);
List<ResolveInfo> resolveInfos =
context.getPackageManager().queryIntentServices(serviceIntent, 0);
return !(resolveInfos == null || resolveInfos.isEmpty());
}
这些是 Facebook SDK 在启动 Chrome 自定义标签之前调用的方法,因此只需调用 isCustomTabsAllowed
您就可以了解您的应用程序出了什么问题。
如果 isCustomTabsEnabled
为假,则说明您的应用程序配置存在问题。在 Facebook 控制台的 App Review 部分下验证该应用程序已上线并且可供 public.
如果 isChromeCustomTabsSupported
为假,可能是您使用的 Chrome 旧版本不支持 Chrome 自定义选项卡,请尝试更新到 [=30] 的最新版本=].
如果 hasCustomTabRedirectActivity
为假,表示集成中存在一些问题,请验证您是否正确执行了 guide 中指示的所有步骤。还要验证 APP_ID 与字符串 facebook_app_id
中使用的相同,否则登录对话框将不会使用 Chrome 自定义选项卡。
我在我的应用程序中使用 Facebook SDK 版本 4.11.0。
按照 Official docs page 中概述的步骤,我在清单文件中添加了以下内容以启用 Chrome 自定义选项卡。
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<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="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
fb_login_protocol_scheme
添加到 strings.xml
,值为 'fb+app_id'
身份验证过程工作正常,没有任何问题。
唯一担心的是,当我点击登录按钮时,登录对话框不会在 Chrome 自定义选项卡中打开,而是以通常的 webview 对话框格式打开。
这里是否缺少要添加到项目中以启用 Chrome 自定义选项卡的内容?
您可以查看为什么 Chrome 自定义标签不适用于此代码段:
private static final String CUSTOM_TABS_SERVICE_ACTION = "android.support.customtabs.action.CustomTabsService";
private static final String CHROME_PACKAGE = "com.android.chrome";
private boolean isCustomTabsAllowed(Context context) {
boolean isCustomTabsAllowed = true;
if (!isCustomTabsEnabled(context)) {
Log.d(TAG, "isCustomTabsEnabled false");
isCustomTabsAllowed = false;
}
if (!isChromeCustomTabsSupported(context)) {
Log.d(TAG, "isChromeCustomTabsSupported false");
isCustomTabsAllowed = false;
}
if (!Validate.hasCustomTabRedirectActivity(context)) {
Log.d(TAG, "hasCustomTabRedirectActivity false");
isCustomTabsAllowed = false;
}
return isCustomTabsAllowed;
}
private boolean isCustomTabsEnabled(Context context) {
final String appId = Utility.getMetadataApplicationId(context);
final Utility.FetchedAppSettings settings = Utility.getAppSettingsWithoutQuery(appId);
return settings != null && settings.getCustomTabsEnabled();
}
private boolean isChromeCustomTabsSupported(final Context context) {
Intent serviceIntent = new Intent(CUSTOM_TABS_SERVICE_ACTION);
serviceIntent.setPackage(CHROME_PACKAGE);
List<ResolveInfo> resolveInfos =
context.getPackageManager().queryIntentServices(serviceIntent, 0);
return !(resolveInfos == null || resolveInfos.isEmpty());
}
这些是 Facebook SDK 在启动 Chrome 自定义标签之前调用的方法,因此只需调用 isCustomTabsAllowed
您就可以了解您的应用程序出了什么问题。
如果 isCustomTabsEnabled
为假,则说明您的应用程序配置存在问题。在 Facebook 控制台的 App Review 部分下验证该应用程序已上线并且可供 public.
如果 isChromeCustomTabsSupported
为假,可能是您使用的 Chrome 旧版本不支持 Chrome 自定义选项卡,请尝试更新到 [=30] 的最新版本=].
如果 hasCustomTabRedirectActivity
为假,表示集成中存在一些问题,请验证您是否正确执行了 guide 中指示的所有步骤。还要验证 APP_ID 与字符串 facebook_app_id
中使用的相同,否则登录对话框将不会使用 Chrome 自定义选项卡。