Skip to content

Commit 432b436

Browse files
committed
align onNotificationClick promises to event.waitUntil
1 parent efe2a01 commit 432b436

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

sw/eventListener.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const mergeNotification = (event, sw, payload, currentNotifications, tag, nid) =
9494
// logger.info(`[sw:push] ${nid} - initial amount: ${initialAmount}`)
9595
// logger.info(`[sw:push] ${nid} - initial sats: ${initialSats}`)
9696

97+
// currentNotifications.reduce causes iOS to sum n notifications + initialAmount which is already n notifications
9798
const mergedPayload = {
9899
...incomingData,
99100
amount: initialAmount + 1,
@@ -132,23 +133,26 @@ const mergeNotification = (event, sw, payload, currentNotifications, tag, nid) =
132133

133134
const options = { icon: payload.options?.icon, tag, data: { url: '/notifications', ...mergedPayload } }
134135
// logger.info(`[sw:push] ${nid} - show notification with title "${title}"`)
135-
return { title, options }
136+
return { title, options } // send the new, merged, payload
136137
}
137138

139+
// iOS-specific bug, notificationclick event only works when the app is closed
138140
export function onNotificationClick (sw) {
139141
return (event) => {
142+
const promises = []
140143
// const logger = getLogger('sw:onNotificationClick', ['onNotificationClick'])
141144
const url = event.notification.data?.url
142145
// logger.info(`[sw:onNotificationClick] clicked notification with url ${url}`)
143146
if (url) {
144-
event.waitUntil(sw.clients.openWindow(url))
147+
promises.push(sw.clients.openWindow(url))
145148
}
146149
activeCount = Math.max(0, activeCount - 1)
147150
if (activeCount === 0) {
148-
clearAppBadge(sw)
151+
promises.push(clearAppBadge(sw))
149152
} else {
150-
setAppBadge(sw, activeCount)
153+
promises.push(setAppBadge(sw, activeCount))
151154
}
155+
event.waitUntil(Promise.all(promises))
152156
event.notification.close()
153157
}
154158
}

0 commit comments

Comments
 (0)