Friday, January 2, 2015

Use Notifications in Cordova

If you want to add notification function in your cordova app local notificaiton plugin (the de.appplant.cordova.plugin.local-notification) is good solution.

https://github.com/katzer/cordova-plugin-local-notifications/

1. Add plugin.
cordova plugin add de.appplant.cordova.plugin.local-notification && cordova prepare

remove plugin.
cordova plugin rm de.appplant.cordova.plugin.local-notification

2. Update config.xml
Add this line to config.xml

<plugin name="local-notification" 
value="de.appplant.cordova.plugin.local-notification" />

3. Add simple notification in index.html.

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
      window.plugin.notification.local.add({ message: 'Great app!' });
}

4. Scheduled local notification.
If you want to get an alarm at the same time weekly...

var now = new Date().getTime(),
_60_seconds_from_now = new Date(now + 60*1000);

window.plugin.notification.local.add({
    id:      888,
    title:   'Reminder',
    message: 'Dont forget to buy some flowers.',
    repeat:  'weekly',
    autoCancel: true,
    sound:'TYPE_ALARM',
    date:    _60_seconds_from_now
 });

5. Add event function
window.plugin.notification.local.ontrigger = function(id, state, json){
// do something... 
}
window.plugin.notification.local.onadd = function(id, state, json){
// do something... 
}
window.plugin.notification.local.onclick = function(id, state, json){
// do something...
}

Useful  but these don't work well if the app is not on.




No comments:

Post a Comment