Added notification vibrations and remove machine from watch list on last notification

This commit is contained in:
keplyx 2019-08-04 14:28:41 +02:00
parent ed597bfa8a
commit fbe687ca98
3 changed files with 47 additions and 12 deletions

View file

@ -9,7 +9,7 @@
"android", "android",
"web" "web"
], ],
"version": "0.0.1", "version": "0.0.2",
"orientation": "portrait", "orientation": "portrait",
"icon": "./assets/icon.png", "icon": "./assets/icon.png",
"primaryColor": "#e42612", "primaryColor": "#e42612",

View file

@ -1,7 +1,7 @@
// @flow // @flow
import * as React from 'react'; import * as React from 'react';
import {Alert, View} from 'react-native'; import {Alert, View, Platform} from 'react-native';
import {Body, Card, CardItem, Left, Right, Text} from 'native-base'; import {Body, Card, CardItem, Left, Right, Text} from 'native-base';
import ThemeManager from '../utils/ThemeManager'; import ThemeManager from '../utils/ThemeManager';
import i18n from "i18n-js"; import i18n from "i18n-js";
@ -10,6 +10,7 @@ import FetchedDataSectionList from "../components/FetchedDataSectionList";
import NotificationsManager from "../utils/NotificationsManager"; import NotificationsManager from "../utils/NotificationsManager";
import PlatformTouchable from "react-native-platform-touchable"; import PlatformTouchable from "react-native-platform-touchable";
import AsyncStorageManager from "../utils/AsyncStorageManager"; import AsyncStorageManager from "../utils/AsyncStorageManager";
import * as Expo from "expo";
const DATA_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/dataProxiwash.json"; const DATA_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/dataProxiwash.json";
@ -76,6 +77,24 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
}; };
} }
componentDidMount() {
if (Platform.OS === 'android') {
Expo.Notifications.createChannelAndroidAsync('reminders', {
name: 'Reminders',
priority: 'max',
vibrate: [0, 250, 250, 250],
});
}
// Remove machine from watch list when receiving last notification
Expo.Notifications.addListener((notification) => {
if (notification.data !== undefined) {
if (this.isMachineWatched(notification.data.id) && notification.data.isMachineFinished === true) {
this.removeNotificationFromPrefs(this.getMachineIndexInWatchList(notification.data.id));
}
}
});
}
getHeaderTranslation() { getHeaderTranslation() {
return i18n.t("screens.proxiwash"); return i18n.t("screens.proxiwash");
} }
@ -128,7 +147,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
let endNotificationID = await NotificationsManager.scheduleNotification( let endNotificationID = await NotificationsManager.scheduleNotification(
i18n.t('proxiwashScreen.notifications.machineFinishedTitle'), i18n.t('proxiwashScreen.notifications.machineFinishedTitle'),
i18n.t('proxiwashScreen.notifications.machineFinishedBody', {number: machineId}), i18n.t('proxiwashScreen.notifications.machineFinishedBody', {number: machineId}),
new Date().getTime() + remainingTime * (60 * 1000) // Convert back to milliseconds new Date().getTime() + remainingTime * (60 * 1000), // Convert back to milliseconds
{id: machineId, isMachineFinished: true},
'reminders'
); );
let reminderNotificationID = await ProxiwashScreen.setupReminderNotification(machineId, remainingTime); let reminderNotificationID = await ProxiwashScreen.setupReminderNotification(machineId, remainingTime);
this.saveNotificationToPrefs(machineId, endNotificationID, reminderNotificationID); this.saveNotificationToPrefs(machineId, endNotificationID, reminderNotificationID);
@ -143,7 +164,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
reminderNotificationID = await NotificationsManager.scheduleNotification( reminderNotificationID = await NotificationsManager.scheduleNotification(
i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: reminderNotificationTime}), i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: reminderNotificationTime}),
i18n.t('proxiwashScreen.notifications.machineRunningBody', {number: machineId}), i18n.t('proxiwashScreen.notifications.machineRunningBody', {number: machineId}),
new Date().getTime() + (remainingTime - reminderNotificationTime) * (60 * 1000) // Convert back to milliseconds new Date().getTime() + (remainingTime - reminderNotificationTime) * (60 * 1000), // Convert back to milliseconds
{id: machineId, isMachineFinished: false},
'reminders'
); );
} }
return reminderNotificationID; return reminderNotificationID;
@ -166,12 +189,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
* @param machineId The machine's ID * @param machineId The machine's ID
*/ */
disableNotification(machineId: string) { disableNotification(machineId: string) {
let data: Object = this.state.machinesWatched; let data = this.state.machinesWatched;
if (data.length > 0) { if (data.length > 0) {
let elem = this.state.machinesWatched.find(function (elem) { let arrayIndex = this.getMachineIndexInWatchList(machineId);
return elem.machineNumber === machineId
});
let arrayIndex = data.indexOf(elem);
if (arrayIndex !== -1) { if (arrayIndex !== -1) {
NotificationsManager.cancelScheduledNotification(data[arrayIndex].endNotificationID); NotificationsManager.cancelScheduledNotification(data[arrayIndex].endNotificationID);
if (data[arrayIndex].reminderNotificationID !== null) if (data[arrayIndex].reminderNotificationID !== null)
@ -181,6 +201,13 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
} }
} }
getMachineIndexInWatchList(machineId: string) {
let elem = this.state.machinesWatched.find(function (elem) {
return elem.machineNumber === machineId
});
return this.state.machinesWatched.indexOf(elem);
}
saveNotificationToPrefs(machineId: string, endNotificationID: string, reminderNotificationID: string | null) { saveNotificationToPrefs(machineId: string, endNotificationID: string, reminderNotificationID: string | null) {
let data = this.state.machinesWatched; let data = this.state.machinesWatched;
data.push({ data.push({

View file

@ -1,7 +1,7 @@
// @flow // @flow
import * as Permissions from 'expo-permissions'; import * as Permissions from 'expo-permissions';
import { Notifications } from 'expo'; import {Notifications} from 'expo';
/** /**
* Static class used to manage notifications sent to the user * Static class used to manage notifications sent to the user
@ -30,7 +30,7 @@ export default class NotificationsManager {
* @param body {String} Notification body text * @param body {String} Notification body text
* @returns {Promise<import("react").ReactText>} Notification Id * @returns {Promise<import("react").ReactText>} Notification Id
*/ */
static async sendNotificationImmediately (title: string, body: string) { static async sendNotificationImmediately(title: string, body: string) {
await NotificationsManager.askPermissions(); await NotificationsManager.askPermissions();
return await Notifications.presentLocalNotificationAsync({ return await Notifications.presentLocalNotificationAsync({
title: title, title: title,
@ -44,14 +44,22 @@ export default class NotificationsManager {
* @param title Notification title * @param title Notification title
* @param body Notification body text * @param body Notification body text
* @param time Time at which we should send the notification * @param time Time at which we should send the notification
* @param data Data to send with the notification, used for listeners
* @returns {Promise<import("react").ReactText>} Notification Id * @returns {Promise<import("react").ReactText>} Notification Id
*/ */
static async scheduleNotification(title: string, body: string, time: number): Promise<string> { static async scheduleNotification(title: string, body: string, time: number, data: Object, androidChannelID: string): Promise<string> {
await NotificationsManager.askPermissions(); await NotificationsManager.askPermissions();
return Notifications.scheduleLocalNotificationAsync( return Notifications.scheduleLocalNotificationAsync(
{ {
title: title, title: title,
body: body, body: body,
data: data,
ios: { // configuration for iOS.
sound: true
},
android: { // configuration for Android.
channelId: androidChannelID,
}
}, },
{ {
time: time, time: time,