Application Android et IOS pour l'amicale des élèves
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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