Browse Source

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

keplyx 4 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
       "android",
9
       "android",
10
       "web"
10
       "web"
11
     ],
11
     ],
12
-    "version": "0.0.1",
12
+    "version": "0.0.2",
13
     "orientation": "portrait",
13
     "orientation": "portrait",
14
     "icon": "./assets/icon.png",
14
     "icon": "./assets/icon.png",
15
     "primaryColor": "#e42612",
15
     "primaryColor": "#e42612",

+ 35
- 8
screens/ProxiwashScreen.js View File

1
 // @flow
1
 // @flow
2
 
2
 
3
 import * as React from 'react';
3
 import * as React from 'react';
4
-import {Alert, View} from 'react-native';
4
+import {Alert, View, Platform} from 'react-native';
5
 import {Body, Card, CardItem, Left, Right, Text} from 'native-base';
5
 import {Body, Card, CardItem, Left, Right, Text} from 'native-base';
6
 import ThemeManager from '../utils/ThemeManager';
6
 import ThemeManager from '../utils/ThemeManager';
7
 import i18n from "i18n-js";
7
 import i18n from "i18n-js";
10
 import NotificationsManager from "../utils/NotificationsManager";
10
 import NotificationsManager from "../utils/NotificationsManager";
11
 import PlatformTouchable from "react-native-platform-touchable";
11
 import PlatformTouchable from "react-native-platform-touchable";
12
 import AsyncStorageManager from "../utils/AsyncStorageManager";
12
 import AsyncStorageManager from "../utils/AsyncStorageManager";
13
+import * as Expo from "expo";
13
 
14
 
14
 const DATA_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/dataProxiwash.json";
15
 const DATA_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/dataProxiwash.json";
15
 
16
 
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
     getHeaderTranslation() {
98
     getHeaderTranslation() {
80
         return i18n.t("screens.proxiwash");
99
         return i18n.t("screens.proxiwash");
81
     }
100
     }
128
             let endNotificationID = await NotificationsManager.scheduleNotification(
147
             let endNotificationID = await NotificationsManager.scheduleNotification(
129
                 i18n.t('proxiwashScreen.notifications.machineFinishedTitle'),
148
                 i18n.t('proxiwashScreen.notifications.machineFinishedTitle'),
130
                 i18n.t('proxiwashScreen.notifications.machineFinishedBody', {number: machineId}),
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
             let reminderNotificationID = await ProxiwashScreen.setupReminderNotification(machineId, remainingTime);
154
             let reminderNotificationID = await ProxiwashScreen.setupReminderNotification(machineId, remainingTime);
134
             this.saveNotificationToPrefs(machineId, endNotificationID, reminderNotificationID);
155
             this.saveNotificationToPrefs(machineId, endNotificationID, reminderNotificationID);
143
             reminderNotificationID = await NotificationsManager.scheduleNotification(
164
             reminderNotificationID = await NotificationsManager.scheduleNotification(
144
                 i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: reminderNotificationTime}),
165
                 i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: reminderNotificationTime}),
145
                 i18n.t('proxiwashScreen.notifications.machineRunningBody', {number: machineId}),
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
         return reminderNotificationID;
172
         return reminderNotificationID;
166
      * @param machineId The machine's ID
189
      * @param machineId The machine's ID
167
      */
190
      */
168
     disableNotification(machineId: string) {
191
     disableNotification(machineId: string) {
169
-        let data: Object = this.state.machinesWatched;
192
+        let data = this.state.machinesWatched;
170
         if (data.length > 0) {
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
             if (arrayIndex !== -1) {
195
             if (arrayIndex !== -1) {
176
                 NotificationsManager.cancelScheduledNotification(data[arrayIndex].endNotificationID);
196
                 NotificationsManager.cancelScheduledNotification(data[arrayIndex].endNotificationID);
177
                 if (data[arrayIndex].reminderNotificationID !== null)
197
                 if (data[arrayIndex].reminderNotificationID !== null)
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
     saveNotificationToPrefs(machineId: string, endNotificationID: string, reminderNotificationID: string | null) {
211
     saveNotificationToPrefs(machineId: string, endNotificationID: string, reminderNotificationID: string | null) {
185
         let data = this.state.machinesWatched;
212
         let data = this.state.machinesWatched;
186
         data.push({
213
         data.push({

+ 11
- 3
utils/NotificationsManager.js View File

1
 // @flow
1
 // @flow
2
 
2
 
3
 import * as Permissions from 'expo-permissions';
3
 import * as Permissions from 'expo-permissions';
4
-import { Notifications } from 'expo';
4
+import {Notifications} from 'expo';
5
 
5
 
6
 /**
6
 /**
7
  * Static class used to manage notifications sent to the user
7
  * Static class used to manage notifications sent to the user
30
      * @param body {String} Notification body text
30
      * @param body {String} Notification body text
31
      * @returns {Promise<import("react").ReactText>} Notification Id
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
         await NotificationsManager.askPermissions();
34
         await NotificationsManager.askPermissions();
35
         return await Notifications.presentLocalNotificationAsync({
35
         return await Notifications.presentLocalNotificationAsync({
36
             title: title,
36
             title: title,
44
      * @param title Notification title
44
      * @param title Notification title
45
      * @param body Notification body text
45
      * @param body Notification body text
46
      * @param time Time at which we should send the notification
46
      * @param time Time at which we should send the notification
47
+     * @param data Data to send with the notification, used for listeners
47
      * @returns {Promise<import("react").ReactText>} Notification Id
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
         await NotificationsManager.askPermissions();
51
         await NotificationsManager.askPermissions();
51
         return Notifications.scheduleLocalNotificationAsync(
52
         return Notifications.scheduleLocalNotificationAsync(
52
             {
53
             {
53
                 title: title,
54
                 title: title,
54
                 body: body,
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
                 time: time,
65
                 time: time,

Loading…
Cancel
Save