75

I have a website which enables the user to make a search query. The query might take some time to complete (minutes to days), and I would like to enable the user to download an Android app and receive the answer there, by sending an email with a link to the user.

I would like this mechanism to work whether the user has the app installed or not; in other words:

  • If the user has the app it should be opened with deep link that contains an identifier argument.
  • If the user does not have it it should open the play store on the app's page (e.g. https://play.google.com/store/apps/details?id=com.bar.foo&referrer=BlahBlah), let the user install it, and open the app with the identifier argument.

enter image description here

Is there a way to form a link that opens an Android application with an argument, that would work regardless if the app is installed or not?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • This is not possible to implement with the Play Store. If you offer a download link from your own server, then you may be able to offer a custom compiled APK download that contains the desired identifier. – corsair992 Feb 26 '15 at 17:40
  • not even with firebase? – Dinesh Apr 09 '18 at 16:21
  • 1
    @DineshVG Not sure. Much has changed since, and I'm not working on that project anymore. – Adam Matan Apr 09 '18 at 19:22

4 Answers4

127

This workaround might work:

  1. At the server side, create a redirect rule to google play. For example, https://www.foo.com/bar/BlahBlah will redirect to https://play.google.com/store/apps/details?id=com.bar.foo&referrer=BlahBlah.

  2. At the app, register the server side link as a deep link:

<data android:scheme="https"
          android:host="www.foo.com"
          android:pathPrefix="/bar" />

Now, if the app is installed, the URL will be caught and the path can be parsed to extract the BlahBlah part. If the app isn't installed pressing the link will redirect the user to the Play store with the referring URL.

enter image description here

Notes:

  • /bar/BlahBlah was converted to &referrer=BlahBlah, because the play store receives a URL argument and the deep link mechanism works with URL paths (as far a I can tell)
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • 3
    do you know if the argument is passed to the app even if it is not opened right away from the play store? – njzk2 Feb 05 '16 at 20:56
  • could you please have a look at this too: http://stackoverflow.com/questions/38157051/firebase-deep-link-opening-play-store-even-when-the-app-is-installed Thanks! – Hammad Nasir Jul 02 '16 at 18:54
  • 1
    As per this article : https://blog.branch.io/technical-guide-android-deep-linking-uri-schemes "The URI method of deep linking is very restrictive and not recommended without significant modification as it will show an error message if your app is not installed. " Your answer and this contradicts any suggestions ? – Utsav Gupta Jul 29 '16 at 13:37
  • 3
    I have a doubt here, why do i need to send it to my server, why can't I simply use google play store link as the deep link? I am totally new to deep linking btw. – Utsav Gupta Jul 29 '16 at 14:22
  • I know this is a pretty much old post, but I have a question: if I need to pass an argument like `www.myserver.com/mydir/index.aspx?myargument=value` how should I revert this into a format for the store and to open the app with that parameter? thanks for this great post – Pier Giorgio Misley Dec 29 '16 at 12:13
  • @PierGiorgioMisley Simply encode the data in the referrer argument – Adam Matan Dec 29 '16 at 15:33
  • @AdamMatan - Works only for `www.example.com` not `http://example.com` may i know reason? – 151291 May 24 '17 at 04:35
  • You should add to AndroidManifest both intent-filter. One for `www.example.com`, another for `example.com` – Yura Shinkarev Aug 01 '17 at 15:28
  • I recommend using branch.io's platform for this, as they handle all this logic as well as OG tags, iOS, etc etc. – Elliott Beach Aug 29 '17 at 19:45
  • Please use a real example, i'm tired of this foo bar programmer turd :D Thank you sir for sharing your knowledge enabling others and my self to improve on our journeys. – Michael Nov 12 '17 at 23:46
  • @UtsavGupta because I don't think you can specify query params in the intent filter https://developer.android.com/guide/topics/manifest/data-element.html – Sohail Nov 29 '17 at 16:33
  • @AdamMatan is the url caught by Deep link automatically? can you please see this question https://stackoverflow.com/questions/47792132/android-deeplink-with-facebook-not-working-redirecting-to-play-store-instead-o?noredirect=1#comment82545494_47792132 – keshav kowshik Dec 15 '17 at 07:14
  • My app doesn't get caught the deep link actually it captured by play store due redirection to play store. do we need to make a rule for redirection? – Mohit Dixit Jun 13 '18 at 07:17
  • Well, what if I have 2 apps. first one `scheme="https"` , `host="www.app1.com"` and second one `scheme="https"` , `host="www.app2.com"`. the URL will confuse which one to open. – Ahmed Salah Nov 06 '18 at 09:06
18

You can try using this scheme(to be sent to the user):

intent://details?id=X&url=Y&referrer=Z#Intent;scheme=market;action=android.intent.action.VIEW;package=com.android.vending;end";

X: Package name of the App

Y: Deep link scheme which should be defined in the App's manifest. (Please refer this) Here, they have used this URL as an example: "http://www.example.com/gizmos" , therefore Y should be replaced by this URL.

Z: Can be any data which you want to pass to the App via Google Play. Please take note that any data which you pass should not be '&' separated because the original parameters are itself '&' separated.

From what I experimented, this URL is understood by the browser and it redirects you to the App based on the package name and the deep-link scheme. Else it takes you to the Google Play.

PS: The Google Play makes a broadcast to the app. So make sure you receive the broadcast in a receiver.

Chirag
  • 56,621
  • 29
  • 151
  • 198
Shivam Dixit
  • 318
  • 4
  • 13
8

This question is pretty old but also very popular, so it's definitely worth knowing that now this use case is officially supported by Firebase, precisely Firebase Dynamic Links.

It supports just opening app and deferred deep linking, which means that after installing app, when app is starting, you can retrieve data (link) that was used to install the app.

It's built on top of App Linking, so you still need the same intent-filter as before.

In short, you need to add implementation 'com.google.firebase:firebase-dynamic-links:VERSION' and retrieve link from instance of FirebaseDynamicLinks class.

Of course you need to change links on website with those generated in Firebase console, but the good news is that these links are "dynamic" (as the name implies) so they work on all systems.

For all detailed info and set up with that use case up go here: https://firebase.google.com/docs/dynamic-links/use-cases/web-to-app

michalbrz
  • 3,354
  • 1
  • 30
  • 41
2

Adam's solution is pretty good if you want to build it yourself.

There's an open-source SDK that does exactly this, by Branch Metrics (full disclosure, I work there).

Basically, you set your URI scheme on the Branch dashboard, create a link with the data you want to pass (in this case, the query parameters, but you decide), and the SDK has a method with a callback that gives you said query parameters (through install or regular deeplink by clicking the Branch link).

Sahil Verma
  • 517
  • 2
  • 5
  • do you know if the argument is passed to the app even if it is not opened right away from the play store? – njzk2 Feb 05 '16 at 20:56
  • 1
    yeah, that's what 'deferred deep linking' is. as long as a link is clicked, the data will pass through the play store, within a 2 hour time frame. – Sahil Verma Feb 08 '16 at 18:49
  • `within a 2 hour time frame` that's a very interesting information! thanks! is there a source that mentions that time frame, in case it ever changes? – njzk2 Feb 08 '16 at 19:12
  • Branch.io is listed on a web content filter system I'm using. I'm not sure if that blacklisting is legit or not, but it seems like something you might want to look into. – Tony Adams Apr 18 '19 at 18:23