27

i have tried link1, link2,link3, link4, link5, link6

Here's everything described about DeepLinking

What i want is the custom uri myapp://some_data, opens the native application installed in the device that requires some_data to initialise the application.

There are 2 scenarios in which the custom url can be clicked.

1) from within the SMS app, when user taps the link it should automatically open the installed otherwise open the googleplay store where the app is hosted

2) from within the body of a email message.

I have tried all the above listed links, but none of them works for me. I m having major problem with the scheme part.

Here's my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="MainActivity"
        android:label="@string/app_name"
         android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="inderbagga" />
        </intent-filter>
    </activity>
</application>

and here's the MainActivity.java

TextView tvText=(TextView)findViewById(R.id.tvid);

    if (getIntent().getAction() == Intent.ACTION_VIEW&&getIntent().getScheme().equals("inderbagga")) {
        Toast.makeText(getApplicationContext(), ""+getIntent().getScheme(), Toast.LENGTH_SHORT).show();
        Uri uri = getIntent().getData();
        // do stuff with uri
        tvText.setText(uri.toString());
    }
    else tvText.setText("NULL");

To be more specific, i want to open the native application when u url of type inderbagga://a1b22c333 is clicked, Either from sms application or gmail/yahoomail email message body.

in order to achieve the same, i 've used intent filters to set the scheme. and getIntent() to read the data that equals to a1b22c333 in the MainActivity.

Community
  • 1
  • 1
inderbagga
  • 1,008
  • 1
  • 9
  • 18
  • "none of them works for me" -- this is not very useful. "i'm having problem with the scheme part" -- this is also not very useful. Please edit your question to explain, in deatil, what *specific* problems you are encountering. – CommonsWare Nov 25 '13 at 12:58
  • you want open app while typing url into a browser is it correct? – prakash Nov 25 '13 at 12:59
  • 1
    @prakash : i want to open the app from email message body say a gmail native app or gmail web app running in the browser. the second case may be, the app can also be started via tapping on the link within the SMS application. – inderbagga Nov 25 '13 at 13:18
  • @indeerbagga... Did you resolved your problem ?? ..Please guide me, i want also to implement these functionality...thank you – Ravindra Kushwaha Nov 27 '15 at 10:06
  • 1
    i had used http scheme, it gives an user the option to open the link either via browser or your application implementing the scheme. – inderbagga Nov 30 '15 at 06:41
  • In the email body, you can send "myapp://some_data" as a link. So it can be clickable in email but in SMS, we can use only http schema. – Kyaw Min Thu L Aug 01 '16 at 08:06
  • @CommonsWare I have a related question here https://stackoverflow.com/q/65407930/5282127 please help me. – Anice Jahanjoo Dec 26 '20 at 08:33

5 Answers5

17

click link means this code will work

          <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="domain.com(google.com)"android:pathPrefix="/wp/poc1/(sufexes)" />

    </intent-filter>

get url data

        //get uri data
     Uri data = getIntent().getData();
     //get schma
     String scheme = data.getScheme(); // "http"
     //get server name
     String host = data.getHost(); // Ipaddress or domain name
    //get parameter
     String urltextboxname=data.getQueryParameter("name");
     //set value in textview
     name.setText(urltextboxname);
prakash
  • 466
  • 4
  • 18
  • 2
    this block of code won't work, i need a custom scheme ... not the http stuff. – inderbagga Nov 25 '13 at 14:22
  • Only now I saw your answer, but the "scheme" can be custom, you can define what best suits you. But I guess you might have found out that after 2 years :) – Edison Spencer Dec 16 '15 at 11:23
12

To be more specific, i want to open the native application when u url of type inderbagga://a1b22c333 is clicked, Either from sms application or gmail/yahoomail email message body.

There are many SMS and email applications available for Android. Precisely none of them know to convert inderbagga://a1b22c333 into clickable entries. You could build a list of all of those apps, contact each of their development teams, and ask them to add it.

Or, you could have your app watch for a particular http:// URL instead. While the user would be presented with a chooser, to view that URL within your app or a Web browser, at least it will be clickable.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    plesae have a look at this link , http://en.wikipedia.org/wiki/Mobile_deep_linking facebook, twitter, ebay,youtube is already doinf this. inderbagga is nothing but a custom scheme value, i've seen in the links i've mentioned that people are already using custom schemes like – inderbagga Nov 25 '13 at 13:29
  • 4
    this is another supporting tutorial sir https://www.sparq.it/web/api/v1/docs/mobile_uris#deep_linking and http://www.deeplink.me/documentation . there must be a way to achieve this. but itsn't working in my case..i'm doing it wrong. that might be the case ! – inderbagga Nov 25 '13 at 13:32
  • look at Linkify.java and see how inderbagga:// could make an automatic link (URLSpan) in a TextView – pskink Nov 25 '13 at 13:43
  • @inderbagga: "facebook, twitter, ebay,youtube is already doinf this" -- few SMS and email clients will recognize their URL schemes, either. Those that do hard-coded that support. You are welcome to ask them to hard-code support for you as well. – CommonsWare Nov 25 '13 at 13:44
  • 1
    @commonsware please have a look at the links i've mentioned, but a dozen people are implementing this. or can u please temm how to Add URI Support to Mobile, or what's the significance of android:scheme ? – inderbagga Nov 25 '13 at 13:51
  • 1
    @inderbagga links are created by Linkify class (https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/util/Linkify.java see addLinks() methods) – pskink Nov 25 '13 at 13:58
  • @inderbagga: "what's the significance of android:scheme ?" -- primarily, it is for use with an `ACTION_VIEW` `Intent` called by `startActivity()`. "please have a look at the links i've mentioned, but a dozen people are implementing this" -- few SMS and email clients will recognize their URL schemes, either. Those that do hard-coded that support. You are welcome to ask them to hard-code support for you as well. Just because you *advertise* that you support a new scheme does mean that everyone will drop everything to make strings with that scheme be clickable. – CommonsWare Nov 25 '13 at 14:00
  • 1
    @CommonsWare : Suppose for a while i m asking the wrong question, this might be the case. then is it invalid question ? if so how about it's agreed solution with 50+ upvotes. http://stackoverflow.com/questions/2448213/how-to-implement-my-very-own-uri-schema-on-android/2448531#2448531 – inderbagga Nov 25 '13 at 14:19
  • 1
    @inderbagga: That solution *advertises* that you *support* a scheme. It does not force anyone to honor it by making things clickable. To draw an analogy, hosting a Web server with Web pages does not force everyone in the world to link to those pages. – CommonsWare Nov 25 '13 at 14:29
  • 1
    @commonsware okay sir ! so according to you can i open my native app via http scheme with having some payload ! – inderbagga Nov 25 '13 at 14:52
  • 3
    @inderbagga: Yes. Any SMS or email app that supports linking to `http` or `https` URLs will know to convert *your* `http` URL into a link as well. The app then blindly asks Android to process the link, which will display the chooser (your app, or the browser?). Ideally, use a URL that is a live page on a Web server you control, so you can have that page provide instructions to the user to download the app or choose you from the chooser. – CommonsWare Nov 25 '13 at 15:19
  • 4
    @CommonsWare : it's all working fine, sir ! what if application is already installed and i want to bypass the chooser option to select the application by default instead od asking for whether to open via browser or application ? – inderbagga Nov 26 '13 at 12:44
  • 2
    @inderbagga: You cannot control that. The *user* can, if they have an option in the chooser for "Always" or "Make Default" or the equivalent. – CommonsWare Nov 26 '13 at 13:36
3

You might also want to try out this library which facilitates declaring deep links and extracting out the parameters you need:

https://github.com/airbnb/DeepLinkDispatch

It allows you to declare the URI you're interested in and the parameter you'd like to extract through annotations, without having to do the parsing yourself.

Christian
  • 799
  • 7
  • 15
2

Few things I have understood based on my knowledge and after reading all the answers and comments.

Implementing Deep Link

The OP's AndroidManifest.xml implementation is correct. Adding <data android:scheme="inderbagga" /> will add deeplink for all URLs starting with "inderbagga". So inderbagga://something.com will work.

Testing Deep link

Instead of testing your implementation by tapping links on SMS app try to follow this ADB command mentioned on Android Deeplink documentation.

$ adb shell am start
    -W -a android.intent.action.VIEW
    -d <URI> <PACKAGE>

Why testing link on Browser/SMS might not work?

Based on my experience most chat apps (including WhatsApp) won't convert custom schemed URL into tappable links. Also sending custom schemed URLs on Gmail won't work. Plus, entering inderbagga://something.com on Google Chrome (Android) will do a Google search instead of sending an intent to your app.

What works then?

  • Test your implementation using ADB cmd (link)
  • Create a HTML webpage <a href="YOURSCHEME://something.com/" target="_blank">. Try this w3school code snippet if you like (link)
Slick Slime
  • 649
  • 7
  • 19
1

It looks like the problem you are having is that your email and SMS client are not parsing the URI scheme and path correctly. Most do not allow you to enter in a URI scheme directly. Additionally, if the app is not installed, you need to fallback to the Play Store so the user is given a good experience.

In order to do this, you need to call the URI scheme in client side JS. You can setup a simple web host and use this script:

<script type="text/javascript">
    window.onload = function() {
        var method = 'iframe';
        var fallbackFunction = function() {
            if (method == 'iframe') {
                window.location = "market://details?id=your.package.name";
            }
        };
        var addIFrame = function() {
            var iframe = document.createElement("iframe");
            iframe.style.border = "none";
            iframe.style.width = "1px";
            iframe.style.height = "1px";
            iframe.src = "inderbagga://a1b22c333";
            document.body.appendChild(iframe);
        };
        var loadChromeIntent = function() {
            method = 'intent';
            document.location = "intent://a1b22c333#Intent;scheme= inderbagga;package=your.package.name;end";
        };
        if (navigator.userAgent.match(/Chrome/) && !navigator.userAgent.match("Version/")) {
            loadChromeIntent();
        }
        else if (navigator.userAgent.match(/Firefox/)) {
            window.location = "inderbagga://a1b22c333";
        }
        else {
            addIFrame();
        }
        setTimeout(fallbackFunction, 750);
    };
</script>

Or, you can use a service like branch.io which automatically assembles this client side JS for you in addition to working on desktop and iOS as well.

Alex Austin
  • 1,142
  • 8
  • 11