EddyVerbruggen / Custom-URL-scheme

:link: Launch your Cordova/PhoneGap app by a Custom URL scheme like mycoolapp://
1.03k stars 365 forks source link

[support] use ANDROID_SCHEME and ANDROID_HOST instead of URL_SCHEME #291

Open guirip opened 5 years ago

guirip commented 5 years ago

Hello

This plugin is great, really, but... we need a web fallback. To achieve this, I would rather use the variables ANDROID_SCHEME and ANDROID_HOST also managed by this plugin. e.g: cordova plugin add cordova-plugin-customurlscheme --variable ANDROID_SCHEME=http --variable ANDROID_HOST=mywebsite.com --variable URL_SCHEME=idontcare

It works and is very powerful because if a user shares a link (e.g http://mywebsite.com/blabla/bla) when the link is clicked :

Pretty awesome! We've nearly achieved our goal, but...

@EddyVerbruggen have you any suggestion please?

JerryBels commented 5 years ago

Hello, have you also tested how it behaves on ios ? Also, have you made any progress since ?

guirip commented 5 years ago

Hello @JerryBels I haven't tested yet on iOS, but I managed to make it work on android.

The solution is to remove Custom-URL-scheme plugin from your project and do it yourself.

For Android

config.xml Add this:

If you set attribute android:autoVerify="true" on the <intent-filter>, then you will have to deploy a .well-known/assetlinks.json. See https://developer.android.com/training/app-links/verify-site-associations

Get the intent value Create a small cordova plugin that exposes a function to get the intent. You will call this function from your javascript, it can return:

I can't speak for iOS now, but for android this is how to get the query string:

package com.yourcompany;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;

import android.content.Intent;
import android.content.res.Resources;
import android.content.res.Configuration;
import android.util.Log;

public class YourIntentPluginName extends CordovaPlugin {

    private static final String LOG_TAG = "YourIntentPluginName";

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        Log.d(LOG_TAG, "initialize");
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        Log.d(LOG_TAG, "executing action "+action);

        switch (action) {
            case "getIntent":
                callbackContext.success(this.getIntentUri());
                break;

            default:
                callbackContext.error("Invalid action: " + action);
                return false;
        }

        return true;
    }

    private String getIntentUri() {
        // see https://developer.android.com/training/app-links/deep-linking#java
        // javadoc:
        // see https://developer.android.com/reference/android/app/Activity
        // see https://developer.android.com/reference/android/content/Intent.html
        Intent intent = this.cordova.getActivity().getIntent();

        Log.i(LOG_TAG, "intent data string: "+intent.getDataString());
        return intent.getDataString();
    }

}

(Would be even better with a try/catch, to call callbackContext.error if any exception is raised.)

Finally:

...

<platform name="android">
    <config-file target="res/xml/config.xml" parent="/*">
        <feature name="YourIntentPluginName">
            <param name="android-package" value="com.yourcompany.YourIntentPluginName"/>
            <param name="onload" value="true" />
        </feature>
    </config-file>

    <source-file src="src/android/YourIntentPluginName.java" target-dir="src/com/yourcompany" />
    ...

In your webapp, you can now call window.YourIntentPluginName.getIntent(successCb, failureCb)

I haven't tested it in production but on a dev environment (using cordova 8.1.2 and cordova-android 7.1.4) it works like a charm.

Send an email with a link: http://yourwebsite.com/... Click on it from the device, if your app is installed then android suggests you to launch the app, if not, then android directly starts the browser with specified url.

guirip commented 5 years ago

For iOS

You need to follow Universal Links documentation: https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html

Procedure includes:

I won't have the time now but if anyone has feedback on this you're welcome to share!