A bunch of local notification plugins for Cordova 3.x.x
by Sebastián Katzer (github.com/katzer)
-
iOS
See Local and Push Notification Programming Guide for detailed informations and screenshots. -
Android
See Notification Guide for detailed informations and screenshots. -
WP8
See Local notifications for Windows Phone for detailed informations and screenshots.
Windows Phone 8.0 has no notification center. Instead local notifications are realized through live tiles updates.
Cordova will check all dependencies and install them if they are missing.
- org.apache.cordova.device (since v0.6.0)
Through the Command-line Interface:
cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git
Through the Command-line Interface:
cordova plugin rm de.appplant.cordova.plugin.local-notifications
- [feature:] Sound can be specified under Android.
- [enhancement:] Adding notifications on Android does not block the ui thread anymore.
- Added WP8 support
Based on the LiveTiles WP8 plugin made by Jesse MacFadyen (purplecabbage) - [enhancement:] The
add()
function now returns the id of the created notification. - [feature:] Added new
title
property. - [bugfix:]
cancel
under iOS did not work do to wrong dict key. - [enhancement:] All notifications under Android display the app icon by default.
- [feature:] Icon can be specified under Android.
- Added Android support
Based on the LocalNotifications Android plugin made by Daniël (dvtoever)
- Added iOS support
Based on the LocalNotifications iOS plugin made by Rodrigo Moyle
The plugin creates the object window.plugin.notification.local
with the following methods:
The method allows to add a custom notification. It takes an hash as an argument to specify the notification's properties and returns the ID for the notification.
All properties are optional. If no date object is given, the notification will pop-up immediately.
window.plugin.notification.local.add({
id: String, // a unique id of the notifiction
date: Date, // this expects a date object
message: String, // the message that is displayed
title: String, // the title of the message
repeat: String, // has the options of daily', 'weekly',''monthly','yearly')
badge: Number, // displays number badge to notification
sound: String, // a sound to be played (iOS & Android)
foreground: String, // a javascript function to be called if the app is running
background: String, // a javascript function to be called if the app is in the background
});
The method cancels a notification which was previously added. It takes the ID of the notification as an argument.
window.plugin.notification.local.cancel(__id__);
The method cancels all notifications which were previously added by the application.
window.plugin.notification.local.cancelAll();
var now = new Date().getTime(),
_60_seconds_from_now = new Date(now + 60*1000);
window.plugin.notification.local.add({
id: 1, // is converted to a string
date: _60_seconds_from_now,
message: 'Hello world!',
title: 'Check that out!',
repeat: 'weekly', // will fire every week on this day
foreground: 'foreground',
background: 'background'
});
function foreground (id) {
console.log('I WAS RUNNING ID='+id)
}
function background (id) {
console.log('I WAS IN THE BACKGROUND ID='+id)
}
By default all notifications will display the app icon. But an specific icon can be defined through the icon
property.
/**
* Displays the <package.name>.R.drawable.ic_launcher icon
*/
window.plugin.notification.local.add({ icon: 'ic_launcher' });
/**
* Displays the android.R.drawable.ic_dialog_email icon
*/
window.plugin.notification.local.add({ icon: 'ic_dialog_email' });
The default sound is RingtoneManager.TYPE_NOTIFICATION
. But an specific sound can be defined through the sound
property.
The sound must be a absolute or relative Uri pointing to the sound file.
/**
* Plays the sound if the notification pop's up.
*/
window.plugin.notification.local.add({ sound: 'res/sounds/beep.mp3' });
/**
* Plays the `RingtoneManager.TYPE_ALARM` sound.
*/
window.plugin.notification.local.add({ sound: 'TYPE_ALARM' });
/**
* Plays no sound if the notification pop's up.
*/
window.plugin.notification.local.add({ sound: null });
The sound must be located in your project's resources and must be a caf file.
/**
* Plays the sound if the notification pop's up.
*/
window.plugin.notification.local.add({ sound: 'sub.caf' });
Note: The right to play notification sounds in the notification center settings has to be granted.
LiveTile's have the ability to display images for different sizes. These images can be defined through the smallImage
, image
and wideImage
properties.
An image must be defined as a relative or absolute URI.
/**
* Displays the application icon as the livetile's background image
*/
window.plugin.notification.local.add({ image: 'appdata:ApplicationIcon.png' })
All images can be restored to the default ones by canceling the notification.
The right to play notification sounds in the notification center settings has to be granted.
An application can only display one notification at a time. Each time a new notification has to be added, the application live tile's data will be overwritten by the new ones.
The methods cancel
and cancelAll
have the same effect.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request