UNPKG

3.36 kBJavaScriptView Raw
1import * as badgin from 'badgin';
2import uuidv4 from 'uuid/v4';
3import { guardPermission, getExponentPushTokenAsync, getDevicePushTokenAsync, } from './ExponentNotificationsHelper.web';
4// Register `message`'s event listener (side-effect)
5import './ExponentNotifications.fx.web';
6let currentBadgeNumber = 0;
7function transformLocalNotification(notification, tag) {
8 const { web = {}, ...abstractNotification } = notification;
9 tag = web.tag || tag;
10 const nativeNotification = {
11 ...abstractNotification,
12 tag,
13 ...web,
14 // Show that this notification is a local notification
15 _isLocal: true,
16 };
17 return [nativeNotification.title, nativeNotification];
18}
19async function getRegistrationAsync() {
20 guardPermission();
21 const registration = await navigator.serviceWorker.getRegistration();
22 if (!registration) {
23 throw new Error('Failed to get notification registration!');
24 }
25 return registration;
26}
27async function getNotificationsAsync(tag) {
28 const registration = await getRegistrationAsync();
29 const notifications = await registration.getNotifications(tag ? { tag } : undefined);
30 return notifications;
31}
32export default {
33 async presentLocalNotification(notification) {
34 const registration = await getRegistrationAsync();
35 const tag = uuidv4();
36 registration.showNotification(...transformLocalNotification(notification, tag));
37 return tag;
38 },
39 async scheduleLocalNotification(notification, options = {}) {
40 if (options.intervalMs) {
41 const registration = await getRegistrationAsync();
42 const tag = uuidv4();
43 setTimeout(() => {
44 registration.showNotification(...transformLocalNotification(notification, tag));
45 }, options.intervalMs);
46 return tag;
47 }
48 else if (options.time) {
49 const intervalMs = options.time - Date.now();
50 if (intervalMs < 0) {
51 throw new Error('Expo.Notifications.scheduleLocalNotification(): options.time must be some time in the future.');
52 }
53 return this.scheduleLocalNotification(notification, {
54 intervalMs,
55 });
56 }
57 throw new Error(`Expo.Notifications.scheduleLocalNotification() options ${JSON.stringify(options, null, 2)} are not supported yet.`);
58 },
59 async dismissNotification(notificationId) {
60 const notifications = await getNotificationsAsync(notificationId);
61 for (const notification of notifications) {
62 notification.close();
63 }
64 },
65 async dismissAllNotifications() {
66 this.dismissNotification();
67 },
68 async cancelScheduledNotificationAsync(notificationId) {
69 this.dismissNotification(notificationId);
70 },
71 async cancelAllScheduledNotificationsAsync() {
72 this.dismissNotification();
73 },
74 async getExponentPushTokenAsync() {
75 return await getExponentPushTokenAsync();
76 },
77 async getDevicePushTokenAsync() {
78 return await getDevicePushTokenAsync();
79 },
80 async getBadgeNumberAsync() {
81 return currentBadgeNumber;
82 },
83 async setBadgeNumberAsync(badgeNumber) {
84 currentBadgeNumber = badgeNumber;
85 badgin.set(badgeNumber);
86 },
87};
88//# sourceMappingURL=ExponentNotifications.web.js.map
\No newline at end of file