18

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.

I have gotten this partially working in a few different ways but have found a few problems that I was wondering if anyone has solutions to.

Attempt 1: Using a custom scheme: myapp://someItem. This works but some email applications do not treat this as a link as it is not http. Is there a way to force applications to treat it as a valid link? gmail for example.

Attempt 2: Using a http link with a host: http://com.myapp/someItem. This works as well but my app ends up registered to handle all http links which is not ideal.

Attempt 3: Using a http link with host and port: http://com.myapp:2345/someItem. This is my current solution with the only drawback being that when the link is opened it still gives the option of opening the link in a browser. Is there a way to stop the browser attempting to open my links?

Does anyone have a way to make links that will be treated as links by all applications and also be ignored by the browser when opening them?

skorulis
  • 4,361
  • 6
  • 32
  • 43
  • Can you please point me to some resources which helps me to - " how to enable deep Links for App Content" - The deep link should take users directly to the content, without any prompts, interstitial pages, or logins. – user2056563 Aug 31 '15 at 10:36

4 Answers4

10

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);
}
xarlymg89
  • 2,552
  • 2
  • 27
  • 41
Ashutosh Srivastava
  • 597
  • 1
  • 9
  • 13
  • 3
    Although this answer may be technically correct it would be worth padding out your answer by explaining what it does. This will help both the OP and future visitors. As it stands, this is just a code dump. – Bugs Mar 02 '17 at 10:41
8

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.

bitsabhi
  • 728
  • 8
  • 12
4

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.

mikerowehl
  • 2,222
  • 1
  • 17
  • 20
  • 1
    I've gotten this 90% working. The only I don't like is that it leaves a page in the browser. I would like it to go back 1 page on success and stay where it is on failure to display a download link. Have you found any way to check if the redirect worked? – skorulis Feb 10 '11 at 22:31
  • 1
    Sorry, but I haven't fooled around with it enough to optimize for history and stuff like that, just tested out the basics. Normally you can launch with a no history flag in an intent to take care of the problem with leaving a page in the browser. But because "something else" is starting it up, I'm not sure how you would go about finding a fix. Good luck! Happy the basics are working at least. – mikerowehl Feb 10 '11 at 22:54
  • @mikerowehl Add code in your answer or update the link added – Marsad Jun 04 '21 at 10:58
0

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...

John Chadwick
  • 3,193
  • 19
  • 19
  • How would I check from the browser if the application was installed? – skorulis Feb 10 '11 at 03:46
  • Oh, I didn't think about that. I don't know if there's an easy way to do that. At least, a click-through page would be possible. ; edit: Maybe you could run an HTTP server service on the application, then use that to detect the presence of the application? That's about all I can come up with at the moment. – John Chadwick Feb 10 '11 at 03:48