import Logger from '@joplin/lib/Logger' ; import { Notification } from '@joplin/lib/models/Alarm' ; const ReactNativeAN = require ( 'joplin-rn-alarm-notification' ) . default ; export default class AlarmServiceDriver { private logger_ : Logger ; constructor ( logger ) { this . logger_ = logger ; } public hasPersistentNotifications ( ) { return true ; } public notificationIsSet ( ) { throw new Error ( 'Available only for non-persistent alarms' ) ; } public async clearNotification ( id ) { const alarm = await this . alarmByJoplinNotificationId ( id ) ; if ( ! alarm ) return ; this . logger_ . info ( 'AlarmServiceDriver: Deleting alarm:' , alarm ) ; await ReactNativeAN . deleteAlarm ( alarm . id ) ; } private alarmJoplinAlarmId ( alarm ) { if ( ! alarm . data || ! alarm . data . joplinNotificationId ) { return - 1 ; } else { return alarm . data . joplinNotificationId ; } } private async alarmByJoplinNotificationId ( joplinNotificationId ) { const alarms = 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 ) { const alarmNotifData = { title : notification . title , message : notification . body ? notification . body : '-' , channel : 'net.cozic.joplin.notification' , small_icon : 'ic_launcher_foreground' , color : 'blue' , data : { joplinNotificationId : notification . id , noteId : notification . noteId , } , } ; const fireDate = ReactNativeAN . parseDate ( notification . date ) ; const alarm = await ReactNativeAN . scheduleAlarm ( { ... alarmNotifData , fire_date : fireDate } ) ; this . logger_ . info ( 'AlarmServiceDriver: Created new alarm:' , alarm ) ; } }