Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Notifications.ts 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import {
  20. checkNotifications,
  21. requestNotifications,
  22. RESULTS,
  23. } from 'react-native-permissions';
  24. import i18n from 'i18n-js';
  25. import AsyncStorageManager from '../managers/AsyncStorageManager';
  26. const PushNotification = require('react-native-push-notification');
  27. // Used to multiply the normal notification id to create the reminder one. It allows to find it back easily
  28. const reminderIdFactor = 100;
  29. /**
  30. * Async function asking permission to send notifications to the user.
  31. * Used on ios.
  32. *
  33. * @returns {Promise<void>}
  34. */
  35. export async function askPermissions(): Promise<void> {
  36. return new Promise((resolve: () => void, reject: () => void) => {
  37. checkNotifications().then(({status}: {status: string}) => {
  38. if (status === RESULTS.GRANTED) {
  39. resolve();
  40. } else if (status === RESULTS.BLOCKED) {
  41. reject();
  42. } else {
  43. requestNotifications([]).then((result: {status: string}) => {
  44. if (result.status === RESULTS.GRANTED) {
  45. resolve();
  46. } else {
  47. reject();
  48. }
  49. });
  50. }
  51. });
  52. });
  53. }
  54. /**
  55. * Creates a notification for the given machine id at the given date.
  56. *
  57. * This creates 2 notifications. One at the exact date, and one a few minutes before, according to user preference.
  58. *
  59. * @param machineID The machine id to schedule notifications for. This is used as id and in the notification string.
  60. * @param date The date to trigger the notification at
  61. */
  62. function createNotifications(machineID: string, date: Date) {
  63. const reminder = AsyncStorageManager.getNumber(
  64. AsyncStorageManager.PREFERENCES.proxiwashNotifications.key,
  65. );
  66. if (!Number.isNaN(reminder) && reminder > 0) {
  67. const id = reminderIdFactor * parseInt(machineID, 10);
  68. const reminderDate = new Date(date);
  69. reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
  70. PushNotification.localNotificationSchedule({
  71. title: i18n.t('screens.proxiwash.notifications.machineRunningTitle', {
  72. time: reminder,
  73. }),
  74. message: i18n.t('screens.proxiwash.notifications.machineRunningBody', {
  75. number: machineID,
  76. }),
  77. id: id.toString(),
  78. date: reminderDate,
  79. });
  80. }
  81. PushNotification.localNotificationSchedule({
  82. title: i18n.t('screens.proxiwash.notifications.machineFinishedTitle'),
  83. message: i18n.t('screens.proxiwash.notifications.machineFinishedBody', {
  84. number: machineID,
  85. }),
  86. id: machineID,
  87. date,
  88. });
  89. }
  90. /**
  91. * Enables or disables notifications for the given machine.
  92. *
  93. * The function is async as we need to ask user permissions.
  94. * If user denies, the promise will be rejected, otherwise it will succeed.
  95. *
  96. * @param machineID The machine ID to setup notifications for
  97. * @param isEnabled True to enable notifications, false to disable
  98. * @param endDate The trigger date, or null if disabling notifications
  99. */
  100. export async function setupMachineNotification(
  101. machineID: string,
  102. isEnabled: boolean,
  103. endDate: Date | null,
  104. ): Promise<void> {
  105. return new Promise((resolve: () => void, reject: () => void) => {
  106. if (isEnabled && endDate != null) {
  107. askPermissions()
  108. .then(() => {
  109. createNotifications(machineID, endDate);
  110. resolve();
  111. })
  112. .catch(() => {
  113. reject();
  114. });
  115. } else {
  116. PushNotification.cancelLocalNotifications({id: machineID});
  117. const reminderId = reminderIdFactor * parseInt(machineID, 10);
  118. PushNotification.cancelLocalNotifications({id: reminderId.toString()});
  119. resolve();
  120. }
  121. });
  122. }