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.

NotificationsManager.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 (AsyncStorageManager.getInstance().preferences.expoToken.current === '') {
  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. /**
  95. * Ask the server to enable/disable notifications for the specified machine
  96. *
  97. * @param machineID
  98. * @param isEnabled
  99. */
  100. static setupMachineNotification(machineID: string, isEnabled: boolean) {
  101. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  102. if (token === '') {
  103. throw Error('Expo token not available');
  104. }
  105. let data = {
  106. function: 'setup_machine_notification',
  107. token: token,
  108. machine_id: machineID,
  109. enabled: isEnabled
  110. };
  111. fetch(EXPO_TOKEN_SERVER, {
  112. method: 'POST',
  113. headers: new Headers({
  114. Accept: 'application/json',
  115. 'Content-Type': 'application/json',
  116. }),
  117. body: JSON.stringify(data) // <-- Post parameters
  118. })
  119. .then((response) => response.text())
  120. .then((responseText) => {
  121. console.log(responseText);
  122. })
  123. .catch((error) => {
  124. console.log(error);
  125. });
  126. }
  127. }