Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Last active November 10, 2023 08:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/fe22e9cc031f53213900fcb8d010958f to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/fe22e9cc031f53213900fcb8d010958f to your computer and use it in GitHub Desktop.
// Local Notifications:
// https://www.npmjs.com/package/de.appplant.cordova.plugin.local-notification/v/0.8.5
// https://github.com/katzer/cordova-plugin-local-notifications/wiki - reference
// https://github.com/katzer/cordova-plugin-local-notifications - beware the ReadMe file. This is v0.9.0-beta
// Installation
// cordova plugin add de.appplant.cordova.plugin.local-notification
//Build (XCode 10 causes build issues for iOS so it needs the --buildFlag)
// cordova emulate ios --target="iPhone-8, 12.0" --buildFlag="-UseModernBuildSystem=0"
// Dialogs:
// https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-dialogs/index.html
let app = {
init: function () {
document.addEventListener("deviceready", app.ready);
},
ready: function () {
app.addListeners();
},
addListeners: function () {
document.querySelector("#add-btn").addEventListener("click", app.addNote);
cordova.plugins.notification.local.on("click", function (notification) {
navigator.notification.alert("clicked: " + notification.id);
//user has clicked on the popped up notification
console.log(notification.data);
});
cordova.plugins.notification.local.on("trigger", function (notification) {
//added to the notification center on the date to trigger it.
navigator.notification.alert("triggered: " + notification.id);
});
},
addNote: function (ev) {
let props = cordova.plugins.notification.local.getDefaults();
//console.log(props);
/**
* Notification Object Properties - use it as a reference later on
* id
* text
* title
* every
* at
* data
* sound
* badge
*/
let inOneMin = new Date();
inOneMin.setMinutes(inOneMin.getMinutes() + 1);
let id = new Date().getMilliseconds();
let noteOptions = {
id: id,
title: "This is the Title",
text: "Don't forget to do that thing.",
at: inOneMin,
badge: 1,
data: {
prop: "prop value",
num: 42
}
};
/**
* if(props.icon){
noteOptions.icon = './img/calendar-md-@2x.png'
}
if(props.led){
noteOptions.led = '#33CC00'
}
if(props.actions){
noteOptions.actions = [{ id: "yes", title: "Do it" }, { id: "no", title: "Nah" }]
}
**/
cordova.plugins.notification.local.schedule(noteOptions, function(note){
//this is the callback function for the schedule method
//this runs AFTER the notification has been scheduled.
});
navigator.notification.alert("Added notification id " + id);
cordova.plugins.notification.local.cancel(id, function () {
// will get rid of notification id 1 if it has NOT been triggered or added to the notification center
// cancelAll() will get rid of all of them
});
cordova.plugins.notification.local.clear(id, function () {
// will dismiss a notification that has been triggered or added to notification center
});
cordova.plugins.notification.local.isPresent(id, function (present) {
// navigator.notification.alert(present ? "present" : "not found");
// can also call isTriggered() or isScheduled()
// getAllIds(), getScheduledIds() and getTriggeredIds() will give you an array of ids
// get(), getAll(), getScheduled() and getTriggered() will get the notification based on an id
});
}
};
app.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment