Custom URL scheme for Cordova - android

I am trying to find out a good document on "How to define custom URL scheme for a Cordova app(on both iOS and Android platforms)".
I have spent hours on internet but couldn't find a good answer. I got some links which are related but not helping me much.
Mine is a Cordova app which runs on iOS and Android platforms. I need to enable my app to be started upon invoking a URL from email(ex: Myapp://).
Please advise me what configuration changes should I maketo my Cordova app to enable this feature.
EDIT:
Android manifest
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.simple.app" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="#drawable/icon" android:label="#string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="#string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="#android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="#string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="com.test.simple" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
Url
Launch The App

I know that you are specifically asking for documentation about how to hand-code this yourself, but just FYI there is a nice plugin that will do all of the (considerable amount of!) work for you:
https://github.com/EddyVerbruggen/Custom-URL-scheme
When you install it, you just provide the URL scheme that you want to use to launch your app:
$ cordova plugin add https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git --variable URL_SCHEME=myCustomUrlScheme
And that's pretty much all there is to it. Works on Android and iOS.

Android :
In the manifest :
<activity
android:name=".MainActivity"
android:label="#string/activity_name"
android:launchMode="singleTask"
<intent-filter android:label="#string/launcher_name" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="yourappscheme"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
android:launchMode="singleTask"
Try to use singleTask and singleTop and find the difference.
Both have different functionalities.
For iOS :
So, in your app page create a button called "Redirect to my App" with href as "yourappscheme://".
Open my app
Also there are other parts like Scheme, host.
Consider If you wanna get params from the url, you have to go with Native code.
Say your url is something like : yourappscheme://?username="xxx#gmail.com"
Android:
put this code in OnCreate.
Intent intent = getIntent();
Uri data = intent.getData();
if(data!=null) { //
//get schma
String scheme = data.getScheme(); //
Toast.makeText(getActivity(), scheme, Toast.LENGTH_LONG).show();
if(scheme.contains("yourappscheme")) {
//get parameter
String urltextboxname = data.getQueryParameter("username");
system.out.println(urltextboxname) // it will print xxx#gmail.com
}
}
For iOS :
In your App Delegate :
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
if (!url) {
return NO;
}
// calls into javascript global function 'handleOpenURL'
NSString* jsString = [NSString stringWithFormat:#"handleOpenURL(\"%#\");", url];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];
// all plugins will get the notification, and their handlers will be called
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
NSString *myUrlString = [url absoluteString];
if ([myUrlString containsString:#"yourappscheme://"])
{
NSLog(#"Calling Application Bundle ID: %#", sourceApplication);
NSLog(#"URL scheme:%#", [url scheme]);
NSLog(#"URL query: %#", [url query]);
}
return YES;
}
Hope this is clear.

I do this :
go to your folder project and open your cmd on that
next type this below :
cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=mycoolapp
instead of mycoolapp write your app name ,but I just remove (app) on cmd when I paste this but I do not sure it is important or not,so write below in your (index.html) on folder (www) .
<script type="text/javascript" src="js/plugins/LaunchMyApp.js"></script>
and next ,when you add plugin (customurlschema) it added on the plugin folder on your project,so go to the
cordova-plugin-customurlscheme->www->android->LaunchMyApp.js
and copy (LaunchMyApp.js),now as you see that
<script type="text/javascript" src="js/plugins/LaunchMyApp.js"></script>
the (src) is "js/plugins/LaunchMyApp.js" ,so you make this path on your (www) folder and paste (LaunchMyApp.js)
on that .now build your app.if at the first time your app isn't build so remove android with
cordova platform rm android
and add it again to your project,and build twice.may it work for you because I test on project,for work with this plugin you should put like tag below on your html page
Open my app
example:
I write this below:
cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=mycoolenglish
so my (href) is like below:
Open my app
and I test it.
.my english isn't very well,so excuse me at first.

UPDATE 2023
As of 2023, PhoneGap is deprecated so using the install script suggested by the accepted answer will fail every time. There is no candidate for the installation of PhoneGap.
If you try to run this in your terminal :
cordova plugin add https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git --variable URL_SCHEME=myCustomUrlScheme
This will return this error :
Failed to fetch plugin
https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git via
registry. Probably this is either a connection problem, or plugin spec
is incorrect. Check your connection and plugin name/version/URL.
CordovaError: Error: No git binary found in $PATH
TLDR
To install the custom-url-scheme plugin run this :
sudo cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=myapp://redirect.html?redirect&return=true
Then on your server you should have a page :
redirect.html
Click here to open app

Related

Deep Linking from browser not working Android

Deep Linking not working in android. I have pasted my manifest code here. When I tested, it goes to the website and not opening my activity in my app. Can someone help me fix this?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.clinicloud.app" >
<application
android:name=".MainApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<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.XXXX.com"
android:pathPrefix="/xyz"
android:scheme="http"/>
</intent-filter>
</activity>
<activity
android:name=".OnboardingActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ProfileViewActivity"
android:label="#string/title_activity_profile_view"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
</application>
</manifest>
UPDATE:
adb shell am start -W -a android.intent.action.VIEW -d "http://www.clinicloud.com/xyz" com.clinicloud.app
Testing with adb opens the app but just with browser its not opening the app.
It's so annoying to test deep link through adb as the Android Developer team suggested. We won't get the gist of the problem I think. We want a straight forward method like paste a link to the browser in the same device which also installed the app already, hit GO, then the app comes up. But unfortunately, it won't work though.
Reason:
It's not a pure link, when you go to URL search of the browser it actually does a different URL, e.g: 'https://www.google.com/search?....'
Solution: Open a pure link
As #Parag Chauhan suggested, using another app to open a link, sure it is a definitely working solution because it's a pure link. However, this is not practical as expected.
Alternatively,
2.1. You can build your own simple web page (simple file *.html) with a link, then click it to open a pure link. Here is an example of the web page:
<html>
<h1>
<a id="deeplink" href="ss.wohui.learn://subscribed">Deep Link</a>
</h1>
</html>
2.2. Replace ss.wohui.learn://subscribed with what your deep link is.
2.3. Download the file to your device.
2.4. Open it by a browser.
2.5. Click on the Deep Link.
2.6. The app will start.
Add this code in Manifest.xml
<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.clinicloud.com"
android:path="/xyz"
android:scheme="http" />
</intent-filter>
For test create another project and run below code
Intent i = new Intent(
Intent.ACTION_VIEW , Uri.parse("http://www.clinicloud.com/xyz")
);
startActivity(i);
You can also pass parameter in link. For more info, see my blog post Make a link in the Android browser start up my app?
This will only work if server has /.well-known/assetlinks.json properly defined in the root domain on the server.
So you need to create a json file and define there package name and SHA256. Then guys from backend should put this json file into root domain on the server.
The content of the file looks like:
[
{
"relation": [
"delegate_permission/common.handle_all_urls"
],
"target": {
"namespace": "android_app",
"package_name": "com.blablabla.staging",
"sha256_cert_fingerprints": ["XXXXXXXXXXXXXXXXXXX"]
}
}
]
You can split it for different environments: staging, beta, prod etc.
I tested in default browser of Samsung “Internet” and it fired the deeplink!

App name issue with Imported Cordova android app

I am creating android app using Cordova. For that I am using the following commands in cmd.
cordova create example com.path.example ExampleApp
cd example
cordova platform add android
cordova plugin add cordova-plugin-network-information
cordova build android
After these process and copying all webpage codings in assets/www folder successfully.
After this
I import it into eclipse the project is created as MainActivity not ExampleApp. Also I cannot change the App name. I changed app name in config.xml and AndroidManifest.xml file and tried many things. But no use.
so I created a new project and imported. But I cannot import that app because of the new app also have the MainActivity as app name. I googled and searched more over in Stackoverflow and other sites. But cannot fix. Please advise.
We can solve this problem by doing following steps,
Open AndroidManifest.xml file from
platforms->android->AndroidManifest.xml
Change as android:name="Project_name" and
android:label="#string/app_name" in activity
<application android:hardwareAccelerated="true" android:icon="#drawable/icon" android:label="#string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="#string/app_name" android:launchMode="singleTop" android:name="FLP" android:theme="#android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="#string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
After that import your project into eclipse.

Android Wear - WearablePkgInstaller﹕ Package install failed returnCode -104

I'm playing around with getting a custom watch face working, but when I package the app with the mobile apk and install the signed mobile APK onto my phone, I get the following error message in my logcat when the wear apk tries to install onto the watch
1149-1159/? E/WearablePkgInstaller﹕ Package install failed com.ptrprograms.wearcustomwatchface, returnCode -104
Does anyone know where I can find a list of the return codes so I can figure out what's going on? The only differences I have in my code from the base wear project that's generated by Android Studio are in the manifests:
wear manifest:
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
mobile manifest:
<activity
android:name=".CustomWatchFaceActivity"
android:label="#string/app_name"
android:enabled="true"
android:taskAffinity=""
android:allowEmbedded="true"
android:theme="#android:style/Theme.DeviceDefault.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.google.android.clockwork.home.category.HOME_BACKGROUND" />
</intent-filter>
<meta-data
android:name="com.google.android.clockwork.home.preview"
android:resource="#drawable/ic_launcher"/>
</activity>
Additional info - added the LAUNCHER category intent filter and it installs then, so it looks like watch faces right now may need to be included with a normal app as well, but someone probably has a workaround for that as well.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
EDIT: Below is a new blog post of mine that explains the new Watch Face API in Lollipop.
http://toastdroid.com/2014/12/10/developing-a-watch-face-for-android-wear-lollipop/
Make sure that your mobile and wear modules have the same package name and that the mobile app contains all of the permissions that the wear module needs. Also, the WAKE_LOCK permission is not required by watchfaces. You can read more about watchface dev on the blog post I did:
http://toastdroid.com/2014/07/18/developing-watchfaces-for-android-wear/
Remove the following 2 permission lines from your wear AndroidManifest.xml file:
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
I believe the the intent filter is optional if you only want your app to be a watch face.
Here is a watch sample app I found that you can look at for reference.
https://github.com/kentarosu/AndroidWearAndLIFX
Just needed to add the permissions in the mobile manifest

Phonegap facebook plugin errors - cordova cannot be resolved

I have tried everything to get the phonegap facebook plugin working on android, but to no avail. I am using cordova 3.0. I have followed the instructions at: https://github.com/Raffaello/phonegap-facebook-plugin/blob/master/README.md
So the application tag in AndroidManifest.xml looks like:
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:hardwareAccelerated="true"
android:debuggable="true">
<activity android:name="face_login" android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.LoginActivity"
android:label="#string/app_name" />
</application>
The bottom of the config.xml file looks like:
<plugins>
<plugin name="org.apache.cordova.facebook.Connect" value="org.apache.cordova.facebook.ConnectPlugin" />
</plugins>
</widget>
However, I get a huge amount of errors, such as:
CallbackContext cannot be resolved to a type ConnectPlugin.java
cordova cannot be resolved ConnectPlugin.java
error: No resource identifier found for attribute 'hardwareAccelerated' in package 'android' AndroidManifest.xml
PluginResult cannot be resolved to a type ConnectPlugin.java
The import org.apache.cordova.api cannot be resolved ConnectPlugin.java
webView cannot be resolved or is not a field ConnectPlugin.java
I have included the cordova-3.0.0.jar in the buildpath. But still getting all these errors. Any idea's?
That plugin hasn't been updated in 6 months so it probably will not run on Cordova 3.x stream without some modification.
Try it on a Cordova 2.9 to see it it works?
Or you can go through the platform upgrade docs and try to upgrade it yourself to 3.0. Unfortunately there is not a plugin upgrade guide but there should be.
You could also try to use the Network Graph page of Github (I clicked the Fork button) and see if maybe somebody has already ported the plugin to 3.X.
Edit: Looks like there is a fork here that has been updated for 3.0. I have no idea how well it works but I'm sure the author would love pull requests if anything is wrong!

application not installing on android emulator

I am running tab based application in android.When i try to install the application on the emulator it gives output in the console as
[2011-03-08 12:40:35 - TabBar] Application already deployed. No need to reinstall.
[2011-03-08 12:40:35 - TabBar] \TabBar\bin\TabBar.apk installed on device
[2011-03-08 12:40:35 - TabBar] Done!
Can anyone tell how should i pursue
Thanks in advance
Tushar
I'm also tackled with this same problem and finally I found the solution for it. When you creating a new android project using Eclipes and if you didn't create a activity for your first window, your project's Manifest also don't create proper codings for you to application launch up.
So, you should hardcode them yourself.
First check these codes are available or not in your Project's Manifest file, within your main activity tags.
**
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
**
These two code lines are very important. Because these two lines are calling to android OS to launch this app.
So, Make sure these codes are available in your project's Manifest file. If it's not this below Manifest file code will gets you rough idea to fix this problem.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mershanfernando.testingappz"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:name=".Main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Categories

Resources