Skip to content

Commit a04647d

Browse files
authored
fix: iOS check, proper AMOUNT count on iOS (#1796)
1 parent 34175ba commit a04647d

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
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: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ let actionChannelPort
1313

1414
// operating system. the value will be received via a STORE_OS message from app since service workers don't have access to window.navigator
1515
let os = ''
16-
const iOS = () => os === 'iOS'
16+
async function getOS () {
17+
if (!os) {
18+
os = await storage.getItem('os') || ''
19+
}
20+
return os
21+
}
1722

1823
// current push notification count for badge purposes
1924
let activeCount = 0
@@ -28,6 +33,7 @@ export function onPush (sw) {
2833
if (!payload) return
2934
const { tag } = payload.options
3035
event.waitUntil((async () => {
36+
const iOS = await getOS() === 'iOS'
3137
// generate random ID for every incoming push for better tracing in logs
3238
const nid = crypto.randomUUID()
3339
log(`[sw:push] ${nid} - received notification with tag ${tag}`)
@@ -56,7 +62,7 @@ export function onPush (sw) {
5662
// close them and then we display the notification.
5763
const notifications = await sw.registration.getNotifications({ tag })
5864
// we only close notifications manually on iOS because we don't want to degrade android UX just because iOS is behind in their support.
59-
if (iOS()) {
65+
if (iOS) {
6066
log(`[sw:push] ${nid} - closing existing notifications`)
6167
notifications.filter(({ tag: nTag }) => nTag === tag).forEach(n => n.close())
6268
}
@@ -84,7 +90,7 @@ export function onPush (sw) {
8490
// return null
8591
}
8692

87-
return await mergeAndShowNotification(sw, payload, notifications, tag, nid)
93+
return await mergeAndShowNotification(sw, payload, notifications, tag, nid, iOS)
8894
})())
8995
}
9096
}
@@ -94,7 +100,7 @@ export function onPush (sw) {
94100
const immediatelyShowNotification = (tag) =>
95101
!tag || ['TIP', 'FORWARDEDTIP', 'EARN', 'STREAK', 'TERRITORY_TRANSFER'].includes(tag.split('-')[0])
96102

97-
const mergeAndShowNotification = async (sw, payload, currentNotifications, tag, nid) => {
103+
const mergeAndShowNotification = async (sw, payload, currentNotifications, tag, nid, iOS) => {
98104
// sanity check
99105
const otherTagNotifications = currentNotifications.filter(({ tag: nTag }) => nTag !== tag)
100106
if (otherTagNotifications.length > 0) {
@@ -119,7 +125,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
119125
const SUM_SATS_TAGS = ['DEPOSIT', 'WITHDRAWAL']
120126
// this should reflect the amount of notifications that were already merged before
121127
let initialAmount = currentNotifications[0]?.data?.amount || 1
122-
if (iOS()) initialAmount = 1
128+
if (iOS) initialAmount = 1
123129
log(`[sw:push] ${nid} - initial amount: ${initialAmount}`)
124130
const mergedPayload = currentNotifications.reduce((acc, { data }) => {
125131
let newAmount, newSats
@@ -165,7 +171,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
165171

166172
// close all current notifications before showing new one to "merge" notifications
167173
// we only do this on iOS because we don't want to degrade android UX just because iOS is behind in their support.
168-
if (iOS()) {
174+
if (iOS) {
169175
log(`[sw:push] ${nid} - closing existing notifications`)
170176
currentNotifications.forEach(n => n.close())
171177
}
@@ -249,19 +255,21 @@ export function onPushSubscriptionChange (sw) {
249255
}
250256

251257
export function onMessage (sw) {
252-
return (event) => {
258+
return async (event) => {
253259
if (event.data.action === ACTION_PORT) {
254260
actionChannelPort = event.ports[0]
255261
return
256262
}
257263
if (event.data.action === STORE_OS) {
258-
os = event.data.os
264+
event.waitUntil(storage.setItem('os', event.data.os))
259265
return
260266
}
261267
if (event.data.action === MESSAGE_PORT) {
262268
messageChannelPort = event.ports[0]
263269
}
264270
log('[sw:message] received message', 'info', { action: event.data.action })
271+
const currentOS = await getOS()
272+
log('[sw:message] stored os: ' + currentOS, 'info', { action: event.data.action })
265273
if (event.data.action === STORE_SUBSCRIPTION) {
266274
log('[sw:message] storing subscription in IndexedDB', 'info', { endpoint: event.data.subscription.endpoint })
267275
return event.waitUntil(storage.setItem('subscription', { ...event.data.subscription, swVersion: 2 }))

0 commit comments

Comments
 (0)