forked from felipekian/push.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMobileChromeAgent.ts
More file actions
108 lines (100 loc) · 3.09 KB
/
Copy pathMobileChromeAgent.ts
File metadata and controls
108 lines (100 loc) · 3.09 KB
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
import { Util, Messages } from '@push/core';
import { AbstractAgent } from '@push/agents';
/**
* Notification agent for modern desktop browsers:
* Safari 6+, Firefox 22+, Chrome 22+, Opera 25+
*/
export default class MobileChromeAgent extends AbstractAgent {
/**
* Returns a boolean denoting support
* @returns {Boolean} boolean denoting whether webkit notifications are supported
*/
isSupported() {
return (
this.win.navigator !== undefined &&
this.win.navigator.serviceWorker !== undefined
);
}
/**
* Returns the function body as a string
* @param func
*/
getFunctionBody(func: Function) {
const str = func.toString().match(/function[^{]+{([\s\S]*)}$/);
return typeof str !== 'undefined' && str !== null && str.length > 1
? str[1]
: null;
}
/**
* Creates a new notification
* @param id ID of notification
* @param title Title of notification
* @param options Options object
* @param serviceWorker ServiceWorker path
* @param callback Callback function
*/
create(
id: number,
title: string,
options: PushOptions,
serviceWorker: string,
callback: Function
) {
/* Register ServiceWorker */
this.win.navigator.serviceWorker.register(serviceWorker);
this.win.navigator.serviceWorker.ready
.then((registration: any) => {
/* Local data the service worker will use */
let localData = {
id: id,
link: options.link,
origin: document.location.href,
onClick: Util.isFunction(options.onClick)
? this.getFunctionBody(options.onClick)
: '',
onClose: Util.isFunction(options.onClose)
? this.getFunctionBody(options.onClose)
: ''
};
/* Merge the local data with user-provided data */
if (options.data !== undefined && options.data !== null)
localData = {
...localData,
...options.data
};
/* Show the notification */
registration
.showNotification(title, {
icon: options.icon,
body: options.body,
vibrate: options.vibrate,
tag: options.tag,
data: localData,
requireInteraction: options.requireInteraction,
silent: options.silent
})
.then(() => {
registration.getNotifications().then((notifications: any[]) => {
/* Send an empty message so the ServiceWorker knows who the client is */
registration.active.postMessage('');
/* Trigger callback */
callback(notifications);
});
})
.catch(function(error: Error) {
throw new Error(
Messages.errors.sw_notification_error + error.message
);
});
})
.catch((error: Error) => {
throw new Error(Messages.errors.sw_registration_error + error.message);
});
}
/**
* Close all notification
*/
close() {
// Can't do this with service workers
}
}