2018-12-28 21:38:40 +02:00
|
|
|
// Note: currently, if Play Services aren't available, notifications will not work at all
|
|
|
|
// There won't be any warning or error message.
|
|
|
|
|
2018-12-16 15:11:45 +02:00
|
|
|
import firebase from 'react-native-firebase';
|
2017-11-28 00:50:46 +02:00
|
|
|
|
|
|
|
class AlarmServiceDriver {
|
|
|
|
|
2018-12-20 15:52:56 +02:00
|
|
|
constructor() {
|
2018-12-28 21:38:40 +02:00
|
|
|
this.playServiceAvailable_ = firebase.utils().playServicesAvailability.isAvailable;
|
|
|
|
if (!this.playServiceAvailable_) return;
|
|
|
|
|
2018-12-20 15:52:56 +02:00
|
|
|
this.channel_ = new firebase.notifications.Android.Channel('net.cozic.joplin.notification', 'Joplin Alarm',firebase.notifications.Android.Importance.Max)
|
|
|
|
.setDescription('Displays a notification for alarms associated with to-dos.');
|
|
|
|
firebase.notifications().android.createChannel(this.channel_);
|
|
|
|
}
|
|
|
|
|
2017-11-28 02:22:38 +02:00
|
|
|
hasPersistentNotifications() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationIsSet(alarmId) {
|
|
|
|
throw new Error('Available only for non-persistent alarms');
|
|
|
|
}
|
|
|
|
|
2018-12-16 15:11:45 +02:00
|
|
|
firebaseNotificationId_(joplinNotificationId) {
|
|
|
|
return 'net.cozic.joplin-' + joplinNotificationId;
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
async clearNotification(id) {
|
2018-12-28 21:38:40 +02:00
|
|
|
if (!this.playServiceAvailable_) return;
|
|
|
|
|
2018-12-16 15:11:45 +02:00
|
|
|
return firebase.notifications().cancelNotification(this.firebaseNotificationId_(id))
|
2017-11-28 00:50:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async scheduleNotification(notification) {
|
2018-12-28 21:38:40 +02:00
|
|
|
if (!this.playServiceAvailable_) return;
|
|
|
|
|
2018-12-16 15:11:45 +02:00
|
|
|
const firebaseNotification = new firebase.notifications.Notification()
|
|
|
|
firebaseNotification.setNotificationId(this.firebaseNotificationId_(notification.id));
|
|
|
|
firebaseNotification.setTitle(notification.title)
|
|
|
|
if ('body' in notification) firebaseNotification.body = notification.body;
|
2018-12-20 15:52:56 +02:00
|
|
|
firebaseNotification.android.setChannelId('net.cozic.joplin.notification');
|
2018-12-16 15:11:45 +02:00
|
|
|
firebaseNotification.android.setSmallIcon('ic_stat_access_alarm');
|
|
|
|
|
2018-12-20 15:52:56 +02:00
|
|
|
await firebase.notifications().scheduleNotification(firebaseNotification, {
|
2018-12-16 15:11:45 +02:00
|
|
|
fireDate: notification.date.getTime(),
|
|
|
|
});
|
2017-11-28 00:50:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AlarmServiceDriver;
|