2020-02-09 18:29:46 +02:00
|
|
|
import PushNotificationIOS from '@react-native-community/push-notification-ios';
|
2017-11-28 21:36:47 +02:00
|
|
|
|
|
|
|
class AlarmServiceDriver {
|
|
|
|
constructor() {
|
|
|
|
this.hasPermission_ = null;
|
2017-11-28 22:17:34 +02:00
|
|
|
this.inAppNotificationHandler_ = null;
|
|
|
|
|
2020-05-20 18:16:43 +02:00
|
|
|
PushNotificationIOS.addEventListener('localNotification', (instance) => {
|
2020-02-09 18:29:46 +02:00
|
|
|
if (!this.inAppNotificationHandler_) return;
|
2017-11-28 22:17:34 +02:00
|
|
|
|
2020-02-09 18:29:46 +02:00
|
|
|
if (!instance || !instance._data || !instance._data.id) {
|
|
|
|
console.warn('PushNotificationIOS.addEventListener: Did not receive a proper notification instance');
|
|
|
|
return;
|
|
|
|
}
|
2017-11-28 22:17:34 +02:00
|
|
|
|
2020-02-09 18:29:46 +02:00
|
|
|
const id = instance._data.id;
|
|
|
|
this.inAppNotificationHandler_(id);
|
|
|
|
});
|
2017-11-28 21:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hasPersistentNotifications() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
notificationIsSet() {
|
2019-07-29 15:43:53 +02:00
|
|
|
throw new Error('Available only for non-persistent alarms');
|
2017-11-28 21:36:47 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 22:17:34 +02:00
|
|
|
setInAppNotificationHandler(v) {
|
|
|
|
this.inAppNotificationHandler_ = v;
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:36:47 +02:00
|
|
|
async hasPermissions(perm = null) {
|
|
|
|
if (perm !== null) return perm.alert && perm.badge && perm.sound;
|
|
|
|
|
|
|
|
if (this.hasPermission_ !== null) return this.hasPermission_;
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
return new Promise((resolve) => {
|
2020-05-20 18:16:43 +02:00
|
|
|
PushNotificationIOS.checkPermissions(async (perm) => {
|
2020-02-09 18:29:46 +02:00
|
|
|
const ok = await this.hasPermissions(perm);
|
|
|
|
this.hasPermission_ = ok;
|
|
|
|
resolve(ok);
|
|
|
|
});
|
2017-11-28 21:36:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async requestPermissions() {
|
2020-02-09 18:29:46 +02:00
|
|
|
const newPerm = await PushNotificationIOS.requestPermissions({
|
|
|
|
alert: 1,
|
|
|
|
badge: 1,
|
|
|
|
sound: 1,
|
|
|
|
});
|
|
|
|
this.hasPermission_ = null;
|
|
|
|
return this.hasPermissions(newPerm);
|
2017-11-28 21:36:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-09 18:29:46 +02:00
|
|
|
async clearNotification(id) {
|
|
|
|
PushNotificationIOS.cancelLocalNotifications({ id: `${id}` });
|
2017-11-28 21:36:47 +02:00
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2017-11-28 21:36:47 +02:00
|
|
|
async scheduleNotification(notification) {
|
|
|
|
if (!(await this.hasPermissions())) {
|
|
|
|
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 = {
|
2019-09-19 23:51:18 +02:00
|
|
|
id: `${notification.id}`,
|
2017-11-28 21:36:47 +02:00
|
|
|
alertTitle: notification.title,
|
2020-02-09 18:29:46 +02:00
|
|
|
fireDate: notification.date.toISOString(),
|
2019-09-19 23:51:18 +02:00
|
|
|
userInfo: { id: `${notification.id}` },
|
2017-11-28 21:36:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if ('body' in notification) iosNotification.alertBody = notification.body;
|
|
|
|
|
2020-02-09 18:29:46 +02:00
|
|
|
PushNotificationIOS.scheduleLocalNotification(iosNotification);
|
2017-11-28 21:36:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = AlarmServiceDriver;
|