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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // @flow
  2. import * as Permissions from 'expo-permissions';
  3. import {Notifications} from 'expo';
  4. /**
  5. * Static class used to manage notifications sent to the user
  6. */
  7. export default class NotificationsManager {
  8. /**
  9. * Async function asking permission to send notifications to the user
  10. *
  11. * @returns {Promise}
  12. */
  13. static async 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. * Async function sending a notification without delay to the user
  24. *
  25. * @param title {String} Notification title
  26. * @param body {String} Notification body text
  27. * @returns {Promise<import("react").ReactText>} Notification Id
  28. */
  29. static async sendNotificationImmediately(title: string, body: string) {
  30. await NotificationsManager.askPermissions();
  31. return await Notifications.presentLocalNotificationAsync({
  32. title: title,
  33. body: body,
  34. });
  35. };
  36. /**
  37. * Async function sending notification at the specified time
  38. *
  39. * @param title Notification title
  40. * @param body Notification body text
  41. * @param time Time at which we should send the notification
  42. * @param data Data to send with the notification, used for listeners
  43. * @returns {Promise<import("react").ReactText>} Notification Id
  44. */
  45. static async scheduleNotification(title: string, body: string, time: number, data: Object, androidChannelID: string): Promise<string> {
  46. await NotificationsManager.askPermissions();
  47. return Notifications.scheduleLocalNotificationAsync(
  48. {
  49. title: title,
  50. body: body,
  51. data: data,
  52. ios: { // configuration for iOS.
  53. sound: true
  54. },
  55. android: { // configuration for Android.
  56. channelId: androidChannelID,
  57. }
  58. },
  59. {
  60. time: time,
  61. },
  62. );
  63. };
  64. /**
  65. * Async function used to cancel the notification of a specific ID
  66. * @param notificationID {Number} The notification ID
  67. * @returns {Promise}
  68. */
  69. static async cancelScheduledNotification(notificationID: number) {
  70. await Notifications.cancelScheduledNotificationAsync(notificationID);
  71. }
  72. }