1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-03 22:37:35 +02:00
joplin/ReactNativeClient/lib/services/AlarmServiceDriver.ios.js

82 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-03-09 17:49:35 +00:00
const { PushNotificationIOS } = require("react-native");
2017-11-28 19:36:47 +00:00
class AlarmServiceDriver {
constructor() {
this.hasPermission_ = null;
2017-11-28 20:17:34 +00:00
this.inAppNotificationHandler_ = null;
2018-03-09 17:49:35 +00:00
PushNotificationIOS.addEventListener("localNotification", instance => {
2017-11-28 20:17:34 +00:00
if (!this.inAppNotificationHandler_) return;
if (!instance || !instance._data || !instance._data.id) {
2018-03-09 17:49:35 +00:00
console.warn("PushNotificationIOS.addEventListener: Did not receive a proper notification instance");
2017-11-28 20:17:34 +00:00
return;
}
const id = instance._data.id;
this.inAppNotificationHandler_(id);
});
2017-11-28 19:36:47 +00:00
}
hasPersistentNotifications() {
return true;
}
notificationIsSet(alarmId) {
2018-03-09 17:49:35 +00:00
throw new Error("Available only for non-persistent alarms");
2017-11-28 19:36:47 +00:00
}
2017-11-28 20:17:34 +00:00
setInAppNotificationHandler(v) {
this.inAppNotificationHandler_ = v;
}
2017-11-28 19:36:47 +00:00
async hasPermissions(perm = null) {
if (perm !== null) return perm.alert && perm.badge && perm.sound;
if (this.hasPermission_ !== null) return this.hasPermission_;
return new Promise((resolve, reject) => {
2018-03-09 17:49:35 +00:00
PushNotificationIOS.checkPermissions(async perm => {
2017-11-28 19:36:47 +00:00
const ok = await this.hasPermissions(perm);
this.hasPermission_ = ok;
resolve(ok);
});
});
}
async requestPermissions() {
const newPerm = await PushNotificationIOS.requestPermissions({
2018-03-09 17:49:35 +00:00
alert: 1,
badge: 1,
sound: 1,
2017-11-28 19:36:47 +00:00
});
this.hasPermission_ = null;
return this.hasPermissions(newPerm);
}
async clearNotification(id) {
2018-03-09 17:49:35 +00:00
PushNotificationIOS.cancelLocalNotifications({ id: id + "" });
2017-11-28 19:36:47 +00:00
}
2018-03-09 17:49:35 +00:00
2017-11-28 19:36:47 +00:00
async scheduleNotification(notification) {
2018-03-09 17:49:35 +00:00
if (!await this.hasPermissions()) {
2017-11-28 19:36:47 +00:00
const ok = await this.requestPermissions();
if (!ok) return;
}
// ID must be a string and userInfo must be supplied otherwise cancel won't work
const iosNotification = {
2018-03-09 17:49:35 +00:00
id: notification.id + "",
2017-11-28 19:36:47 +00:00
alertTitle: notification.title,
fireDate: notification.date,
2018-03-09 17:49:35 +00:00
userInfo: { id: notification.id + "" },
2017-11-28 19:36:47 +00:00
};
2018-03-09 17:49:35 +00:00
if ("body" in notification) iosNotification.alertBody = notification.body;
2017-11-28 19:36:47 +00:00
PushNotificationIOS.scheduleLocalNotification(iosNotification);
}
}
2018-03-09 17:49:35 +00:00
module.exports = AlarmServiceDriver;