-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ExponentNotifications.web.ts
124 lines (112 loc) · 3.73 KB
/
ExponentNotifications.web.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as badgin from 'badgin';
import UUID from 'uuid-js';
import { LocalNotification, LocalNotificationId } from './Notifications.types';
import {
guardPermission,
getExponentPushTokenAsync,
getDevicePushTokenAsync,
} from './ExponentNotificationsHelper.web';
// Register `message`'s event listener (side-effect)
import './ExponentNotifications.fx.web';
let currentBadgeNumber = 0;
function transformLocalNotification(
notification: LocalNotification,
tag: string
): [string, NotificationOptions] {
const { web = {}, ...abstractNotification } = notification;
tag = web.tag || tag;
const nativeNotification = {
...abstractNotification,
tag,
...web,
// Show that this notification is a local notification
_isLocal: true,
};
return [nativeNotification.title, nativeNotification];
}
function generateID(): string {
return UUID.create().toString();
}
async function getRegistrationAsync(): Promise<ServiceWorkerRegistration> {
guardPermission();
const registration = await navigator.serviceWorker.getRegistration();
if (!registration) {
throw new Error('Failed to get notification registration!');
}
return registration;
}
async function getNotificationsAsync(tag?: string): Promise<Notification[]> {
const registration = await getRegistrationAsync();
const notifications = await registration.getNotifications(tag ? { tag } : undefined);
return notifications;
}
export default {
async presentLocalNotification(notification: LocalNotification): Promise<LocalNotificationId> {
const registration = await getRegistrationAsync();
const tag = generateID();
registration.showNotification(...transformLocalNotification(notification, tag));
return tag;
},
async scheduleLocalNotification(
notification: any,
options: {
time?: Date | number;
repeat?: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
intervalMs?: number;
} = {}
): Promise<string> {
if (options.intervalMs) {
const registration = await getRegistrationAsync();
const tag = generateID();
setTimeout(() => {
registration.showNotification(...transformLocalNotification(notification, tag));
}, options.intervalMs);
return tag;
} else if (options.time) {
const intervalMs = (options.time as number) - Date.now();
if (intervalMs < 0) {
throw new Error(
'Expo.Notifications.scheduleLocalNotification(): options.time must be some time in the future.'
);
}
return this.scheduleLocalNotification(notification, {
intervalMs,
});
}
throw new Error(
`Expo.Notifications.scheduleLocalNotification() options ${JSON.stringify(
options,
null,
2
)} are not supported yet.`
);
},
async dismissNotification(notificationId?: string): Promise<void> {
const notifications = await getNotificationsAsync(notificationId);
for (const notification of notifications) {
notification.close();
}
},
async dismissAllNotifications(): Promise<void> {
this.dismissNotification();
},
async cancelScheduledNotificationAsync(notificationId: string): Promise<void> {
this.dismissNotification(notificationId);
},
async cancelAllScheduledNotificationsAsync(): Promise<void> {
this.dismissNotification();
},
async getExponentPushTokenAsync(): Promise<string> {
return await getExponentPushTokenAsync();
},
async getDevicePushTokenAsync(): Promise<{ type: string; data: Object }> {
return await getDevicePushTokenAsync();
},
async getBadgeNumberAsync(): Promise<number> {
return currentBadgeNumber;
},
async setBadgeNumberAsync(badgeNumber: number): Promise<void> {
currentBadgeNumber = badgeNumber;
badgin.set(badgeNumber);
},
};