Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect back to Android app?

I need to open a Custom Chrome tab that brings the user to my website, and then redirects the user back to the app. Its similar to launching a page for OAuth.

Here is what the Android side looks like...

AndroidManifest.xml (adds intent filter to activity for listening to URI)

<activity android:name=".app.MyActivity"
        android:label="@string/title"
        android:theme="@style/AppTheme"
        android:launchMode="singleTask"
        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:scheme="mytest" />
        </intent-filter>

    </activity>

Activity.java (launches custom chrome tab)

private void goToSite() {
    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
    int toolbarColor = getResources().getColor(R.color.primary);
    intentBuilder.setToolbarColor(toolbarColor);vice_key(), shared().getAccessToken());
    intentBuilder.build().launchUrl(this, Uri.parse("https://example.com/some-page"));
}

On my server, the user completes a form and is eventually brought to a page that is supposed to redirect them back into the app...

server.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<script>
    var redirectToApp = function() {
        setTimeout(function appNotInstalled() {
            window.location.replace("http://example.com/app-not-installed");
        }, 100);
        window.location.replace("mytest://justanexample");
    };
    window.onload = redirectToApp;
</script>
</head>
<body></body>
</html>

But I am not being redirected to the app. I suspect something is wrong with my server code because a similar setup (the manifest and activity code) works when dealing with OAuth pages. What is wrong with my server code? How can I troubleshoot this?

EDIT

The Android side appears to be correct - Changing the server code to <a href="mytest://other/parameters/here">GO TO APP</a> appears to work but I need a way that does not involve clicking a link

EDIT 2

The code above actually works :/

Because I was in a Chrome custom tab I was redirecting to the page I was already at (because the Chroms custom tab activity is considered to be in-app). This made me think no redirect was occurring. Tristan's comment helped me realize this

like image 894
Daiwik Daarun Avatar asked Jan 14 '16 19:01

Daiwik Daarun


1 Answers

You can simply use Javascript as explained in This answer.

Basically you'll be making a link with an

<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">

in which package = your package name

For more information, see this question Open android application from a web page

like image 58
Tristan Wiley Avatar answered Nov 27 '22 06:11

Tristan Wiley