Cordova Plugin for accessing the Cordova Intent and handling onNewIntent (Android Only)
This plugin allows you to add functionality for receiving content sent from other apps. To enable receiving sent content add the following XML to the MainActivity section of your AndroidManifest.xml
You can adjust the mime types your application accepts by defining them explicitly. The above example allows any mime type.
The following example limits allowed mime types to JPG-Images.
If you do not want to handle multiple files at once, you can remove the following tag:
It is recommended to use a hook or the custom config plugin to ensure that the above XML will automatically added in case you want to have a fresh checkout or remove/add the platform.
A very quick and much more dirty example hook (tested on Mac OS-X, requires Ruby):
#!/usr/bin/env ruby replace=" </intent-filter> <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='*/*' /> " filename = "platforms/android/AndroidManifest.xml"outdata = File.read(filename).gsub(/<category android\:name=\"android\.intent\.category\.LAUNCHER\" \/>([^<]*)<\/intent-filter>([^<]*)<\/activity>/, "<category android\:name=\"android\.intent\.category\.LAUNCHER\" \/><\/intent-filter><\/activity>") File.open(filename, 'w') do |out| out << outdataend
Note
By default the launch mode of the MainActivity of cordova based applications is set to "singleTop". This is ok for most situations. However you may prefer having the launch mode set to "singleTask" instead. Please read this article to get an idea about the different launch modes: https://www.mobomo.com/2011/06/android-understanding-activity-launchmode/
Setting the launch mode to "singleTask" ensures that your app cannot run in multiple instances as it might happen if launch mode is set to "singleTop" for example if your application is already running and you try to share a webpage from the browser to it.
Example of MainActivity section in AndroidManifest.xml
Installation
Add the plugin to your project using Cordova CLI:
cordova plugin add https://github.com/napolitano/cordova-plugin-intent
Or using PhoneGap CLI:
phonegap local plugin add https://github.com/napolitano/cordova-plugin-intent
Usage
windowpluginsintent;
windowpluginsintent;
Example Intent passed from plugin
While this example shows an JSON representation, you'll actually receive a ready-to-use object (of course with the attributes shown above).
Methods
getCordovaIntent() - Android
Get limited access to intent properties
Events
setNewIntentHandler(method) - Android
Method passed will be triggered on new intent. Provides limited access to the new intent.
Supported Platforms
- Android (>= API Level 19 / Kitkat)
Basic example
Get cordova intent
document
Handle new intent
document
Limitations
The plugin is a bare-bones implementation to help me with my projects. The intent passed to JavaScript is not a complete serialized object. The code should receive some refactorings and would benefit from a better JSON library like gson or similar. Test would be nice too.
Example App
An example app for Android (made with cordova 5.4.1) was added to the project. The app shows basic usage of the plugin.
iOS
On iOS the method "window.handleOpenURL" is your friend. This method will be triggered if someone uses "Open in" to share content to your app.
A basic implementation could look like:
window { window};
In order to get this working and make your app a "Open in"-Target on iOS, you have to add some XML to your info.plist
<!-- whatever else --> UIFileSharingEnabled CFBundleDocumentTypes CFBundleTypeIconFiles icon-small CFBundleTypeName ${PRODUCT_NAME} CFBundleTypeRole Viewer LSHandlerRank Alternate LSItemContentTypes public.data public.text public.image public.audio public.audiovisual-content public.xml public.movie public.font com.adobe.postscript com.adobe.pdf org.gnu.gnu-zip-archve