Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep linking from Chrome to native app

I am working with the Uber API which uses uber://?... URLs to deep link into the Uber native app. I am building a mobile website and I'm building one of these URLs as described here;

On iOS everything works fine but on Android the Uber app opens and only gets one of the parameters product_id. So I think there is some issue with how I am encoding the URL and how the Android system is opening it. Here is my JS to build the URL:

uber.createURL = function() {

    var params = {
        "client_id": uber.CLIENT_ID,
        "product_id": maps.product_id,
        "pickup[latitude]": maps.noSurgeMarker.getPosition().lat(),
        "pickup[longitude]": maps.noSurgeMarker.getPosition().lng(),
        "dropoff[latitude]": maps.destMarker.getPosition().lat(),
        "dropoff[longitude]": maps.destMarker.getPosition().lng(),
        "pickup[formatted_address]": $('#pickup').val(),
        "dropoff[formatted_address]": $('#destination').val()
    };

    var url = 'uber://?action=setPickup';

    for (var key in params) {
        if (params.hasOwnProperty(key)) {
            url += ('&' + key + '=' + encodeURIComponent(params[key]));
        }
    }

    return url;
};

I am then calling the following code to open the link:

var url = uber.createURL();
window.location.href = url;

Am I missing something obvious? Again, this works on iOS but not on Android. Also the strange thing is if I generate the URL using createURL on a computer, send it over to my Android device using PushBullet, opening it works perfectly. But if I get the URL through Chrome for Android the Uber app opens and only has product_id correct, not any of the pickup or dropoff parts.

Note: I already trued using jQuery's params function but that didn't work any better, which is why I went to the manual for (var key in params) {...} loop.

like image 768
Sam Stern Avatar asked Sep 17 '25 21:09

Sam Stern


2 Answers

Ok well I figured out it is not an issue of encoding but rather how Chrome launches intents to apps. Here is an intent from Pushbullet, this one works fine:

I/ActivityManager(  942): START u0 
{act=android.intent.action.VIEW 
dat=uber://?
client_id=REDACTED&
action=setPickup&
product_id=a1111c8c-c720-46c3-8534-2fcdd730040d&
pickup[latitude]=37.780654&
pickup[longitude]=-122.405599&
dropoff[latitude]=37.7970558&
dropoff[longitude]=-122.43199099999998
flg=0x10000000 cmp=com.ubercab/.client.feature.launch.LauncherActivity} from pid 19023

Here is an intent from Chrome, this one launches the Uber app but none of the data is parsed by the app (it just opens as if I had clicked the launcher icon):

I/ActivityManager(  942): START u0 
{act=android.intent.action.VIEW 
cat=[android.intent.category.BROWSABLE] 
dat=uber://?
client_id=REDACTED&
action=setPickup&
product_id=a1111c8c-c720-46c3-8534-2fcdd730040d&
pickup[latitude]=37.780654&
pickup[longitude]=-122.405599&
dropoff[latitude]=37.7970558&
dropoff[longitude]=-122.43199099999998
flg=0x10000000 cmp=com.ubercab/.client.feature.launch.LauncherActivity (has extras)} from pid 26446

You can see that the only difference is the added cat=[android.intent.category.BROWSABLE] which is something that is required for intents launched from Chrome for android (reference).

So overall I think this is something Uber will have to solve. For now it looks like I'm going to have to package my website into a WebView in an Android app if I want it to work.

like image 108
Sam Stern Avatar answered Sep 19 '25 12:09

Sam Stern


You need to call encodeUriComponent on both key and params[key].

like image 45
Greg Ennis Avatar answered Sep 19 '25 13:09

Greg Ennis