@@ -34,41 +34,48 @@ self.addEventListener('push', event => {
3434 notificationVersion = parseInt(notificationData.version, 10);
3535 pushToken = notificationData.token;
3636 } catch {
37- self.registration.showNotification(event.data.text());
37+ event.waitUntil( self.registration.showNotification(event.data.text() ));
3838 return;
3939 }
4040
41- const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
42- const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10);
41+ event.waitUntil((async () => {
42+ const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
43+ const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10);
4344
44- // Force update if versions differ
45- if (assetsVersion !== notificationVersion) {
46- self.registration.update();
47- }
45+ // Force update if versions differ
46+ if (assetsVersion !== notificationVersion) {
47+ await self.registration.update();
48+ }
49+
50+ const formData = new FormData();
51+ formData.append('item_id', itemId.toString(10));
52+ formData.append('type_id', typeId.toString(10));
53+ formData.append('user_id', userId.toString(10));
54+ formData.append('token', pushToken);
4855
49- const formData = new FormData();
50- formData.append('item_id', itemId.toString(10));
51- formData.append('type_id', typeId.toString(10));
52- formData.append('user_id', userId.toString(10));
53- formData.append('token', pushToken);
56+ try {
57+ const response = await fetch(getNotificationUrl, {
58+ method: 'POST',
59+ headers: {
60+ 'X-Requested-With': 'XMLHttpRequest',
61+ },
62+ body: formData,
63+ });
5464
55- fetch(getNotificationUrl, {
56- method: 'POST',
57- headers: {
58- 'X-Requested-With': 'XMLHttpRequest',
59- },
60- body: formData,
61- })
62- .then(response => response.json())
63- .then(response => {
64- const responseBody = response.title + '\n' + response.text;
65+ const responseData = await response.json();
66+
67+ const responseBody = responseData.title + '\n' + responseData.text;
6568 const options = {
6669 body: responseBody,
67- data: response ,
68- icon: response .avatar.src,
70+ data: responseData ,
71+ icon: responseData .avatar.src,
6972 };
70- self.registration.showNotification(response.heading, options);
71- });
73+
74+ await self.registration.showNotification(responseData.heading, options);
75+ } catch (e) {
76+ console.error('Push error:', e);
77+ }
78+ })());
7279});
7380
7481/**
0 commit comments