From fbe687ca989c904273ed40965fe3d97d0e43e75f Mon Sep 17 00:00:00 2001 From: keplyx Date: Sun, 4 Aug 2019 14:28:41 +0200 Subject: [PATCH] Added notification vibrations and remove machine from watch list on last notification --- app.json | 2 +- screens/ProxiwashScreen.js | 43 ++++++++++++++++++++++++++++------- utils/NotificationsManager.js | 14 +++++++++--- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app.json b/app.json index 03a8d11..c363d63 100644 --- a/app.json +++ b/app.json @@ -9,7 +9,7 @@ "android", "web" ], - "version": "0.0.1", + "version": "0.0.2", "orientation": "portrait", "icon": "./assets/icon.png", "primaryColor": "#e42612", diff --git a/screens/ProxiwashScreen.js b/screens/ProxiwashScreen.js index d6cc078..2ec2a6c 100644 --- a/screens/ProxiwashScreen.js +++ b/screens/ProxiwashScreen.js @@ -1,7 +1,7 @@ // @flow 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 ThemeManager from '../utils/ThemeManager'; import i18n from "i18n-js"; @@ -10,6 +10,7 @@ import FetchedDataSectionList from "../components/FetchedDataSectionList"; import NotificationsManager from "../utils/NotificationsManager"; import PlatformTouchable from "react-native-platform-touchable"; import AsyncStorageManager from "../utils/AsyncStorageManager"; +import * as Expo from "expo"; 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() { return i18n.t("screens.proxiwash"); } @@ -128,7 +147,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList { let endNotificationID = await NotificationsManager.scheduleNotification( i18n.t('proxiwashScreen.notifications.machineFinishedTitle'), 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); this.saveNotificationToPrefs(machineId, endNotificationID, reminderNotificationID); @@ -143,7 +164,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList { reminderNotificationID = await NotificationsManager.scheduleNotification( i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: reminderNotificationTime}), 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; @@ -166,12 +189,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList { * @param machineId The machine's ID */ disableNotification(machineId: string) { - let data: Object = this.state.machinesWatched; + let data = this.state.machinesWatched; if (data.length > 0) { - let elem = this.state.machinesWatched.find(function (elem) { - return elem.machineNumber === machineId - }); - let arrayIndex = data.indexOf(elem); + let arrayIndex = this.getMachineIndexInWatchList(machineId); if (arrayIndex !== -1) { NotificationsManager.cancelScheduledNotification(data[arrayIndex].endNotificationID); 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) { let data = this.state.machinesWatched; data.push({ diff --git a/utils/NotificationsManager.js b/utils/NotificationsManager.js index 978fcd9..036c744 100644 --- a/utils/NotificationsManager.js +++ b/utils/NotificationsManager.js @@ -1,7 +1,7 @@ // @flow import * as Permissions from 'expo-permissions'; -import { Notifications } from 'expo'; +import {Notifications} from 'expo'; /** * Static class used to manage notifications sent to the user @@ -30,7 +30,7 @@ export default class NotificationsManager { * @param body {String} Notification body text * @returns {Promise} Notification Id */ - static async sendNotificationImmediately (title: string, body: string) { + static async sendNotificationImmediately(title: string, body: string) { await NotificationsManager.askPermissions(); return await Notifications.presentLocalNotificationAsync({ title: title, @@ -44,14 +44,22 @@ export default class NotificationsManager { * @param title Notification title * @param body Notification body text * @param time Time at which we should send the notification + * @param data Data to send with the notification, used for listeners * @returns {Promise} Notification Id */ - static async scheduleNotification(title: string, body: string, time: number): Promise { + static async scheduleNotification(title: string, body: string, time: number, data: Object, androidChannelID: string): Promise { await NotificationsManager.askPermissions(); return Notifications.scheduleLocalNotificationAsync( { title: title, body: body, + data: data, + ios: { // configuration for iOS. + sound: true + }, + android: { // configuration for Android. + channelId: androidChannelID, + } }, { time: time,