This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
BMSPushServiceWorker.js
135 lines (117 loc) · 4.23 KB
/
BMSPushServiceWorker.js
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
/*
Copyright 2016-17 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//import "bmsutils";
/**
* Push SDK class for handling the Web device requests
* @module BMSPushServiceWorker
*/
const regex = /{{\s*([^}]+)\s*}}/g;
var _pushVaribales = "";
function interpolate(messageData) {
return function interpolate(o) {
return messageData.replace(regex, function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
};
}
function createTemplateMessage(messageData) {
if (Object.keys(_pushVaribales).length > 0 ) {
var message = interpolate(messageData)(_pushVaribales);
return message;
} else {
return messageData;
}
}
/**
* Methos to show the notifications when an event arrives
* @method module:BMSPushServiceWorker#displayNotification
* @param {Object} event - The push event from browser
*/
function displayNotification(event) {
var messageJson = event.data.json();
var title = messageJson.title ? messageJson.title : "New message";
var imageUrl = messageJson.iconUrl ? messageJson.iconUrl : "images/icon.png";
var tagJson = messageJson.payload;
var tag = tagJson.tag ? tagJson.tag : "";
var bodyAlert = messageJson.alert ? messageJson.alert : "Example message";
var payloadData = messageJson.payload ? messageJson.payload : "Example message";
let messageTemp;
if ((messageTemp = regex.exec(bodyAlert)) !== null) {
bodyAlert = createTemplateMessage(bodyAlert);
}
self.registration.showNotification(title, {
body: bodyAlert,
icon: imageUrl,
data: payloadData,
tag: tag
});
return Promise.resolve();
}
function triggerSeenEvent(strMsg) {
sendMessageToAllClients("msgEventSeen:" + strMsg);
}
function triggerOpenEvent(strMsg) {
sendMessageToAllClients("msgEventOpen:" + strMsg);
}
function onPushNotificationReceived(event) {
console.log('Push notification received : ', event);
if (event.data) {
console.log('Event data is : ', event.data.text());
}
event.waitUntil(displayNotification(event).then(() => triggerSeenEvent(event.data.text())));
}
self.addEventListener('push', onPushNotificationReceived);
function sendMessageToClient(client, msg) {
return new Promise(function (resolve, reject) {
var msgChan = new MessageChannel();
msgChan.port1.onmessage = function (event) {
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data);
}
};
client.postMessage(msg, [msgChan.port2]);
});
}
function sendMessageToAllClients(msg) {
clients.matchAll().then(clients => {
clients.forEach(client => {
sendMessageToClient(client, msg);
});
});
}
self.addEventListener('install', function (event) {
self.skipWaiting();
console.log('Installed Service Worker : ', event);
//event.postMessage("SW Says 'Hello back!'");
});
self.addEventListener('message', function (event) {
replyPort = event.ports[0];
_pushVaribales = event.data;
});
self.addEventListener('activate', function (event) {
console.log('Activated Service Worker : ', event);
event.waitUntil(self.clients.claim());
});
self.addEventListener('notificationclick', function (event) {
console.log('Notification clicked with tag' + event.notification.tag + " and data " + event.notification.data);
let nidjson = event.notification.data;
event.notification.close();
event.waitUntil(triggerOpenEvent(nidjson));
});
self.addEventListener('pushsubscriptionchange', function () {
console.log('Push Subscription change');
sendMessageToAllClients("updateRegistration:");
});