Browse Source

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

keplyx 2 years ago
parent
commit
fbe687ca98
3 changed files with 47 additions and 12 deletions
  1. 1
    1
      app.json
  2. 35
    8
      screens/ProxiwashScreen.js
  3. 11
    3
      utils/NotificationsManager.js

+ 1
- 1
app.json View File

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

+ 35
- 8
screens/ProxiwashScreen.js View File

@@ -1,7 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import * as React from 'react';
4
-import {Alert, View} from 'react-native';
4
+import {Alert, View, Platform} from 'react-native';
5 5
 import {Body, Card, CardItem, Left, Right, Text} from 'native-base';
6 6
 import ThemeManager from '../utils/ThemeManager';
7 7
 import i18n from "i18n-js";
@@ -10,6 +10,7 @@ import FetchedDataSectionList from "../components/FetchedDataSectionList";
10 10
 import NotificationsManager from "../utils/NotificationsManager";
11 11
 import PlatformTouchable from "react-native-platform-touchable";
12 12
 import AsyncStorageManager from "../utils/AsyncStorageManager";
13
+import * as Expo from "expo";
13 14
 
14 15
 const DATA_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/dataProxiwash.json";
15 16
 
@@ -76,6 +77,24 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
76 77
         };
77 78
     }
78 79
 
80
+    componentDidMount() {
81
+        if (Platform.OS === 'android') {
82
+            Expo.Notifications.createChannelAndroidAsync('reminders', {
83
+                name: 'Reminders',
84
+                priority: 'max',
85
+                vibrate: [0, 250, 250, 250],
86
+            });
87
+        }
88
+        // Remove machine from watch list when receiving last notification
89
+        Expo.Notifications.addListener((notification) => {
90
+            if (notification.data !== undefined) {
91
+                if (this.isMachineWatched(notification.data.id) && notification.data.isMachineFinished === true) {
92
+                    this.removeNotificationFromPrefs(this.getMachineIndexInWatchList(notification.data.id));
93
+                }
94
+            }
95
+        });
96
+    }
97
+
79 98
     getHeaderTranslation() {
80 99
         return i18n.t("screens.proxiwash");
81 100
     }
@@ -128,7 +147,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
128 147
             let endNotificationID = await NotificationsManager.scheduleNotification(
129 148
                 i18n.t('proxiwashScreen.notifications.machineFinishedTitle'),
130 149
                 i18n.t('proxiwashScreen.notifications.machineFinishedBody', {number: machineId}),
131
-                new Date().getTime() + remainingTime * (60 * 1000) // Convert back to milliseconds
150
+                new Date().getTime() + remainingTime * (60 * 1000), // Convert back to milliseconds
151
+                {id: machineId, isMachineFinished: true},
152
+                'reminders'
132 153
             );
133 154
             let reminderNotificationID = await ProxiwashScreen.setupReminderNotification(machineId, remainingTime);
134 155
             this.saveNotificationToPrefs(machineId, endNotificationID, reminderNotificationID);
@@ -143,7 +164,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
143 164
             reminderNotificationID = await NotificationsManager.scheduleNotification(
144 165
                 i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: reminderNotificationTime}),
145 166
                 i18n.t('proxiwashScreen.notifications.machineRunningBody', {number: machineId}),
146
-                new Date().getTime() + (remainingTime - reminderNotificationTime) * (60 * 1000) // Convert back to milliseconds
167
+                new Date().getTime() + (remainingTime - reminderNotificationTime) * (60 * 1000), // Convert back to milliseconds
168
+                {id: machineId, isMachineFinished: false},
169
+                'reminders'
147 170
             );
148 171
         }
149 172
         return reminderNotificationID;
@@ -166,12 +189,9 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
166 189
      * @param machineId The machine's ID
167 190
      */
168 191
     disableNotification(machineId: string) {
169
-        let data: Object = this.state.machinesWatched;
192
+        let data = this.state.machinesWatched;
170 193
         if (data.length > 0) {
171
-            let elem = this.state.machinesWatched.find(function (elem) {
172
-                return elem.machineNumber === machineId
173
-            });
174
-            let arrayIndex = data.indexOf(elem);
194
+            let arrayIndex = this.getMachineIndexInWatchList(machineId);
175 195
             if (arrayIndex !== -1) {
176 196
                 NotificationsManager.cancelScheduledNotification(data[arrayIndex].endNotificationID);
177 197
                 if (data[arrayIndex].reminderNotificationID !== null)
@@ -181,6 +201,13 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
181 201
         }
182 202
     }
183 203
 
204
+    getMachineIndexInWatchList(machineId: string) {
205
+        let elem = this.state.machinesWatched.find(function (elem) {
206
+            return elem.machineNumber === machineId
207
+        });
208
+        return this.state.machinesWatched.indexOf(elem);
209
+    }
210
+
184 211
     saveNotificationToPrefs(machineId: string, endNotificationID: string, reminderNotificationID: string | null) {
185 212
         let data = this.state.machinesWatched;
186 213
         data.push({

+ 11
- 3
utils/NotificationsManager.js View File

@@ -1,7 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import * as Permissions from 'expo-permissions';
4
-import { Notifications } from 'expo';
4
+import {Notifications} from 'expo';
5 5
 
6 6
 /**
7 7
  * Static class used to manage notifications sent to the user
@@ -30,7 +30,7 @@ export default class NotificationsManager {
30 30
      * @param body {String} Notification body text
31 31
      * @returns {Promise<import("react").ReactText>} Notification Id
32 32
      */
33
-    static async sendNotificationImmediately (title: string, body: string) {
33
+    static async sendNotificationImmediately(title: string, body: string) {
34 34
         await NotificationsManager.askPermissions();
35 35
         return await Notifications.presentLocalNotificationAsync({
36 36
             title: title,
@@ -44,14 +44,22 @@ export default class NotificationsManager {
44 44
      * @param title Notification title
45 45
      * @param body Notification body text
46 46
      * @param time Time at which we should send the notification
47
+     * @param data Data to send with the notification, used for listeners
47 48
      * @returns {Promise<import("react").ReactText>} Notification Id
48 49
      */
49
-    static async scheduleNotification(title: string, body: string, time: number): Promise<string> {
50
+    static async scheduleNotification(title: string, body: string, time: number, data: Object, androidChannelID: string): Promise<string> {
50 51
         await NotificationsManager.askPermissions();
51 52
         return Notifications.scheduleLocalNotificationAsync(
52 53
             {
53 54
                 title: title,
54 55
                 body: body,
56
+                data: data,
57
+                ios: { // configuration for iOS.
58
+                    sound: true
59
+                },
60
+                android: { // configuration for Android.
61
+                    channelId: androidChannelID,
62
+                }
55 63
             },
56 64
             {
57 65
                 time: time,

Loading…
Cancel
Save