@@ -5,6 +5,8 @@ import NativeAccountStorage from './NativeAccountStorage'
55import Account from '../Account'
66import { STATUS_ALLGOOD , STATUS_DISABLED , STATUS_ERROR , STATUS_SYNCING } from '../interfaces/Controller'
77import { initSharp } from '../sentry'
8+ import { LocalNotifications } from '@capacitor/local-notifications'
9+ import { i18n } from './I18n'
810
911const INACTIVITY_TIMEOUT = 1000 * 7
1012const MAX_BACKOFF_INTERVAL = 1000 * 60 * 60 // 1 hour
@@ -196,12 +198,50 @@ export default class NativeController {
196198 if ( account . getData ( ) . syncing ) {
197199 return
198200 }
201+ const startTime = Date . now ( )
199202 setTimeout ( ( ) => this . updateStatus ( ) , 500 )
203+ let notifications
204+ if ( ( await LocalNotifications . checkPermissions ( ) ) . display !== 'denied' ) {
205+ ( { notifications } = await LocalNotifications . schedule ( {
206+ notifications : [
207+ {
208+ id : Math . round ( ( Math . random ( ) * 2 - 1 ) * 2147483647 ) ,
209+ title : i18n . getMessage ( 'StatusSyncing' ) ,
210+ body : i18n . getMessage ( 'NotificationSyncingprofile' , [ account . getLabel ( ) ] ) ,
211+ ongoing : true ,
212+ smallIcon : 'notification_icon' ,
213+ largeIcon : 'notification_icon' ,
214+ }
215+ ]
216+ } ) )
217+ }
200218 try {
201219 await account . sync ( strategy , forceSync )
202220 } catch ( error ) {
203221 console . error ( error )
204222 }
223+ if ( ( await LocalNotifications . checkPermissions ( ) ) . display !== 'denied' ) {
224+ // Cancel the ongoing notification
225+ await LocalNotifications . cancel ( { notifications } )
226+ // eslint-disable-next-line no-constant-condition
227+ if ( Date . now ( ) - startTime > 60 * 1000 ) {
228+ await LocalNotifications . schedule ( {
229+ notifications : [
230+ {
231+ id : Math . round ( ( Math . random ( ) * 2 - 1 ) * 2147483647 ) ,
232+ title : account . getData ( ) . error
233+ ? i18n . getMessage ( 'StatusSyncingfailed' )
234+ : i18n . getMessage ( 'StatusSyncingcomplete' ) ,
235+ body : account . getData ( ) . error
236+ ? i18n . getMessage ( 'NotificationSyncingfailed' , [ account . getLabel ( ) ] )
237+ : i18n . getMessage ( 'NotificationSyncingsucceeded' , [ account . getLabel ( ) ] ) ,
238+ smallIcon : 'notification_icon' ,
239+ largeIcon : 'notification_icon' ,
240+ }
241+ ]
242+ } )
243+ }
244+ }
205245 this . updateStatus ( )
206246 }
207247
@@ -249,6 +289,10 @@ export default class NativeController {
249289 initSharp ( )
250290 }
251291
292+ if ( ( await LocalNotifications . checkPermissions ( ) ) . display !== 'denied' ) {
293+ LocalNotifications . requestPermissions ( )
294+ }
295+
252296 const accounts = await Account . getAllAccounts ( )
253297 await Promise . all (
254298 accounts . map ( async acc => {
0 commit comments