-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathweb.ts
248 lines (215 loc) · 6.5 KB
/
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { WebPlugin } from '@capacitor/core';
import type { PermissionState } from '@capacitor/core';
import type {
DeliveredNotifications,
EnabledResult,
ListChannelsResult,
LocalNotificationSchema,
LocalNotificationsPlugin,
PendingResult,
PermissionStatus,
ScheduleOptions,
ScheduleResult,
SettingsPermissionStatus,
} from './definitions';
export class LocalNotificationsWeb
extends WebPlugin
implements LocalNotificationsPlugin
{
protected pending: LocalNotificationSchema[] = [];
protected deliveredNotifications: Notification[] = [];
async getDeliveredNotifications(): Promise<DeliveredNotifications> {
const deliveredSchemas = [];
for (const notification of this.deliveredNotifications) {
const deliveredSchema: LocalNotificationSchema = {
title: notification.title,
id: parseInt(notification.tag),
body: notification.body,
};
deliveredSchemas.push(deliveredSchema);
}
return {
notifications: deliveredSchemas,
};
}
async removeDeliveredNotifications(
delivered: DeliveredNotifications,
): Promise<void> {
for (const toRemove of delivered.notifications) {
const found = this.deliveredNotifications.find(
n => n.tag === String(toRemove.id),
);
found?.close();
this.deliveredNotifications = this.deliveredNotifications.filter(
() => !found,
);
}
}
async removeAllDeliveredNotifications(): Promise<void> {
for (const notification of this.deliveredNotifications) {
notification.close();
}
this.deliveredNotifications = [];
}
async createChannel(): Promise<void> {
throw this.unimplemented('Not implemented on web.');
}
async deleteChannel(): Promise<void> {
throw this.unimplemented('Not implemented on web.');
}
async listChannels(): Promise<ListChannelsResult> {
throw this.unimplemented('Not implemented on web.');
}
async schedule(options: ScheduleOptions): Promise<ScheduleResult> {
if (!this.hasNotificationSupport()) {
throw this.unavailable('Notifications not supported in this browser.');
}
for (const notification of options.notifications) {
this.sendNotification(notification);
}
return {
notifications: options.notifications.map(notification => ({
id: notification.id,
})),
};
}
async getPending(): Promise<PendingResult> {
return {
notifications: this.pending,
};
}
async registerActionTypes(): Promise<void> {
throw this.unimplemented('Not implemented on web.');
}
async cancel(pending: ScheduleResult): Promise<void> {
this.pending = this.pending.filter(
notification =>
!pending.notifications.find(n => n.id === notification.id),
);
}
async areEnabled(): Promise<EnabledResult> {
const { display } = await this.checkPermissions();
return {
value: display === 'granted',
};
}
async changeExactNotificationSetting(): Promise<SettingsPermissionStatus> {
throw this.unimplemented('Not implemented on web.');
}
async checkExactNotificationSetting(): Promise<SettingsPermissionStatus> {
throw this.unimplemented('Not implemented on web.');
}
async requestPermissions(): Promise<PermissionStatus> {
if (!this.hasNotificationSupport()) {
throw this.unavailable('Notifications not supported in this browser.');
}
const display = this.transformNotificationPermission(
await Notification.requestPermission(),
);
return { display };
}
async checkPermissions(): Promise<PermissionStatus> {
if (!this.hasNotificationSupport()) {
throw this.unavailable('Notifications not supported in this browser.');
}
const display = this.transformNotificationPermission(
Notification.permission,
);
return { display };
}
protected hasNotificationSupport = (): boolean => {
if (!('Notification' in window) || !Notification.requestPermission) {
return false;
}
if (Notification.permission !== 'granted') {
// don't test for `new Notification` if permission has already been granted
// otherwise this sends a real notification on supported browsers
try {
new Notification('');
} catch (e) {
if (e.name == 'TypeError') {
return false;
}
}
}
return true;
};
protected transformNotificationPermission(
permission: NotificationPermission,
): PermissionState {
switch (permission) {
case 'granted':
return 'granted';
case 'denied':
return 'denied';
default:
return 'prompt';
}
}
protected sendPending(): void {
const toRemove: LocalNotificationSchema[] = [];
const now = new Date().getTime();
for (const notification of this.pending) {
if (
notification.schedule?.at &&
notification.schedule.at.getTime() <= now
) {
this.buildNotification(notification);
toRemove.push(notification);
}
}
this.pending = this.pending.filter(
notification => !toRemove.find(n => n === notification),
);
}
protected sendNotification(notification: LocalNotificationSchema): void {
if (notification.schedule?.at) {
const diff = notification.schedule.at.getTime() - new Date().getTime();
this.pending.push(notification);
setTimeout(() => {
this.sendPending();
}, diff);
return;
}
this.buildNotification(notification);
}
protected buildNotification(
notification: LocalNotificationSchema,
): Notification {
const localNotification = new Notification(notification.title, {
body: notification.body,
tag: String(notification.id),
});
localNotification.addEventListener(
'click',
this.onClick.bind(this, notification),
false,
);
localNotification.addEventListener(
'show',
this.onShow.bind(this, notification),
false,
);
localNotification.addEventListener(
'close',
() => {
this.deliveredNotifications = this.deliveredNotifications.filter(
() => !this,
);
},
false,
);
this.deliveredNotifications.push(localNotification);
return localNotification;
}
protected onClick(notification: LocalNotificationSchema): void {
const data = {
actionId: 'tap',
notification,
};
this.notifyListeners('localNotificationActionPerformed', data);
}
protected onShow(notification: LocalNotificationSchema): void {
this.notifyListeners('localNotificationReceived', notification);
}
}