1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-26 18:58:21 +02:00

Android: Got notifications to work

This commit is contained in:
Laurent Cozic 2017-11-28 18:58:04 +00:00
parent 18dc6c826a
commit 67a457b9c5
4 changed files with 35 additions and 32 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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');
}