Looking for an updated solution, running the latest ionic 1.1.0 release which uses Cordova 5.x. Trying to be able to browse a website in chrome and send that url to my ionic android app using web intent. My app compiles and runs, however when i attempt to use the share to feature from chrome(or any other app) and choose my app to share to, my app crashes.
I first attempted to use the plugin:
ionic plugin add https://github.com/Initsogar/cordova-webintent
and then removed the plugin and i also tried a more recently updated fork:
ionic plugin add https://github.com/fluentstream/cordova-webintent
In my app.js file I putting the following code:
.run(function($ionicPlatform, $rootScope, $ionicHistory, $state) {
$ionicPlatform.ready(function() {
window.plugins.webintent.getExtra(window.plugins.webintent.EXTRA_TEXT,
function(url) {
incomingURL = url;
//alert(incomingURL);
console.log(incomingURL);
}, function() {
incomingURL = false;
//alert("no url");
console.log("no url");
});
});
})
I also tried:
.run(function($ionicPlatform, $rootScope, $ionicHistory, $state) {
$ionicPlatform.ready(function() {
window.plugins.webintent.getUri(function(url) {
if(url !== "") {
alert(url);//url is the url the intent was launched with
}
});
});
})
In the file config.xml I would put:
<plugin name="WebIntent" value="com.borismus.webintent.WebIntent"/>
In the AndroidManifest.xml I would manually put in:
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
The app runs, but when I go to chrome and click the share button, and then choose my app, the app shuts down the following android message appears:
Unfortunately, MyAppName has stopped.
Can anybody suggest a solution to getting the share to intent to work with my app...or am i forgetting something and doing something wrong.
Thank you!
I want to add to @axel-napolitano's answer to make it explicit as to where the code should be added in an Ionic 3 application. Disclaimer: I can barely code Ionic apps.
At the time of writing, I am currently using Ionic 3.9. After a lot of trial and error and piecing together answers on SO, I was able to get this working (i.e. to have another app share to my Ionic app.)
At this point, you should have already compiled your app for Android and hooked up your Android phone to your computer so that the app runs on it. Look for documentation on how to run ionic apps on a physical Android device.
Here's the command: ionic cordova run android -l -c
.
You should also have added the <intent-filter>
into AndroidManifest.xml.
Put the code in /pages/home/home.ts - ensure your constructor has the 'platform' injected.
export class HomePage {
constructor(private platform:Platform){
...
}
...
}
Create this method (in home.ts) to receive the intent for when the app is not in a loaded state:
ionViewDidLoad(){
this.platform.ready().then(() => {
(<any>window).plugins.intent.getCordovaIntent(
function (Intent) {
//you should filter on the intents you actually want to receive based on Intent.action
console.log('intent received on app launch' + JSON.stringify(Intent));
},
function () {
console.log('Error getting cordova intent');
}
);
});
}
To receive the intent for when the app is already loaded:
ionViewDidEnter() {
this.platform.ready().then(() => {
(<any>window).plugins.intent.setNewIntentHandler(
function (Intent) {
console.log('new intent' + JSON.stringify(Intent) );
}
);
});
}
Fun fact: the "official" web intent plugin (darryncambell's) - which I couldn't get to work - credits Napolitano's plugin. Source
I eventually got darryncambell's plugin to work using the same methodology.
This question is a bit aged. But i had a more or less similar challenge. I had no luck with the WebIntent plugin and therefore i developed my own plugin for dealing with Intents on Android.
You may check this: https://github.com/napolitano/cordova-plugin-intent
While i'm still working on this plugin for my own projects, it's yet almost usable and documentation should be good enough to get a foot into the door.
Some background:
To allow third party apps to send content to your app, you must add an intent-filter to your MainActivity in the AndroidManifest.xml.
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
While you can do this manually, this has some disadvantages - so you probably want to have a hook or something similar. If you go to the repository above, you'll find and example for this.
Additionally to the intent-filter, you currently need an plugin that allows you a) to access the cordova intent (that's important to get access to sent content on application startup) and to receive notifications if the onNewIntent
event was triggered, which happens, if content is sent while your application is running.
That's exaclty what my plugin does. It gives you access to the cordova intent and allows you to handle the onNewIntent
event.
Examples:
window.plugins.intent.getCordovaIntent(function (Intent) {
console.log(Intent);
}, function () {
console.log('Error');
});
window.plugins.intent.setNewIntentHandler(function (Intent) {
console.log(Intent);
});
You will receive a limited intent object as result on success. This can be processed by your app.
Example:
{
"action": "android.intent.action.SEND_MULTIPLE",
"clipItems": [
{
"uri": "file:///storage/emulated/0/Download/example-document.pdf"
},
{
"uri": "file:///storage/emulated/0/Download/example-archive.zip"
}
],
"flags": 390070273,
"type": "*/*",
"component": "ComponentInfo{com.example.droid/com.example.droid.MainActivity}",
"extras": "Bundle[mParcelledData.dataSize=596]"
}
While this example actually shows JSON, you will receive a ready-to-use object.
Please note that i develop the plugin currently only for my own needs. However it may work also for you. Currently no angular examples were added to the README, but i think that should not be a big problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With