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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // @flow
  2. import * as Permissions from 'expo-permissions';
  3. import {Notifications} from 'expo';
  4. import AsyncStorageManager from "./AsyncStorageManager";
  5. const EXPO_TOKEN_SERVER = 'https://srv-falcon.etud.insa-toulouse.fr/~amicale_app/expo_notifications/save_token.php';
  6. /**
  7. * Static class used to manage notifications sent to the user
  8. */
  9. export default class NotificationsManager {
  10. /**
  11. * Async function asking permission to send notifications to the user
  12. *
  13. * @returns {Promise}
  14. */
  15. static async askPermissions() {
  16. const {status: existingStatus} = await Permissions.getAsync(Permissions.NOTIFICATIONS);
  17. let finalStatus = existingStatus;
  18. if (existingStatus !== 'granted') {
  19. const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
  20. finalStatus = status;
  21. }
  22. return finalStatus === 'granted';
  23. }
  24. /**
  25. * Async function sending a notification without delay to the user
  26. *
  27. * @param title {String} Notification title
  28. * @param body {String} Notification body text
  29. * @returns {Promise<import("react").ReactText>} Notification Id
  30. */
  31. static async sendNotificationImmediately(title: string, body: string) {
  32. await NotificationsManager.askPermissions();
  33. return await Notifications.presentLocalNotificationAsync({
  34. title: title,
  35. body: body,
  36. });
  37. };
  38. /**
  39. * Async function sending notification at the specified time
  40. *
  41. * @param title Notification title
  42. * @param body Notification body text
  43. * @param time Time at which we should send the notification
  44. * @param data Data to send with the notification, used for listeners
  45. * @param androidChannelID
  46. * @returns {Promise<import("react").ReactText>} Notification Id
  47. */
  48. static async scheduleNotification(title: string, body: string, time: number, data: Object, androidChannelID: string): Promise<string> {
  49. await NotificationsManager.askPermissions();
  50. console.log(time);
  51. let date = new Date();
  52. date.setTime(time);
  53. console.log(date);
  54. return Notifications.scheduleLocalNotificationAsync(
  55. {
  56. title: title,
  57. body: body,
  58. data: data,
  59. ios: { // configuration for iOS.
  60. sound: true
  61. },
  62. android: { // configuration for Android.
  63. channelId: androidChannelID,
  64. }
  65. },
  66. {
  67. time: time,
  68. },
  69. );
  70. };
  71. /**
  72. * Async function used to cancel the notification of a specific ID
  73. * @param notificationID {Number} The notification ID
  74. * @returns {Promise}
  75. */
  76. static async cancelScheduledNotification(notificationID: number) {
  77. await Notifications.cancelScheduledNotificationAsync(notificationID);
  78. }
  79. /**
  80. * Save expo token to allow sending notifications to this device.
  81. * This token is unique for each device and won't change.
  82. * It only needs to be fetched once, then it will be saved in storage.
  83. *
  84. * @return {Promise<void>}
  85. */
  86. static async initExpoToken() {
  87. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  88. if (token === '') {
  89. let expoToken = await Notifications.getExpoPushTokenAsync();
  90. // Save token for instant use later on
  91. AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, expoToken);
  92. }
  93. }
  94. static getMachineNotificationWatchlist(callback: Function) {
  95. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  96. if (token === '') {
  97. throw Error('Expo token not available');
  98. }
  99. let data = {
  100. function: 'get_machine_watchlist',
  101. token: token,
  102. };
  103. fetch(EXPO_TOKEN_SERVER, {
  104. method: 'POST',
  105. headers: new Headers({
  106. Accept: 'application/json',
  107. 'Content-Type': 'application/json',
  108. }),
  109. body: JSON.stringify(data) // <-- Post parameters
  110. })
  111. .then((response) => response.json())
  112. .then((responseJson) => {
  113. callback(responseJson);
  114. })
  115. .catch((error) => {
  116. console.log(error);
  117. });
  118. }
  119. /**
  120. * Ask the server to enable/disable notifications for the specified machine
  121. *
  122. * @param machineID
  123. * @param isEnabled
  124. */
  125. static setupMachineNotification(machineID: string, isEnabled: boolean) {
  126. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  127. if (token === '') {
  128. throw Error('Expo token not available');
  129. }
  130. let data = {
  131. function: 'setup_machine_notification',
  132. token: token,
  133. machine_id: machineID,
  134. enabled: isEnabled
  135. };
  136. fetch(EXPO_TOKEN_SERVER, {
  137. method: 'POST',
  138. headers: new Headers({
  139. Accept: 'application/json',
  140. 'Content-Type': 'application/json',
  141. }),
  142. body: JSON.stringify(data) // <-- Post parameters
  143. })
  144. .then((response) => response.text())
  145. .then((responseText) => {
  146. console.log(responseText);
  147. })
  148. .catch((error) => {
  149. console.log(error);
  150. });
  151. }
  152. /**
  153. * Send the selected reminder time for notifications to the server
  154. * @param time
  155. */
  156. static setMachineReminderNotificationTime(time: number) {
  157. console.log(time);
  158. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  159. if (token === '') {
  160. throw Error('Expo token not available');
  161. }
  162. let data = {
  163. function: 'set_machine_reminder',
  164. token: token,
  165. time: time,
  166. };
  167. fetch(EXPO_TOKEN_SERVER, {
  168. method: 'POST',
  169. headers: new Headers({
  170. Accept: 'application/json',
  171. 'Content-Type': 'application/json',
  172. }),
  173. body: JSON.stringify(data) // <-- Post parameters
  174. })
  175. .then((response) => response.text())
  176. .then((responseText) => {
  177. console.log(responseText);
  178. })
  179. .catch((error) => {
  180. console.log(error);
  181. });
  182. }
  183. }