From 67a457b9c528e45ea761efe30d9dfa188cc488d1 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 28 Nov 2017 18:58:04 +0000 Subject: [PATCH] Android: Got notifications to work --- ReactNativeClient/lib/poor-man-intervals.js | 17 +++++++++ .../lib/services/AlarmService.js | 9 ++--- .../services/AlarmServiceDriver.android.js | 4 +- ReactNativeClient/root.js | 37 ++++++------------- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/ReactNativeClient/lib/poor-man-intervals.js b/ReactNativeClient/lib/poor-man-intervals.js index 2aeaebe07..7ce0cfd3b 100644 --- a/ReactNativeClient/lib/poor-man-intervals.js +++ b/ReactNativeClient/lib/poor-man-intervals.js @@ -15,6 +15,20 @@ class PoorManIntervals { return PoorManIntervals.intervalId_; } + static setTimeout(callback, interval) { + PoorManIntervals.intervalId_++; + + PoorManIntervals.intervals_.push({ + id: PoorManIntervals.intervalId_, + callback: callback, + interval: interval, + lastIntervalTime: time.unixMs(), + oneOff: true, + }); + + return PoorManIntervals.intervalId_; + } + static intervalById(id) { for (let i = 0; i < PoorManIntervals.intervals_.length; i++) { if (PoorManIntervals.intervals_[i].id == id) return PoorManIntervals.intervals_[id]; @@ -41,6 +55,9 @@ class PoorManIntervals { if (now - interval.lastIntervalTime >= interval.interval) { interval.lastIntervalTime = now; interval.callback(); + if (interval.oneOff) { + this.clearInterval(interval.id); + } } } diff --git a/ReactNativeClient/lib/services/AlarmService.js b/ReactNativeClient/lib/services/AlarmService.js index cff8c1024..287d26e8b 100644 --- a/ReactNativeClient/lib/services/AlarmService.js +++ b/ReactNativeClient/lib/services/AlarmService.js @@ -57,11 +57,13 @@ class AlarmService { let clearAlarm = false; const makeNotificationFromAlarm = (alarm) => { - return { + const output = { id: alarm.id, date: new Date(note.todo_due), - title: note.title, + title: note.title.substr(0,128), } + if (note.body) output.body = note.body.substr(0,512); + return output; } if (isDeleted || @@ -103,9 +105,6 @@ class AlarmService { alarm = await Alarm.byNoteId(note.id); const notification = makeNotificationFromAlarm(alarm); - - if (note.body) notification.body = note.body; - this.logger().info('Scheduling notification for note ' + note.id, notification); await driver.scheduleNotification(notification); } diff --git a/ReactNativeClient/lib/services/AlarmServiceDriver.android.js b/ReactNativeClient/lib/services/AlarmServiceDriver.android.js index 3bcc752ac..5012aad12 100644 --- a/ReactNativeClient/lib/services/AlarmServiceDriver.android.js +++ b/ReactNativeClient/lib/services/AlarmServiceDriver.android.js @@ -17,11 +17,11 @@ class AlarmServiceDriver { async scheduleNotification(notification) { const androidNotification = { id: notification.id, - message: notification.title.substr(0, 100), // No idea what the limits are for title and body but set something reasonable anyway + message: notification.title, // No idea what the limits are for title and body but set something reasonable anyway date: notification.date, }; - if ('body' in notification) androidNotification.body = notification.body.substr(0, 512); + if ('body' in notification) androidNotification.body = notification.body; PushNotification.localNotificationSchedule(androidNotification); } diff --git a/ReactNativeClient/root.js b/ReactNativeClient/root.js index 4c955a403..08edfeeb1 100644 --- a/ReactNativeClient/root.js +++ b/ReactNativeClient/root.js @@ -60,6 +60,10 @@ const generalMiddleware = store => next => async (action) => { if (!await reg.syncTarget().syncStarted()) reg.scheduleSync(); } + if (['EVENT_NOTE_ALARM_FIELD_CHANGE', 'NOTE_DELETE'].indexOf(action.type) >= 0) { + await AlarmService.updateNoteNotification(action.id, action.type === 'NOTE_DELETE'); + } + if (action.type == 'SETTING_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTING_UPDATE_ALL') { reg.setupRecurrentSync(); } @@ -369,32 +373,15 @@ async function initialize(dispatch, backButtonHandler) { reg.setupRecurrentSync(); - if (Setting.value('env') == 'dev') { - // reg.scheduleSync(); - } else { - reg.scheduleSync(); - } - - - - //reg.logger().info('Scheduling iOS notification'); - - // PushNotificationIOS.scheduleLocalNotification({ - // alertTitle: "From Joplin", - // alertBody : "Testing notification on iOS", - // fireDate: new Date(Date.now() + (10 * 1000)), - // }); - - - - // const r = PushNotification.localNotificationSchedule({ - // id: '222456', - // message: "My Notification Message", // (required) - // date: new Date(Date.now() + (10 * 1000)) // in 60 secs - // }); - - //PushNotification.cancelLocalNotifications({ id: '222456' }); + PoorManIntervals.setTimeout(() => { + AlarmService.garbageCollect(); + }, 1000 * 60 * 60); + reg.scheduleSync().then(() => { + // Wait for the first sync before updating the notifications, since synchronisation + // might change the notifications. + AlarmService.updateAllNotifications(); + }); reg.logger().info('Application initialized'); }