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.

NotificationsManager.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // @flow
  2. import * as Permissions from 'expo-permissions';
  3. import {Notifications} from 'expo';
  4. import AsyncStorageManager from "./AsyncStorageManager";
  5. import LocaleManager from "./LocaleManager";
  6. import passwords from "../passwords";
  7. const EXPO_TOKEN_SERVER = 'https://etud.insa-toulouse.fr/~amicale_app/expo_notifications/save_token.php';
  8. /**
  9. * Static class used to manage notifications sent to the user
  10. */
  11. export default class NotificationsManager {
  12. /**
  13. * Async function asking permission to send notifications to the user
  14. *
  15. * @returns {Promise}
  16. */
  17. static async askPermissions() {
  18. const {status: existingStatus} = await Permissions.getAsync(Permissions.NOTIFICATIONS);
  19. let finalStatus = existingStatus;
  20. if (existingStatus !== 'granted') {
  21. const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
  22. finalStatus = status;
  23. }
  24. return finalStatus === 'granted';
  25. }
  26. /**
  27. * Save expo token to allow sending notifications to this device.
  28. * This token is unique for each device and won't change.
  29. * It only needs to be fetched once, then it will be saved in storage.
  30. *
  31. * @return {Promise<void>}
  32. */
  33. static async initExpoToken() {
  34. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  35. if (token === '') {
  36. try {
  37. await NotificationsManager.askPermissions();
  38. let expoToken = await Notifications.getExpoPushTokenAsync();
  39. // Save token for instant use later on
  40. AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, expoToken);
  41. } catch(e) {
  42. console.log(e);
  43. }
  44. }
  45. }
  46. /**
  47. * Gets the machines watched from the server
  48. *
  49. * @param callback Function to execute with the fetched data
  50. */
  51. static getMachineNotificationWatchlist(callback: Function) {
  52. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  53. if (token !== '') {
  54. let data = {
  55. function: 'get_machine_watchlist',
  56. password: passwords.expoNotifications,
  57. token: token,
  58. };
  59. fetch(EXPO_TOKEN_SERVER, {
  60. method: 'POST',
  61. headers: new Headers({
  62. Accept: 'application/json',
  63. 'Content-Type': 'application/json',
  64. }),
  65. body: JSON.stringify(data) // <-- Post parameters
  66. }).then((response) => response.json())
  67. .then((responseJson) => {
  68. callback(responseJson);
  69. });
  70. }
  71. }
  72. /**
  73. * Asks the server to enable/disable notifications for the specified machine
  74. *
  75. * @param machineID The machine ID
  76. * @param isEnabled True to enable notifications, false to disable
  77. */
  78. static setupMachineNotification(machineID: string, isEnabled: boolean) {
  79. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  80. if (token !== '') {
  81. let data = {
  82. function: 'setup_machine_notification',
  83. password: passwords.expoNotifications,
  84. locale: LocaleManager.getCurrentLocale(),
  85. token: token,
  86. machine_id: machineID,
  87. enabled: isEnabled
  88. };
  89. fetch(EXPO_TOKEN_SERVER, {
  90. method: 'POST',
  91. headers: new Headers({
  92. Accept: 'application/json',
  93. 'Content-Type': 'application/json',
  94. }),
  95. body: JSON.stringify(data) // <-- Post parameters
  96. });
  97. }
  98. }
  99. /**
  100. * Sends the selected reminder time for notifications to the server
  101. *
  102. * @param time The reminder time to use
  103. */
  104. static setMachineReminderNotificationTime(time: number) {
  105. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  106. if (token !== '') {
  107. let data = {
  108. function: 'set_machine_reminder',
  109. password: passwords.expoNotifications,
  110. token: token,
  111. time: time,
  112. };
  113. fetch(EXPO_TOKEN_SERVER, {
  114. method: 'POST',
  115. headers: new Headers({
  116. Accept: 'application/json',
  117. 'Content-Type': 'application/json',
  118. }),
  119. body: JSON.stringify(data) // <-- Post parameters
  120. });
  121. }
  122. }
  123. }