You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-30 23:44:55 +02:00
Added back support for alarms
This commit is contained in:
@ -1,48 +1,67 @@
|
||||
import Logger from 'lib/Logger';
|
||||
import { Notification } from 'lib/models/Alarm';
|
||||
|
||||
const PushNotification = require('react-native-push-notification');
|
||||
const ReactNativeAN = require('react-native-alarm-notification').default;
|
||||
|
||||
export default class AlarmServiceDriver {
|
||||
|
||||
private PushNotification_:any = null;
|
||||
private logger_:Logger;
|
||||
|
||||
PushNotificationHandler_() {
|
||||
if (!this.PushNotification_) {
|
||||
PushNotification.configure({
|
||||
// (required) Called when a remote or local notification is opened or received
|
||||
onNotification: function(notification:any) {
|
||||
console.info('Notification was opened: ', notification);
|
||||
// process the notification
|
||||
},
|
||||
popInitialNotification: true,
|
||||
requestPermissions: true,
|
||||
});
|
||||
|
||||
this.PushNotification_ = PushNotification;
|
||||
}
|
||||
|
||||
return this.PushNotification_;
|
||||
constructor(logger:Logger) {
|
||||
this.logger_ = logger;
|
||||
}
|
||||
|
||||
hasPersistentNotifications() {
|
||||
public hasPersistentNotifications() {
|
||||
return true;
|
||||
}
|
||||
|
||||
notificationIsSet() {
|
||||
public notificationIsSet() {
|
||||
throw new Error('Available only for non-persistent alarms');
|
||||
}
|
||||
|
||||
async clearNotification(id:any) {
|
||||
return this.PushNotificationHandler_().cancelLocalNotifications({ id: `${id}` });
|
||||
public async clearNotification(id:number) {
|
||||
const alarm = await this.alarmByJoplinNotificationId(id);
|
||||
if (!alarm) return;
|
||||
|
||||
this.logger_.info('AlarmServiceDriver: Deleting alarm:', alarm);
|
||||
|
||||
await ReactNativeAN.deleteAlarm(alarm.id);
|
||||
}
|
||||
|
||||
async scheduleNotification(notification:Notification) {
|
||||
const config = {
|
||||
id: `${notification.id}`,
|
||||
message: notification.title,
|
||||
date: notification.date,
|
||||
// Returns -1 if could not be found
|
||||
private alarmJoplinAlarmId(alarm:any):number {
|
||||
if (!alarm.data) return -1;
|
||||
const m = alarm.data.match(/joplinNotificationId==>(\d+)/)
|
||||
return m ? Number(m[1]) : -1;
|
||||
}
|
||||
|
||||
private async alarmByJoplinNotificationId(joplinNotificationId:number) {
|
||||
const alarms:any[] = await ReactNativeAN.getScheduledAlarms();
|
||||
for (const alarm of alarms) {
|
||||
const id = this.alarmJoplinAlarmId(alarm);
|
||||
if (id === joplinNotificationId) return alarm;
|
||||
}
|
||||
|
||||
this.logger_.warn('AlarmServiceDriver: Could not find alarm that matches Joplin notification ID. It could be because it has already been triggered:', joplinNotificationId);
|
||||
return null;
|
||||
}
|
||||
|
||||
public async scheduleNotification(notification:Notification) {
|
||||
const alarmNotifData = {
|
||||
title: notification.title,
|
||||
message: notification.body ? notification.body : '-', // Required
|
||||
channel: "net.cozic.joplin.notification",
|
||||
small_icon: "ic_launcher",
|
||||
color: 'white',
|
||||
data: { joplinNotificationId: notification.id + '' },
|
||||
};
|
||||
|
||||
this.PushNotificationHandler_().localNotificationSchedule(config);
|
||||
// ReactNativeAN expects a string as a date and it seems this utility
|
||||
// function converts it to the right format.
|
||||
const fireDate = ReactNativeAN.parseDate(notification.date);
|
||||
|
||||
const alarm = await ReactNativeAN.scheduleAlarm({ ...alarmNotifData, fire_date: fireDate });
|
||||
|
||||
this.logger_.info('AlarmServiceDriver: Created new alarm:', alarm);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user