Skip to content

Commit 21487fa

Browse files
committed
fix onPush can't read STORE_OS from onMessage
1 parent 1166f17 commit 21487fa

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

components/serviceworker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ export const ServiceWorkerProvider = ({ children }) => {
158158
// since (a lot of) browsers don't support the pushsubscriptionchange event,
159159
// we sync with server manually by checking on every page reload if the push subscription changed.
160160
// see https://medium.com/@madridserginho/how-to-handle-webpush-api-pushsubscriptionchange-event-in-modern-browsers-6e47840d756f
161+
navigator?.serviceWorker?.controller?.postMessage?.({ action: STORE_OS, os: detectOS() })
162+
logger.info('sent STORE_OS to service worker: ', detectOS())
161163
navigator?.serviceWorker?.controller?.postMessage?.({ action: SYNC_SUBSCRIPTION })
162164
logger.info('sent SYNC_SUBSCRIPTION to service worker')
163-
navigator?.serviceWorker?.controller?.postMessage?.({ action: STORE_OS, os: detectOS() })
164165
}, [registration, permission.notification])
165166

166167
const contextValue = useMemo(() => ({

sw/eventListener.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ const storage = new ServiceWorkerStorage('sw:storage', 1)
1111
let messageChannelPort
1212
let actionChannelPort
1313

14-
// TODO 756.2 Fix iOS check, initialValue isn't updated if iOS is true
14+
// DONE 756.2 Fix iOS check, initialValue isn't updated if iOS is true
15+
// FIX: store os in ServiceWorkerStorage and update it on every STORE_OS message
16+
1517
// operating system. the value will be received via a STORE_OS message from app since service workers don't have access to window.navigator
1618
let os = ''
1719
const iOS = () => os === 'iOS'
1820

21+
async function getOS () {
22+
if (!os) {
23+
os = await storage.getItem('os') || ''
24+
}
25+
return os
26+
}
27+
1928
// current push notification count for badge purposes
2029
let activeCount = 0
2130

@@ -32,7 +41,7 @@ export function onPush (sw) {
3241
// generate random ID for every incoming push for better tracing in logs
3342
const nid = crypto.randomUUID()
3443
log(`[sw:push] ${nid} - received notification with tag ${tag}`)
35-
44+
const currentOS = await getOS()
3645
// due to missing proper tag support in Safari on iOS, we can't rely on the tag built-in filter.
3746
// we therefore fetch all notifications with the same tag and manually filter them, too.
3847
// see https://bugs.webkit.org/show_bug.cgi?id=258922
@@ -57,7 +66,7 @@ export function onPush (sw) {
5766
// close them and then we display the notification.
5867
const notifications = await sw.registration.getNotifications({ tag })
5968
// we only close notifications manually on iOS because we don't want to degrade android UX just because iOS is behind in their support.
60-
if (iOS()) {
69+
if (currentOS === 'iOS') {
6170
log(`[sw:push] ${nid} - closing existing notifications`)
6271
notifications.filter(({ tag: nTag }) => nTag === tag).forEach(n => n.close())
6372
}
@@ -85,7 +94,7 @@ export function onPush (sw) {
8594
// return null
8695
}
8796

88-
return await mergeAndShowNotification(sw, payload, notifications, tag, nid)
97+
return await mergeAndShowNotification(sw, payload, notifications, tag, nid, currentOS)
8998
})())
9099
}
91100
}
@@ -95,7 +104,7 @@ export function onPush (sw) {
95104
const immediatelyShowNotification = (tag) =>
96105
!tag || ['TIP', 'FORWARDEDTIP', 'EARN', 'STREAK', 'TERRITORY_TRANSFER'].includes(tag.split('-')[0])
97106

98-
const mergeAndShowNotification = async (sw, payload, currentNotifications, tag, nid) => {
107+
const mergeAndShowNotification = async (sw, payload, currentNotifications, tag, nid, currentOS) => {
99108
// sanity check
100109
const otherTagNotifications = currentNotifications.filter(({ tag: nTag }) => nTag !== tag)
101110
if (otherTagNotifications.length > 0) {
@@ -120,7 +129,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
120129
const SUM_SATS_TAGS = ['DEPOSIT', 'WITHDRAWAL']
121130
// this should reflect the amount of notifications that were already merged before
122131
let initialAmount = currentNotifications[0]?.data?.amount || 1
123-
if (iOS()) initialAmount = 1
132+
if (currentOS === 'iOS') initialAmount = 1
124133
log(`[sw:push] ${nid} - initial amount: ${initialAmount}`)
125134
const mergedPayload = currentNotifications.reduce((acc, { data }) => {
126135
let newAmount, newSats
@@ -132,7 +141,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
132141
}
133142
const newPayload = { ...data, amount: newAmount, sats: newSats }
134143
return newPayload
135-
}, { ...incomingData, amount: 1 }) // 1 is hard coded to test iOS scenario, should be initialValue
144+
}, { ...incomingData, amount: initialAmount })
136145

137146
log(`[sw:push] ${nid} - merged payload: ${JSON.stringify(mergedPayload)}`)
138147

@@ -177,7 +186,6 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
177186
}
178187

179188
// TODO 756.3 sometimes you get sent to very old threads, this is because the notification is not updated
180-
// PROPOSAL close all notifications with the same tag
181189

182190
export function onNotificationClick (sw) {
183191
return (event) => {
@@ -253,19 +261,21 @@ export function onPushSubscriptionChange (sw) {
253261
}
254262

255263
export function onMessage (sw) {
256-
return (event) => {
264+
return async (event) => {
257265
if (event.data.action === ACTION_PORT) {
258266
actionChannelPort = event.ports[0]
259267
return
260268
}
261269
if (event.data.action === STORE_OS) {
262-
os = event.data.os
270+
event.waitUntil(storage.setItem('os', event.data.os))
263271
return
264272
}
265273
if (event.data.action === MESSAGE_PORT) {
266274
messageChannelPort = event.ports[0]
267275
}
268276
log('[sw:message] received message', 'info', { action: event.data.action })
277+
const currentOS = await getOS()
278+
log('[sw:message] stored os: ' + currentOS, 'info', { action: event.data.action })
269279
if (event.data.action === STORE_SUBSCRIPTION) {
270280
log('[sw:message] storing subscription in IndexedDB', 'info', { endpoint: event.data.subscription.endpoint })
271281
return event.waitUntil(storage.setItem('subscription', { ...event.data.subscription, swVersion: 2 }))

0 commit comments

Comments
 (0)