-
Notifications
You must be signed in to change notification settings - Fork 961
/
local-notifications.ts
144 lines (121 loc) · 4.11 KB
/
local-notifications.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
import { WebPlugin } from './index';
import {
LocalNotificationsPlugin,
LocalNotificationEnabledResult,
LocalNotificationPendingList,
LocalNotificationActionType,
LocalNotification,
LocalNotificationScheduleResult,
NotificationPermissionResponse,
NotificationChannel,
NotificationChannelList
} from '../core-plugin-definitions';
import { PermissionsRequestResult } from '../definitions';
export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotificationsPlugin {
private pending: LocalNotification[] = [];
constructor() {
super({
name: 'LocalNotifications',
platforms: ['web']
});
}
createChannel(channel: NotificationChannel): Promise<void> {
throw new Error('Feature not available in the browser. ' + channel.id);
}
deleteChannel(channel: NotificationChannel): Promise<void> {
throw new Error('Feature not available in the browser. ' + channel.id);
}
listChannels(): Promise<NotificationChannelList> {
throw new Error('Feature not available in the browser');
}
sendPending() {
const toRemove: LocalNotification[] = [];
const now = +new Date;
this.pending.forEach(localNotification => {
if (localNotification.schedule && localNotification.schedule.at) {
if (+localNotification.schedule.at <= now) {
this.buildNotification(localNotification);
toRemove.push(localNotification);
}
}
});
console.log('Sent pending, removing', toRemove);
this.pending = this.pending.filter(localNotification => !toRemove.find(ln => ln === localNotification));
}
sendNotification(localNotification: LocalNotification): Notification {
const l = localNotification;
if (localNotification.schedule && localNotification.schedule.at) {
const diff = +localNotification.schedule.at - +new Date;
this.pending.push(l);
setTimeout(() => {
this.sendPending();
}, diff);
return;
}
this.buildNotification(localNotification);
}
buildNotification(localNotification: LocalNotification) {
const l = localNotification;
return new Notification(l.title, {
body: l.body
});
}
schedule(options: { notifications: LocalNotification[]; }): Promise<LocalNotificationScheduleResult> {
const notifications: Notification[] = [];
options.notifications.forEach(notification => {
notifications.push(this.sendNotification(notification));
});
return Promise.resolve({
notifications: options.notifications.map(notification => { return { id: '' + notification.id }; })
});
}
getPending(): Promise<LocalNotificationPendingList> {
return Promise.resolve({
notifications: this.pending.map(localNotification => {
return {
id: '' + localNotification.id
};
})
});
}
registerActionTypes(_options: { types: LocalNotificationActionType[]; }): Promise<void> {
throw new Error('Method not implemented.');
}
cancel(pending: LocalNotificationPendingList): Promise<void> {
console.log('Cancel these', pending);
this.pending = this.pending.filter(
localNotification => !pending.notifications.find(ln => ln.id === '' + localNotification.id));
return Promise.resolve();
}
areEnabled(): Promise<LocalNotificationEnabledResult> {
return Promise.resolve({
value: Notification.permission === 'granted'
});
}
requestPermission(): Promise<NotificationPermissionResponse> {
return new Promise((resolve) => {
Notification.requestPermission((result) => {
let granted = true;
if (result === 'denied' || result === 'default') {
granted = false;
}
resolve({ granted });
});
});
}
requestPermissions(): Promise<PermissionsRequestResult> {
return new Promise((resolve, reject) => {
Notification.requestPermission((result) => {
if (result === 'denied' || result === 'default') {
reject(result);
return;
}
resolve({
results: [result]
});
});
});
}
}
const LocalNotifications = new LocalNotificationsPluginWeb();
export { LocalNotifications };