@@ -13,7 +13,12 @@ let actionChannelPort
13
13
14
14
// operating system. the value will be received via a STORE_OS message from app since service workers don't have access to window.navigator
15
15
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
+ }
17
22
18
23
// current push notification count for badge purposes
19
24
let activeCount = 0
@@ -28,6 +33,7 @@ export function onPush (sw) {
28
33
if ( ! payload ) return
29
34
const { tag } = payload . options
30
35
event . waitUntil ( ( async ( ) => {
36
+ const iOS = await getOS ( ) === 'iOS'
31
37
// generate random ID for every incoming push for better tracing in logs
32
38
const nid = crypto . randomUUID ( )
33
39
log ( `[sw:push] ${ nid } - received notification with tag ${ tag } ` )
@@ -56,7 +62,7 @@ export function onPush (sw) {
56
62
// close them and then we display the notification.
57
63
const notifications = await sw . registration . getNotifications ( { tag } )
58
64
// 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 ) {
60
66
log ( `[sw:push] ${ nid } - closing existing notifications` )
61
67
notifications . filter ( ( { tag : nTag } ) => nTag === tag ) . forEach ( n => n . close ( ) )
62
68
}
@@ -84,7 +90,7 @@ export function onPush (sw) {
84
90
// return null
85
91
}
86
92
87
- return await mergeAndShowNotification ( sw , payload , notifications , tag , nid )
93
+ return await mergeAndShowNotification ( sw , payload , notifications , tag , nid , iOS )
88
94
} ) ( ) )
89
95
}
90
96
}
@@ -94,7 +100,7 @@ export function onPush (sw) {
94
100
const immediatelyShowNotification = ( tag ) =>
95
101
! tag || [ 'TIP' , 'FORWARDEDTIP' , 'EARN' , 'STREAK' , 'TERRITORY_TRANSFER' ] . includes ( tag . split ( '-' ) [ 0 ] )
96
102
97
- const mergeAndShowNotification = async ( sw , payload , currentNotifications , tag , nid ) => {
103
+ const mergeAndShowNotification = async ( sw , payload , currentNotifications , tag , nid , iOS ) => {
98
104
// sanity check
99
105
const otherTagNotifications = currentNotifications . filter ( ( { tag : nTag } ) => nTag !== tag )
100
106
if ( otherTagNotifications . length > 0 ) {
@@ -119,7 +125,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
119
125
const SUM_SATS_TAGS = [ 'DEPOSIT' , 'WITHDRAWAL' ]
120
126
// this should reflect the amount of notifications that were already merged before
121
127
let initialAmount = currentNotifications [ 0 ] ?. data ?. amount || 1
122
- if ( iOS ( ) ) initialAmount = 1
128
+ if ( iOS ) initialAmount = 1
123
129
log ( `[sw:push] ${ nid } - initial amount: ${ initialAmount } ` )
124
130
const mergedPayload = currentNotifications . reduce ( ( acc , { data } ) => {
125
131
let newAmount , newSats
@@ -165,7 +171,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
165
171
166
172
// close all current notifications before showing new one to "merge" notifications
167
173
// 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 ) {
169
175
log ( `[sw:push] ${ nid } - closing existing notifications` )
170
176
currentNotifications . forEach ( n => n . close ( ) )
171
177
}
@@ -249,19 +255,21 @@ export function onPushSubscriptionChange (sw) {
249
255
}
250
256
251
257
export function onMessage ( sw ) {
252
- return ( event ) => {
258
+ return async ( event ) => {
253
259
if ( event . data . action === ACTION_PORT ) {
254
260
actionChannelPort = event . ports [ 0 ]
255
261
return
256
262
}
257
263
if ( event . data . action === STORE_OS ) {
258
- os = event . data . os
264
+ event . waitUntil ( storage . setItem ( 'os' , event . data . os ) )
259
265
return
260
266
}
261
267
if ( event . data . action === MESSAGE_PORT ) {
262
268
messageChannelPort = event . ports [ 0 ]
263
269
}
264
270
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 } )
265
273
if ( event . data . action === STORE_SUBSCRIPTION ) {
266
274
log ( '[sw:message] storing subscription in IndexedDB' , 'info' , { endpoint : event . data . subscription . endpoint } )
267
275
return event . waitUntil ( storage . setItem ( 'subscription' , { ...event . data . subscription , swVersion : 2 } ) )
0 commit comments