Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 i18n from 'i18n-js';
  20. import PushNotificationIOS from '@react-native-community/push-notification-ios';
  21. import PushNotification from 'react-native-push-notification';
  22. import { Platform } from 'react-native';
  23. // Used to multiply the normal notification id to create the reminder one. It allows to find it back easily
  24. const reminderIdFactor = 100;
  25. PushNotification.createChannel(
  26. {
  27. channelId: 'reminders', // (required)
  28. channelName: 'Reminders', // (required)
  29. channelDescription: 'Get laundry reminders', // (optional) default: undefined.
  30. playSound: true, // (optional) default: true
  31. soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
  32. importance: 4, // (optional) default: 4. Int value of the Android notification importance
  33. vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
  34. },
  35. (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
  36. );
  37. PushNotification.configure({
  38. // (required) Called when a remote is received or opened, or local notification is opened
  39. onNotification: function (notification) {
  40. console.log('NOTIFICATION:', notification);
  41. // process the notification
  42. // (required) Called when a remote is received or opened, or local notification is opened
  43. notification.finish(PushNotificationIOS.FetchResult.NoData);
  44. },
  45. // IOS ONLY (optional): default: all - Permissions to register.
  46. permissions: {
  47. alert: true,
  48. badge: true,
  49. sound: true,
  50. },
  51. // Should the initial notification be popped automatically
  52. // default: true
  53. popInitialNotification: true,
  54. /**
  55. * (optional) default: true
  56. * - Specified if permissions (ios) and token (android and ios) will requested or not,
  57. * - if not, you must call PushNotificationsHandler.requestPermissions() later
  58. * - if you are not using remote notification or do not have Firebase installed, use this:
  59. * requestPermissions: Platform.OS === 'ios'
  60. */
  61. requestPermissions: Platform.OS === 'ios',
  62. });
  63. /**
  64. * Creates a notification for the given machine id at the given date.
  65. *
  66. * This creates 2 notifications. One at the exact date, and one a few minutes before, according to user preference.
  67. *
  68. * @param machineID The machine id to schedule notifications for. This is used as id and in the notification string.
  69. * @param date The date to trigger the notification at
  70. */
  71. function createNotifications(machineID: string, date: Date, reminder?: number) {
  72. if (reminder && !Number.isNaN(reminder) && reminder > 0) {
  73. const id = reminderIdFactor * parseInt(machineID, 10);
  74. const reminderDate = new Date(date);
  75. reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
  76. PushNotification.localNotificationSchedule({
  77. title: i18n.t('screens.proxiwash.notifications.machineRunningTitle', {
  78. time: reminder,
  79. }),
  80. message: i18n.t('screens.proxiwash.notifications.machineRunningBody', {
  81. number: machineID,
  82. }),
  83. id: id,
  84. date: reminderDate,
  85. });
  86. }
  87. PushNotification.localNotificationSchedule({
  88. title: i18n.t('screens.proxiwash.notifications.machineFinishedTitle'),
  89. message: i18n.t('screens.proxiwash.notifications.machineFinishedBody', {
  90. number: machineID,
  91. }),
  92. id: parseInt(machineID, 10),
  93. date,
  94. });
  95. }
  96. /**
  97. * Enables or disables notifications for the given machine.
  98. *
  99. * The function is async as we need to ask user permissions.
  100. * If user denies, the promise will be rejected, otherwise it will succeed.
  101. *
  102. * @param machineID The machine ID to setup notifications for
  103. * @param isEnabled True to enable notifications, false to disable
  104. * @param endDate The trigger date, or null if disabling notifications
  105. */
  106. export function setupMachineNotification(
  107. machineID: string,
  108. isEnabled: boolean,
  109. reminder?: number,
  110. endDate?: Date | null
  111. ) {
  112. if (isEnabled && endDate) {
  113. createNotifications(machineID, endDate, reminder);
  114. } else {
  115. PushNotification.cancelLocalNotifications({ id: machineID });
  116. const reminderId = reminderIdFactor * parseInt(machineID, 10);
  117. PushNotification.cancelLocalNotifications({ id: reminderId.toString() });
  118. }
  119. }