Skip to content

Instantly share code, notes, and snippets.

@Sampath-Lokuge
Created March 30, 2018 14:55
Show Gist options
  • Save Sampath-Lokuge/dcb2979ca02c38a7bfedbc2acc07203d to your computer and use it in GitHub Desktop.
Save Sampath-Lokuge/dcb2979ca02c38a7bfedbc2acc07203d to your computer and use it in GitHub Desktop.
async scheduleNotification(data: Transaction) {
let notifications: any[] = [];
notifications = await this.storage.get(LocalStorage.NOTIFICATIONS);
let firstNotificationTime = new Date(data.dueOn);
firstNotificationTime.setDate(firstNotificationTime.getDate());
firstNotificationTime.setHours(20);
firstNotificationTime.setMinutes(23);
firstNotificationTime.setSeconds(0);
const id = random(1, 1000);
let notification = {
id: id,
title: 'Due Today',
text: `Payment for ${data.category.name}: ${data.description} of ${Number(data.totalPrice).toLocaleString()} is due today.`,
data: { transaction: data },
at: firstNotificationTime,
}
if (notifications == null) notifications = [];
notifications.push(notification);
this.storage.set(LocalStorage.NOTIFICATIONS, notifications);
this.scheduleNotifications(notifications);
}
scheduleNotifications(notifications: any[]) {
cordova.plugins.notification.local.clearAll((res) => {
let notificationsDesc = notifications.map((notification) => {
return {
id: notification.id,
title: notification.title,
text: notification.text,
data: notification.data,
trigger: { at: notification.at }
}
});
cordova.plugins.notification.local.schedule(notificationsDesc);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment