2020-11-07 17:59:37 +02:00
|
|
|
import { Notification } from '@joplin/lib/models/Alarm';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2021-01-23 17:51:19 +02:00
|
|
|
const PushNotificationIOS = require('@react-native-community/push-notification-ios').default;
|
2017-11-28 21:36:47 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export default class AlarmServiceDriver {
|
2017-11-28 22:17:34 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private hasPermission_: boolean = null;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
private inAppNotificationHandler_: any = null;
|
|
|
|
private logger_: Logger;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor(logger: Logger) {
|
2020-10-16 17:26:19 +02:00
|
|
|
this.logger_ = logger;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
PushNotificationIOS.addEventListener('localNotification', (instance: any) => {
|
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) {
|
2020-10-16 17:26:19 +02:00
|
|
|
this.logger_.warn('PushNotificationIOS.addEventListener: Did not receive a proper notification instance');
|
2020-02-09 18:29:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public hasPersistentNotifications() {
|
2017-11-28 21:36:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public setInAppNotificationHandler(v: any) {
|
2017-11-28 22:17:34 +02:00
|
|
|
this.inAppNotificationHandler_ = v;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public async hasPermissions(perm: any = null) {
|
2017-11-28 21:36:47 +02:00
|
|
|
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) => {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
PushNotificationIOS.checkPermissions(async (perm: any) => {
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async requestPermissions() {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const options: any = {
|
2020-02-09 18:29:46 +02:00
|
|
|
alert: 1,
|
|
|
|
badge: 1,
|
|
|
|
sound: 1,
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
|
|
|
const newPerm = await PushNotificationIOS.requestPermissions(options);
|
2020-02-09 18:29:46 +02:00
|
|
|
this.hasPermission_ = null;
|
|
|
|
return this.hasPermissions(newPerm);
|
2017-11-28 21:36:47 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async clearNotification(id: number) {
|
2020-02-09 18:29:46 +02:00
|
|
|
PushNotificationIOS.cancelLocalNotifications({ id: `${id}` });
|
2017-11-28 21:36:47 +02:00
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async scheduleNotification(notification: Notification) {
|
2017-11-28 21:36:47 +02:00
|
|
|
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
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const iosNotification: any = {
|
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
|
|
|
}
|
|
|
|
}
|