Cordova plugin to access and modify the badge number of the app icon on various mobile platforms including iOS, Android and Windows Phone.
- iOS (including iOS8)
- Android (SDK >=11)
See Notification Guide for detailed informations and screenshots. - WP8
See WP8 Guide for detailed informations and screenshots.
The plugin can either be installed from git repository, from local file system through the Command-line Interface. Or cloud based through PhoneGap Build.
From master:
# ~~ from master branch ~~
cordova plugin add https://github.com/katzer/cordova-plugin-badge.git
from a local folder:
# ~~ local folder ~~
cordova plugin add de.appplant.cordova.plugin.badge --searchpath path/to/plugin
or to use the last stable version:
# ~~ stable version ~~
cordova plugin add de.appplant.cordova.plugin.badge@0.6.1
Add the following xml to your config.xml to always use the latest version of this plugin:
<gap:plugin name="de.appplant.cordova.plugin.badge" version="0.6.1" />
More informations can be found here.
- [bugfix:]
hasPermission
andpromptForPermission
let the app crash on iOS7 and older.
- [enhancement:] iOS 8 support
- [enhancement:] All methods are now asynchron and do not block the main thread anymore.
- [feature:] New method
hasPermission
to ask if the user has granted to display badge notifications. - [feature:] New method
promptForPermission
to promt the user to grant permission to display badge notifications. - [feature:] New method
configure
to configure badge properties. - [feature:] The small icon on Android can be changed through
configure
. - [change:] The namespace
plugin.notification.badge
will be removed with v0.6.1 - [change:]
setTitle
is deprecated, please useconfigure({ title: 'title' })
. - [change:]
clearOnTap
is deprecated, please useconfigure({ autoClear: true })
. - [bugfix:]
getBadge
still returned the number when autoClear: was set and the notification was already cleared by the system (Android). - [bugfix:]
clean
was not working on Windows Phone.
- See CHANGELOG.md to get the full changelog for the plugin.
The plugin creates the object cordova.plugins.notification.badge
with the following methods:
- notification.badge.hasPermission
- notification.badge.promptForPermission
- notification.badge.set
- notification.badge.get
- notification.badge.clear
- notification.badge.configure
Note: The previous namespace plugin.notification.badge
will be removed with v0.6.1
The plugin and its methods are not available before the deviceready event has been fired.
document.addEventListener('deviceready', function () {
// cordova.plugins.notification.badge is now available
}, false);
If the permission has been granted through the user can be retrieved through the notification.badge.hasPermission
interface.
The method takes a callback function as its argument which will be called with a boolean value. Optional the scope of the callback function ca be defined through a second argument.
- The method is supported on each platform, however its only relevant for iOS8 and above.
cordova.plugins.notification.badge.hasPermission(function (granted) {
// console.log('Permission has been granted: ' + granted);
});
The user can be prompted to grant the required permission through the notification.badge.promptForPermission
interface.
- The method is supported on each platform, however its only relevant for iOS8 and above.
- The user will only get a prompt dialog for the first time. Later its only possible to change the setting via the notification center.
cordova.plugins.notification.badge.promptForPermission();
The badge number can be set through the notification.badge.set
interface.
The method takes the badge as its argument. It needs to be a number or a string which can be parsed to a number.
- The badge number can only be set if the user has previously granted the required permission.
- On Android the badge will be displayed through a notification. See configure how to specify a custom notification title.
- On Windows Phone 8 the badge will be displayed through the app's live tile.
- See get how to get back the current badge number.
- See clear of how to clear the badge number.
- See the examples of how to use the plugin.
cordova.plugins.notification.badge.set(Number);
The badge number can be accessed through the notification.badge.get
interface.
The method takes a callback function as its argument which will be called with the badge number. Optional the scope of the callback function ca be defined through a second argument.
cordova.plugins.notification.badge.get(function (badge) {
// console.log('badge number: ' + badge);
}, scope);
The badge number can be removed through the notification.badge.clear
interface.
- Clearing the badge number is equivalent to set a zero number.
- See configure how to clear the badge automatically after the user has taped the app icon.
- See set of how to set the badge number.
cordova.plugins.notification.badge.clear();
The badge number can be cleared automatically after the user has taped the app icon. The default value is false.
cordova.plugins.notification.badge.configure({ autoClear: Boolean });
Please see the Example branch for more examples and a running example app.
The following example shows how to set the badge number to 1.
cordova.plugins.notification.badge.set(1);
Note: The badge number can only be set if the user has granted the required permission.
See below how to clear the badge number.
cordova.plugins.notification.badge.clear();
The code below tells the plugin to clear the badge each time the user taps the app icon.
cordova.plugins.notification.badge.configure({ autoClear: true });
The default format for the title is %d new messages
, but is customizable through configure
.
cordova.plugins.notification.badge.configure({ title: '%d neue Meldungen' });
As default the email icon is used, but is customizable through configure
.
cordova.plugins.notification.badge.configure({ smallIcon: 'icon' });
Note: A small icon is required.
Please note that the plugin as well as cordova need to be compiled for each iOS version. The compiled iOS8 code will crash for example under iOS7.
- 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
This software is released under the Apache 2.0 License.
© 2013-2014 appPlant UG, Inc. All rights reserved