Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deeplinking mobile browsers to native app - Issues with Chrome when app isn't installed

I have a webpage, lets call it entry.html.

When a user enters this page, a javascript code (see below) is attempting to deep-link the user to the native iOS / Android app.

If the deep-link fails (probably if the app isn't installed on device), user should "fall back" to another page- lets call it fallback.html.

here is the javascript code that is running on entry.html:

$(function(){     window.location = 'myapp://';     setTimeout(function(){         window.location = 'fallback.html';     }, 500); }); 

this is a standard deep-linking method that is recommended all over the network; try to deep-link, and if the timeout fires it means that deep-link didn't occur- so fallback.

this works fine, as long app is installed on device.

but if the app isn't installed, this is the behaviour when trying to deep-link:

Mobile Safari: I see an alert message saying "Safari cannot open this page..." for a moment, and then it falls-back properly to fallback.html- which is the expected behaviour.

Mobile Chrome is my problem.

when the app isn't installed, browser is actually redirected to the myapp:// url, which is of course, invalid- so i get a "not found" page, and fall-back doesn't occur.

Finally- my question is:

How can I fix my code so FALL-BACK WILL OCCUR on mobile Chrome as well? just like mobile Safari?

note: i see that LinkedIn mobile website does this properly, with Safari & Chrome, with or without the app installed, but i couldn't trace the code responsible for it :(

note2: i tried appending an iframe instead of window.location = url, this works only on Safari, mobile Chrome doesn't deep-link when appending an iFrame even if app is installed.

Thanks all!


UPDATE:

i found a decent solution, and answered my own question. see accepted answer for my solution.

like image 533
geevee Avatar asked Nov 26 '14 14:11

geevee


2 Answers

for whoever is interested, i managed to find a decent solution to solve these issues with deeplinking Chrome on Android.

i abandoned the myapp:// approach, i left it functioning only in cases of an iOS device.

for Android devices, i'm now using intents which are conceptually different than the myapp:// protocol.

I'm mainly a web developer, not an Android developer, so it took me some time to understand the concept, but it's quite simple. i'll try to explain and demonstrate MY solution here (note that there are other approaches that could be implemented with intents, but this one worked for me perfectly).

here is the relevant part in the Android app manifest, registering the intent rules (note the android:scheme="http" - we'll talk about it shortly):

<receiver android:name=".DeepLinkReceiver">     <intent-filter >         <data android:scheme="http" android:host="www.myapp.com" />         <action android:name="android.intent.action.VIEW" />         <category android:name="android.intent.category.BROWSABLE"/>         <category android:name="android.intent.category.DEFAULT"/>     </intent-filter> </receiver> 

now, after this is declared in the app manifest, i'm sending myself an email with "http://www.myapp.com" in the message.

when link is tapped with the Android device, a "chooser" dialog comes up, asking with which application i want to open the following? [chrome, myapp]

the reason this dialog came up upon tapping on a "regular" url, is because we registered the intent with the http scheme.

with this approach, the deeplink isn't even handled in the webpage, it's handled by the device itself, when tapping a matching link to an existing intent rule defined in the Android app manifest.

and yes, as i said, this approach is different by concept than the iOS approach, which invokes the deeplink from within the webpage, but it solves the problem, and it does the magic.

Note: when app isn't installed, no chooser dialog will come up, you'll just get navigated to the actual web page with the given address (unless you have more than 1 browser, so you'll need to choose one... but lets not be petty).

i really hope that this could help someone who's facing the same thing.. wish i had such an explanation ;-)

cheers.

like image 142
geevee Avatar answered Sep 20 '22 00:09

geevee


It is very important to make sure that when you try to open a deeplink URL with JavaScript that the URL is properly formatted for the device and browser. (If you do not use the appropriate deeplink URL for the browser/platform, a user may be redirected to a “Page Not Found”, which is what you experience.)

Now you must note that Chrome on Android has a different URL format than the old standard Android browser 1! You need to annotate the deep links using href="android-app://" in the HTML markup of your web pages. You can do this in the section for each web page by adding a tag and specifying the deep link as an alternate URI.

For example, the following HTML snippet shows how you might specify the corresponding deep link in a web page that has the URL example://gizmos.

<html> <head>     <link rel="alternate"           href="android-app://com.example.android/example/gizmos" />     ... </head> <body> ... </body> 

For more details, see the references here:
https://developer.chrome.com/multidevice/android/intents
https://developers.google.com/app-indexing/webmasters/server
https://developer.android.com/training/app-indexing/enabling-app-indexing.html#webpages

And here's a deep link testing tool for Android: https://developers.google.com/app-indexing/webmasters/test.html

Hope that helps.

1 Since the old AOSP browser was replaced by chromium, this is now the default way to handle deep links for recent Android versions. Nonetheless, Android still requires a conditional soltion, because older OS versions still use the AOSP browser.

like image 40
eyecatchUp Avatar answered Sep 22 '22 00:09

eyecatchUp