import { Notification } from '@joplin/lib/models/Alarm' ; import Logger from '@joplin/lib/Logger' ; const PushNotificationIOS = require ( '@react-native-community/push-notification-ios' ) . default ; export default class AlarmServiceDriver { private hasPermission_ : boolean = null ; private inAppNotificationHandler_ : any = null ; private logger_ : Logger ; constructor ( logger ) { this . logger_ = logger ; PushNotificationIOS . addEventListener ( 'localNotification' , ( instance ) => { if ( ! this . inAppNotificationHandler_ ) return ; if ( ! instance || ! instance . _data || ! instance . _data . id ) { this . logger_ . warn ( 'PushNotificationIOS.addEventListener: Did not receive a proper notification instance' ) ; return ; } const id = instance . _data . id ; this . inAppNotificationHandler_ ( id ) ; } ) ; } hasPersistentNotifications ( ) { return true ; } notificationIsSet ( ) { throw new Error ( 'Available only for non-persistent alarms' ) ; } setInAppNotificationHandler ( v ) { this . inAppNotificationHandler_ = v ; } 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 ) => { PushNotificationIOS . checkPermissions ( async ( perm ) => { const ok = await this . hasPermissions ( perm ) ; this . hasPermission_ = ok ; resolve ( ok ) ; } ) ; } ) ; } async requestPermissions ( ) { const options = { alert : 1 , badge : 1 , sound : 1 , } ; const newPerm = await PushNotificationIOS . requestPermissions ( options ) ; this . hasPermission_ = null ; return this . hasPermissions ( newPerm ) ; } async clearNotification ( id ) { PushNotificationIOS . cancelLocalNotifications ( { id : `${ id }` } ) ; } async scheduleNotification ( notification ) { if ( ! ( await this . hasPermissions ( ) ) ) { const ok = await this . requestPermissions ( ) ; if ( ! ok ) return ; } const iosNotification = { id : `${ notification . id }` , alertTitle : notification . title , fireDate : notification . date . toISOString ( ) , userInfo : { id : `${ notification . id }` } , } ; if ( 'body' in notification ) iosNotification . alertBody = notification . body ; PushNotificationIOS . scheduleLocalNotification ( iosNotification ) ; } }