2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
2019-06-27 19:33:22 +02:00
|
|
|
import * as Permissions from 'expo-permissions';
|
2019-08-04 14:28:41 +02:00
|
|
|
import {Notifications} from 'expo';
|
2019-08-13 12:20:03 +02:00
|
|
|
import AsyncStorageManager from "./AsyncStorageManager";
|
2019-08-22 11:41:34 +02:00
|
|
|
import LocaleManager from "./LocaleManager";
|
2019-08-26 18:03:01 +02:00
|
|
|
import passwords from "../passwords";
|
2019-08-13 12:20:03 +02:00
|
|
|
|
|
|
|
const EXPO_TOKEN_SERVER = 'https://srv-falcon.etud.insa-toulouse.fr/~amicale_app/expo_notifications/save_token.php';
|
2019-06-27 19:33:22 +02:00
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Static class used to manage notifications sent to the user
|
|
|
|
*/
|
2019-06-27 19:33:22 +02:00
|
|
|
export default class NotificationsManager {
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Async function asking permission to send notifications to the user
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2019-06-27 19:33:22 +02:00
|
|
|
static async askPermissions() {
|
|
|
|
const {status: existingStatus} = await Permissions.getAsync(Permissions.NOTIFICATIONS);
|
|
|
|
let finalStatus = existingStatus;
|
|
|
|
if (existingStatus !== 'granted') {
|
|
|
|
const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
|
|
|
|
finalStatus = status;
|
|
|
|
}
|
|
|
|
return finalStatus === 'granted';
|
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Async function sending a notification without delay to the user
|
|
|
|
*
|
|
|
|
* @param title {String} Notification title
|
|
|
|
* @param body {String} Notification body text
|
|
|
|
* @returns {Promise<import("react").ReactText>} Notification Id
|
|
|
|
*/
|
2019-08-04 14:28:41 +02:00
|
|
|
static async sendNotificationImmediately(title: string, body: string) {
|
2019-06-27 19:33:22 +02:00
|
|
|
await NotificationsManager.askPermissions();
|
|
|
|
return await Notifications.presentLocalNotificationAsync({
|
|
|
|
title: title,
|
|
|
|
body: body,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Async function sending notification at the specified time
|
|
|
|
*
|
|
|
|
* @param title Notification title
|
|
|
|
* @param body Notification body text
|
|
|
|
* @param time Time at which we should send the notification
|
2019-08-04 14:28:41 +02:00
|
|
|
* @param data Data to send with the notification, used for listeners
|
2019-08-13 12:20:03 +02:00
|
|
|
* @param androidChannelID
|
2019-06-29 13:37:21 +02:00
|
|
|
* @returns {Promise<import("react").ReactText>} Notification Id
|
|
|
|
*/
|
2019-08-04 14:28:41 +02:00
|
|
|
static async scheduleNotification(title: string, body: string, time: number, data: Object, androidChannelID: string): Promise<string> {
|
2019-06-27 19:33:22 +02:00
|
|
|
await NotificationsManager.askPermissions();
|
2019-08-13 12:20:03 +02:00
|
|
|
let date = new Date();
|
|
|
|
date.setTime(time);
|
2019-06-27 19:33:22 +02:00
|
|
|
return Notifications.scheduleLocalNotificationAsync(
|
|
|
|
{
|
|
|
|
title: title,
|
|
|
|
body: body,
|
2019-08-04 14:28:41 +02:00
|
|
|
data: data,
|
|
|
|
ios: { // configuration for iOS.
|
|
|
|
sound: true
|
|
|
|
},
|
|
|
|
android: { // configuration for Android.
|
|
|
|
channelId: androidChannelID,
|
|
|
|
}
|
2019-06-27 19:33:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
time: time,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Async function used to cancel the notification of a specific ID
|
|
|
|
* @param notificationID {Number} The notification ID
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
static async cancelScheduledNotification(notificationID: number) {
|
|
|
|
await Notifications.cancelScheduledNotificationAsync(notificationID);
|
2019-06-27 19:33:22 +02:00
|
|
|
}
|
2019-08-13 12:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save expo token to allow sending notifications to this device.
|
|
|
|
* This token is unique for each device and won't change.
|
|
|
|
* It only needs to be fetched once, then it will be saved in storage.
|
|
|
|
*
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async initExpoToken() {
|
|
|
|
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
2019-08-20 19:15:24 +02:00
|
|
|
if (token === '') {
|
2019-08-26 12:49:26 +02:00
|
|
|
await NotificationsManager.askPermissions();
|
2019-08-13 12:20:03 +02:00
|
|
|
let expoToken = await Notifications.getExpoPushTokenAsync();
|
|
|
|
// Save token for instant use later on
|
|
|
|
AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, expoToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:49:26 +02:00
|
|
|
static async forceExpoTokenUpdate() {
|
|
|
|
await NotificationsManager.askPermissions();
|
|
|
|
let expoToken = await Notifications.getExpoPushTokenAsync();
|
|
|
|
// Save token for instant use later on
|
|
|
|
AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, expoToken);
|
|
|
|
}
|
|
|
|
|
2019-08-13 12:53:21 +02:00
|
|
|
static getMachineNotificationWatchlist(callback: Function) {
|
|
|
|
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
2019-09-12 23:23:36 +02:00
|
|
|
if (token !== '') {
|
|
|
|
let data = {
|
|
|
|
function: 'get_machine_watchlist',
|
|
|
|
password: passwords.expoNotifications,
|
|
|
|
token: token,
|
|
|
|
};
|
|
|
|
fetch(EXPO_TOKEN_SERVER, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: new Headers({
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
}),
|
|
|
|
body: JSON.stringify(data) // <-- Post parameters
|
2019-08-13 12:53:21 +02:00
|
|
|
})
|
2019-09-12 23:23:36 +02:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((responseJson) => {
|
|
|
|
callback(responseJson);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('Expo token not available');
|
|
|
|
}
|
|
|
|
|
2019-08-13 12:53:21 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:20:03 +02:00
|
|
|
/**
|
|
|
|
* Ask the server to enable/disable notifications for the specified machine
|
|
|
|
*
|
|
|
|
* @param machineID
|
|
|
|
* @param isEnabled
|
|
|
|
*/
|
|
|
|
static setupMachineNotification(machineID: string, isEnabled: boolean) {
|
|
|
|
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
2019-09-12 23:23:36 +02:00
|
|
|
if (token !== '') {
|
|
|
|
let data = {
|
|
|
|
function: 'setup_machine_notification',
|
|
|
|
password: passwords.expoNotifications,
|
|
|
|
locale: LocaleManager.getCurrentLocale(),
|
|
|
|
token: token,
|
|
|
|
machine_id: machineID,
|
|
|
|
enabled: isEnabled
|
|
|
|
};
|
|
|
|
fetch(EXPO_TOKEN_SERVER, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: new Headers({
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
}),
|
|
|
|
body: JSON.stringify(data) // <-- Post parameters
|
2019-08-13 12:20:03 +02:00
|
|
|
})
|
2019-09-12 23:23:36 +02:00
|
|
|
.then((response) => response.text())
|
|
|
|
.then((responseText) => {
|
|
|
|
console.log(responseText);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('Expo token not available');
|
|
|
|
}
|
2019-08-13 12:20:03 +02:00
|
|
|
}
|
2019-08-21 17:07:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the selected reminder time for notifications to the server
|
|
|
|
* @param time
|
|
|
|
*/
|
|
|
|
static setMachineReminderNotificationTime(time: number) {
|
|
|
|
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
2019-09-12 23:23:36 +02:00
|
|
|
if (token !== '') {
|
|
|
|
let data = {
|
|
|
|
function: 'set_machine_reminder',
|
|
|
|
password: passwords.expoNotifications,
|
|
|
|
token: token,
|
|
|
|
time: time,
|
|
|
|
};
|
|
|
|
fetch(EXPO_TOKEN_SERVER, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: new Headers({
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
}),
|
|
|
|
body: JSON.stringify(data) // <-- Post parameters
|
2019-08-21 17:07:27 +02:00
|
|
|
})
|
2019-09-12 23:23:36 +02:00
|
|
|
.then((response) => response.text())
|
|
|
|
.then((responseText) => {
|
|
|
|
console.log(responseText);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('Expo token not available');
|
|
|
|
}
|
2019-08-21 17:07:27 +02:00
|
|
|
}
|
2019-06-27 19:33:22 +02:00
|
|
|
}
|