DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

Can I pass a variable to an Android app from a link in an email?

StackOverflow https://stackoverflow.com/questions/18181147
 Checked

Question

I have a service that users register for, and part of the registration process requires an email confirmation. There are three ways a user might sign up for the service. On the web, using an iPhone app, or using an Android device.

When I send them the email to confirm their registration, I can include links to complete the registration, and to make the process easier on the user, I can attach their username and a verification code which will then be automatically placed in a form.

In the case of the HTML link, it looks like this:

<a target="_blank" href="https://mysite.com/confirmation.html?verification=XXXXXXXXX&username=larry">Complete verification on the web</a>

I can get the app to open on the iPhone by making a link to a PHP script:

<a target="_blank" href="https://mysite.com/iphoneopen.php?verification=XXXXXXXXX&username=larry">complete your verification with the iPhone app</a>

And then in the PHP script I have this:

<meta http-equiv="refresh" content="0;URL=myappname://?verification=XXXXXXXXX&username=larry"/>

What code do I use on the Android so that when a user gets the email on their phone (probably with the GMail app), they can click on a link and they will be taken directly to the app? And how do I include variables in that link?

I am using Adobe Phonegap Build for my Android app, so it is built with Javascript and HTML. As a result, I do not know what "intents filters" are. Please take that into consideration when answering. Thanks for your understanding.

Solution

In your AndroidManifest.xml, you need to add the following XML as a child of your Activity, which will add an Intent Filter (you don't need to know anything more than that):

<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="https" android:host="mysite.com" />
</intent-filter>

Then, in your main Java file, where you call a line like this:

super.loadUrl(“file:///android_asset/www/index.html”);

add the following code (after the line above):

try {
    Uri uri = getIntent().getData();
    String data = uri.getSchemeSpecificPart();//this will set data to //mysite.com/iphoneopen.php?verification=XXXXXXXXX&username=larry
    if (data != null) {
        String[] vars = data.split("?");
        vars = vars[1].split("&");
        String verification = vars[0].split("=")[1];
        String username = vars[1].split("=")[1];
        //TODO: handle verification and username from here.
    }
} catch (Throwable t) {
    t.printStackTrace();
}

OTHER TIPS

I don't know how stuff works with Phonegap etc., but using pure Android, when your app will open, your Activity will be started actually by an Intent, so you can use getIntent().getUri(), and you'll actually get the url that have started the app. You can then extract the query params from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow