Skip to content

Commit cbbb7c7

Browse files
authored
Merge pull request #113 from iMattPro/fix-safari (thanks to teebling)
Fix Safari terminating after 3 pushes
2 parents 364e6eb + caca5d1 commit cbbb7c7

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

styles/all/template/push_worker.js.twig

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)