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.js 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // @flow
  2. import {checkNotifications, requestNotifications, RESULTS} from 'react-native-permissions';
  3. import AsyncStorageManager from "../managers/AsyncStorageManager";
  4. import i18n from "i18n-js";
  5. const PushNotification = require("react-native-push-notification");
  6. // Used to multiply the normal notification id to create the reminder one. It allows to find it back easily
  7. const reminderIdFactor = 100;
  8. /**
  9. * Async function asking permission to send notifications to the user.
  10. * Used on ios.
  11. *
  12. * @returns {Promise}
  13. */
  14. export async function askPermissions() {
  15. return new Promise(((resolve, reject) => {
  16. checkNotifications().then(({status}) => {
  17. if (status === RESULTS.GRANTED)
  18. resolve();
  19. else if (status === RESULTS.BLOCKED)
  20. reject()
  21. else {
  22. requestNotifications().then(({status}) => {
  23. if (status === RESULTS.GRANTED)
  24. resolve();
  25. else
  26. reject();
  27. });
  28. }
  29. });
  30. }));
  31. }
  32. /**
  33. * Creates a notification for the given machine id at the given date.
  34. *
  35. * This creates 2 notifications. One at the exact date, and one a few minutes before, according to user preference.
  36. *
  37. * @param machineID The machine id to schedule notifications for. This is used as id and in the notification string.
  38. * @param date The date to trigger the notification at
  39. */
  40. function createNotifications(machineID: string, date: Date) {
  41. let reminder = AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key);
  42. if (!isNaN(reminder) && reminder > 0) {
  43. let id = reminderIdFactor * parseInt(machineID);
  44. let reminderDate = new Date(date);
  45. reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
  46. PushNotification.localNotificationSchedule({
  47. title: i18n.t("screens.proxiwash.notifications.machineRunningTitle", {time: reminder}),
  48. message: i18n.t("screens.proxiwash.notifications.machineRunningBody", {number: machineID}),
  49. id: id.toString(),
  50. date: reminderDate,
  51. });
  52. console.log("Setting up notifications for ", date, " and reminder for ", reminderDate);
  53. } else
  54. console.log("Setting up notifications for ", date);
  55. PushNotification.localNotificationSchedule({
  56. title: i18n.t("screens.proxiwash.notifications.machineFinishedTitle"),
  57. message: i18n.t("screens.proxiwash.notifications.machineFinishedBody", {number: machineID}),
  58. id: machineID,
  59. date: date,
  60. });
  61. }
  62. /**
  63. * Enables or disables notifications for the given machine.
  64. *
  65. * The function is async as we need to ask user permissions.
  66. * If user denies, the promise will be rejected, otherwise it will succeed.
  67. *
  68. * @param machineID The machine ID to setup notifications for
  69. * @param isEnabled True to enable notifications, false to disable
  70. * @param endDate The trigger date, or null if disabling notifications
  71. */
  72. export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate: Date | null) {
  73. return new Promise((resolve, reject) => {
  74. if (isEnabled && endDate != null) {
  75. askPermissions()
  76. .then(() => {
  77. createNotifications(machineID, endDate);
  78. resolve();
  79. })
  80. .catch(() => {
  81. reject();
  82. });
  83. } else {
  84. PushNotification.cancelLocalNotifications({id: machineID});
  85. let reminderId = reminderIdFactor * parseInt(machineID);
  86. PushNotification.cancelLocalNotifications({id: reminderId.toString()});
  87. resolve();
  88. }
  89. });
  90. }