I am trying to create a link that can be sent via email which when opened on an android device with my app installed will automatically open the correct page in my app.
1 - Set Custom URL schemes like http://example.com
For example, the URL http://example.com/?id=95 , will open up the relevant FAQ and the URL http://example.com/?sectionid=8 (where sectionid is the publish id of any Section), will open up relevant section.
2 - Define in your AndroidManifest.xml
your DeepLinkActivity
(the one that will receive the URL data:
<activity android:name="com.example.shilpi.deeplinkingsample.DeepLinkActivity">
<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="http" android:host="example.com"/>
</intent-filter>
</activity>
3 - Override the onResume()
method of your DeepLinkActivity
class:
@Override
protected void onResume() {
super.onResume();
Intent in = getIntent();
Uri data = in.getData();
System.out.println("deeplinkingcallback :- "+data);
}
To answer your questions, below is a sample intent filter in the manifest.
<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:host="www.example.com"
android:pathPrefix="/home"
android:scheme="https"/>
</intent-filter>
So basically, when some one clicks on any link starting with http://www.example.com/home, he/she will be given the option to open it with your app along with the Browser apps. You have to handle the intent in the activity
Also the scheme could be anything, but http scheme is recommended by google, so that both your app and the browser app can listen to clicks on the deeplinked url.
Note : Don't forget to add android-app://yourpackage name/http/www.example.com/home/... in your web pages, if you want app linking.
This technique actually seems to be working on Android from what I can tell:
http://mobile.dzone.com/news/custom-url-schemes-phonegap
I haven't tried it in a real production app yet, so your mileage may vary. What I've done is use that technique of creating a hidden iframe and attempting to set the location to the custom url scheme, and call the function from onload for the document. What I've seen so far (I've only tested it out on 2.2 and 2.3 devices) is that if I have an app installed that handles the custom scheme the app will launch, and if not the page will render.
Relatively clean single URL to cover both cases, and doesn't ruin things like Twitter shares of the URL. A real production version might only do the hidden iframe probe if the request is coming from something that looks like a platform that might support the application to reduce the risk of incompatible desktop behavior.
If you can use something server-side, maybe you could have a regular page which jumps to the custom-scheme link from the browser when visited? This has the added advantage of possibly being able to tell if the application is not installed and to prompt installation.
It doesn't fix the problem of links opening in the browser, though. The only true solution I can think of for that is a link-handler that jumps to the appropriate application. Then the issue would be allowing the user to change the preferred browser...