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 4.1KB

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