Chrome 自定义标签重定向到 Android 应用程序将关闭该应用程序 [英] Chrome Custom Tabs redirect to Android app will close the app

查看:24
本文介绍了Chrome 自定义标签重定向到 Android 应用程序将关闭该应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Android Chrome 自定义选项卡实现 OAuth2 流程,但是当 Chrome 自定义选项卡收到包含我的应用程序位置/方案的 302 时,我的应用程序始终关闭(没有崩溃).

I am trying to implement an OAuth2 flow with an Android Chrome Custom Tab but my app is always closed (no crash) when the Chrome Custom Tab is receiving the 302 with the location/scheme of my app.

如果我创建一个带有 ahref 链接的 HTML 页面并手动触摸它,Chrome 自定义选项卡会正确切换到我的应用程序.

If I create a HTML page with ahref link and touch manually on it the Chrome Custom Tab is correctly switching to my app.

似乎在 Chrome 自定义选项卡中处理服务器 302 重定向时,它无法正确处理我的自定义应用程序方案......但为什么呢?

Seems like when handling the server 302 redirect in the Chrome Custom Tab it will not correctly handle my custom app scheme... but why?

如果我在常用浏览器或 WebView 中尝试使用相同的重定向 URL,则一切正常.

If I try the same redirect URL in a stock browser or with a WebView everything is working too.

这是我当前的设置:

MainActiviy.java

MainActiviy.java

    Button btnChromeCustomTab = (Button) findViewById(R.id.btnChromeCustomTab);
    btnChromeCustomTab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
            String packageName = CustomTabsHelper.getPackageNameToUse(MainActivity.this);
            customTabsIntent.intent.setPackage(packageName);
            Uri theLocationUri = Uri.parse(URL);
            customTabsIntent.launchUrl(MainActivity.this, theLocationUri);
        }
    });

AndroidManifest.xml

AndroidManifest.xml

    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:label="@string/filter_title">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="myappscheme" android:host="oauth" />
        </intent-filter>
    </activity>

这是应用收到的带有 HTTP 302 代码的重定向 URL:

This is the redirect URL that the app received with HTTP 302 code:

myappscheme://oauth?code=1234567&state=tokenCheck123

myappscheme://oauth?code=1234567&state=tokenCheck123

build.gradle

build.gradle

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "de.myapptest.webviewtest"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:23.2.1'
   compile 'com.android.support:design:23.2.1'
   compile 'com.android.support:customtabs:23.0.0+'
}

感谢您的帮助...

推荐答案

我还观察到我的 Android 应用程序在服务器端 302 重定向到自定义方案后意外后台,并观察到独立 Chrome 和手动触发的预期处理在客户端重定向.

I've also observed my Android app unexpectedly background after server-side 302 redirection to a custom scheme, and observed expected handling from stand-alone Chrome and manually triggered redirection in the client.

我能够通过调用 预热 功能,然后加载重定向的 url.

I was able to "fix" the issue by calling the warmup function before loading the url that redirects.

换句话说,这是有效的:

In other words, this works:

void launchTab(Context context, Uri uri){
    final CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
        @Override
        public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient client) {
            final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            final CustomTabsIntent intent = builder.build();
            client.warmup(0L); // This prevents backgrounding after redirection
            intent.launchUrl(context, uri);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {}
    };
    CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", connection);
}

这不起作用:

void launchTab(Context context, Uri uri){
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent intent = builder.build();
    intent.launchUrl(context, uri);
}

Chrome 自定义标签文档 将热身描述为最佳实践,但它似乎也有助于确保预期的行为.

The Chrome Custom Tab docs describe warming up as a best practice, but it also appears to help ensure expected behavior.

就环境而言,我正在使用 Chrome 51 的 Nexus 5X 进行测试.我在 Gradle 中的 chrome 选项卡依赖项如下所示:

In terms of env, I'm testing on a Nexus 5X w Chrome 51. My chrome tab dependency in Gradle looks like this:

dependencies {
    compile 'com.android.support:customtabs:24.0.0'

这篇关于Chrome 自定义标签重定向到 Android 应用程序将关闭该应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆