Reputation: 33297
When a user opens a website I want one of the following two cases to be happen:
Case 1 is easy you can use the following code:
window.location = "mycoolapp://";
This will work if the app is installed on the device. When the app is not installed it redirects the user to a blank page which is unknown.
For case 2 I have a solution using an iFrame which works great on iOS and on the native Android browser. It does not work on Chrome for Android.
var redirect = function (location) {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', location);
iframe.setAttribute('width', '1px');
iframe.setAttribute('height', '1px');
iframe.setAttribute('position', 'absolute');
iframe.setAttribute('top', '0');
iframe.setAttribute('left', '0');
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
};
redirect('mycoolapp://');
When the app is installed it is working on the native Android browser, but on Chrome for Android there is not redirect. Nothing happens on the page.
How can I make redirect to my app working on Chrome for Android without redirecting to a blank page when the app is not installed?
Edit: I know that you can use an Intent
window.location = "intent://#Intent;package=com.mycoolapp;scheme=mycoolapp;launchFlags=268435456;end;";
This is not what I want because it launches the app page on google play if the app is not installed. Is there a way that it will not redirect to google play?
Upvotes: 18
Views: 47132
Reputation: 157
Add this in manifest file,note the scheme.
<intent-filter>
<data android:scheme="startci" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
in html give the code, the scheme is same as in the manifest file.
<a href="intent:#Intent;scheme=startci;package=gramener.star;end">click here</a>
if you want to add parameter to your app use this
<a href="intent:#Intent;scheme=startci://open?url_param=hi santhosh;package=gramener.star;end">click here</a>
if the app is not installed so if you want to redirect to some other page add the fallback url like this. the url must be encoded
<a href="intent:#Intent;scheme=startci://open?url_param=hi santhosh;package=gramener.star;S.browser_fallback_url=http%3A%2F%2Fzxing.org;end">click here</a>
to get the parameter add below code
Uri data = this.getIntent().getData();
if (data != null && data.isHierarchical()) {
if (data.getQueryParameter("url_param") != null) {
String param = data.getQueryParameter("url_param");
Log.d("the param is",param);
//do something here
}
}
Upvotes: 11
Reputation: 78525
You don't need to launch your app on a custom protocol. It will work for any address, e.g. in your AndroidManifest.xml
:
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="www.mycoolapp.com" />
<data android:scheme="http" />
<data android:pathPattern="/Android" />
</intent-filter>
This means you can direct the user using:
window.location = "/Android";
If the app is installed, Android will prompt to "Open With". If not, the user will just be taken to the /Android page in their browser.
Upvotes: 6