Skip to content

Commit 5acd690

Browse files
committed
feat: add webpush
1 parent b632936 commit 5acd690

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
let jwtToken = '';
2+
let tokenIntervalRef;
3+
const refreshToken = (myToken) => {
4+
if (!!tokenIntervalRef) {
5+
clearInterval(tokenIntervalRef);
6+
}
7+
jwtToken = myToken;
8+
if (!!jwtToken && jwtToken.length > 10) {
9+
tokenIntervalRef = setInterval(() => {
10+
const requestOptions = {
11+
method: 'GET',
12+
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
13+
};
14+
fetch('/appuser/refreshtoken', requestOptions).then(response => response.json()).then(result => {
15+
if ((!result.Message && !!result.Token && result.Token.length > 10)) {
16+
//console.log('Token refreshed.');
17+
jwtToken = result.Token;
18+
/* eslint-disable-next-line no-restricted-globals */
19+
self.postMessage(result);
20+
}
21+
else {
22+
jwtToken = '';
23+
clearInterval(tokenIntervalRef);
24+
}
25+
});
26+
}, 45000);
27+
}
28+
};
29+
let firstNotRequest = true;
30+
let notificationIntervalRef;
31+
/* eslint-disable-next-line no-restricted-globals */
32+
self.addEventListener('message', (event) => {
33+
const msgData = event.data;
34+
refreshToken(msgData.jwtToken);
35+
if (!!notificationIntervalRef) {
36+
clearInterval(notificationIntervalRef);
37+
}
38+
notificationIntervalRef = setInterval(() => {
39+
if (!jwtToken) {
40+
clearInterval(notificationIntervalRef);
41+
firstNotRequest = true;
42+
}
43+
const requestOptions = {
44+
method: 'GET',
45+
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
46+
};
47+
/* eslint-disable-next-line no-restricted-globals */
48+
self.fetch(msgData.newNotificationUrl, requestOptions).then(result => result.json()).then(resultJson => {
49+
var _a, _b, _c, _d;
50+
if (!!resultJson && (resultJson === null || resultJson === void 0 ? void 0 : resultJson.length) > 0) {
51+
/* eslint-disable-next-line no-restricted-globals */
52+
self.postMessage(resultJson);
53+
//Notification
54+
//console.log(Notification.permission);
55+
if (Notification.permission === 'granted' && !firstNotRequest) {
56+
if ((resultJson === null || resultJson === void 0 ? void 0 : resultJson.length) > 0 && ((_b = (_a = resultJson[0]) === null || _a === void 0 ? void 0 : _a.Message) === null || _b === void 0 ? void 0 : _b.length) > 1 && ((_d = (_c = resultJson[0]) === null || _c === void 0 ? void 0 : _c.Title) === null || _d === void 0 ? void 0 : _d.length) > 1) {
57+
for (let value of resultJson) {
58+
new Notification(value === null || value === void 0 ? void 0 : value.Title, { body: value === null || value === void 0 ? void 0 : value.Message });
59+
}
60+
}
61+
}
62+
else if (!!firstNotRequest) {
63+
firstNotRequest = false;
64+
}
65+
}
66+
});
67+
}, 60000);
68+
});
69+
export {};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
- Copyright 2022 Sven Loesekann
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
/// <reference lib="webworker" />
14+
/* eslint-disable-next-line no-restricted-globals */
15+
declare var self: DedicatedWorkerGlobalScope;
16+
export { };
17+
18+
interface MsgData {
19+
jwtToken: string;
20+
newNotificationUrl: string;
21+
}
22+
23+
interface UserResponse {
24+
Token?: string
25+
Message?: string
26+
}
27+
28+
let jwtToken = '';
29+
let tokenIntervalRef: ReturnType<typeof setInterval>;
30+
const refreshToken = (myToken: string) => {
31+
if (!!tokenIntervalRef) {
32+
clearInterval(tokenIntervalRef);
33+
}
34+
jwtToken = myToken;
35+
if (!!jwtToken && jwtToken.length > 10) {
36+
tokenIntervalRef = setInterval(() => {
37+
const requestOptions = {
38+
method: 'GET',
39+
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
40+
};
41+
fetch('/appuser/refreshtoken', requestOptions).then(response => response.json() as UserResponse).then(result => {
42+
if ((!result.Message && !!result.Token && result.Token.length > 10)) {
43+
//console.log('Token refreshed.');
44+
jwtToken = result.Token;
45+
/* eslint-disable-next-line no-restricted-globals */
46+
self.postMessage(result);
47+
} else {
48+
jwtToken = '';
49+
clearInterval(tokenIntervalRef);
50+
}
51+
});
52+
}, 45000);
53+
}
54+
}
55+
56+
let firstNotRequest = true;
57+
let notificationIntervalRef: ReturnType<typeof setInterval>;
58+
/* eslint-disable-next-line no-restricted-globals */
59+
self.addEventListener('message', (event: MessageEvent) => {
60+
const msgData = event.data as MsgData;
61+
refreshToken(msgData.jwtToken);
62+
if (!!notificationIntervalRef) {
63+
clearInterval(notificationIntervalRef);
64+
}
65+
notificationIntervalRef = setInterval(() => {
66+
if (!jwtToken) {
67+
clearInterval(notificationIntervalRef);
68+
firstNotRequest = true;
69+
}
70+
const requestOptions = {
71+
method: 'GET',
72+
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
73+
};
74+
/* eslint-disable-next-line no-restricted-globals */
75+
self.fetch(msgData.newNotificationUrl, requestOptions).then(result => result.json()).then(resultJson => {
76+
if (!!resultJson && resultJson?.length > 0) {
77+
/* eslint-disable-next-line no-restricted-globals */
78+
self.postMessage(resultJson);
79+
//Notification
80+
//console.log(Notification.permission);
81+
if (Notification.permission === 'granted' && !firstNotRequest) {
82+
if(resultJson?.length > 0 && resultJson[0]?.Message?.length > 1 && resultJson[0]?.Title?.length > 1) {
83+
for(let value of resultJson) {
84+
new Notification(value?.Title, {body: value?.Message});
85+
}
86+
}
87+
} else if(!!firstNotRequest) {
88+
firstNotRequest = false;
89+
}
90+
}
91+
});
92+
}, 60000);
93+
});

0 commit comments

Comments
 (0)