1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/ReactNativeClient/lib/services/AlarmServiceDriverNode.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

const notifier = require('node-notifier');
class AlarmServiceDriverNode {
constructor(options) {
// Note: appName is required to get the notification to work. It must be the same as the appId defined in package.json
// https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058
this.appName_ = options.appName;
this.notifications_ = {};
}
hasPersistentNotifications() {
return false;
}
notificationIsSet(id) {
return id in this.notifications_;
}
async clearNotification(id) {
if (!this.notificationIsSet(id)) return;
clearTimeout(this.notifications_[id].timeoutId);
delete this.notifications_[id];
}
2019-07-29 15:43:53 +02:00
async scheduleNotification(notification) {
const now = Date.now();
const interval = notification.date.getTime() - now;
if (interval < 0) return;
if (isNaN(interval)) {
throw new Error('Trying to create a notification from an invalid object: ' + JSON.stringify(notification));
}
const timeoutId = setTimeout(() => {
const o = {
appName: this.appName_,
title: notification.title,
};
if ('body' in notification) o.message = notification.body;
notifier.notify(o);
this.clearNotification(notification.id);
}, interval);
this.notifications_[notification.id] = Object.assign({}, notification);
this.notifications_[notification.id].timeoutId = timeoutId;
}
}
2019-07-29 15:43:53 +02:00
module.exports = AlarmServiceDriverNode;