@@ -11,11 +11,20 @@ const storage = new ServiceWorkerStorage('sw:storage', 1)
11
11
let messageChannelPort
12
12
let actionChannelPort
13
13
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
+
15
17
// operating system. the value will be received via a STORE_OS message from app since service workers don't have access to window.navigator
16
18
let os = ''
17
19
const iOS = ( ) => os === 'iOS'
18
20
21
+ async function getOS ( ) {
22
+ if ( ! os ) {
23
+ os = await storage . getItem ( 'os' ) || ''
24
+ }
25
+ return os
26
+ }
27
+
19
28
// current push notification count for badge purposes
20
29
let activeCount = 0
21
30
@@ -32,7 +41,7 @@ export function onPush (sw) {
32
41
// generate random ID for every incoming push for better tracing in logs
33
42
const nid = crypto . randomUUID ( )
34
43
log ( `[sw:push] ${ nid } - received notification with tag ${ tag } ` )
35
-
44
+ const currentOS = await getOS ( )
36
45
// due to missing proper tag support in Safari on iOS, we can't rely on the tag built-in filter.
37
46
// we therefore fetch all notifications with the same tag and manually filter them, too.
38
47
// see https://bugs.webkit.org/show_bug.cgi?id=258922
@@ -57,7 +66,7 @@ export function onPush (sw) {
57
66
// close them and then we display the notification.
58
67
const notifications = await sw . registration . getNotifications ( { tag } )
59
68
// 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' ) {
61
70
log ( `[sw:push] ${ nid } - closing existing notifications` )
62
71
notifications . filter ( ( { tag : nTag } ) => nTag === tag ) . forEach ( n => n . close ( ) )
63
72
}
@@ -85,7 +94,7 @@ export function onPush (sw) {
85
94
// return null
86
95
}
87
96
88
- return await mergeAndShowNotification ( sw , payload , notifications , tag , nid )
97
+ return await mergeAndShowNotification ( sw , payload , notifications , tag , nid , currentOS )
89
98
} ) ( ) )
90
99
}
91
100
}
@@ -95,7 +104,7 @@ export function onPush (sw) {
95
104
const immediatelyShowNotification = ( tag ) =>
96
105
! tag || [ 'TIP' , 'FORWARDEDTIP' , 'EARN' , 'STREAK' , 'TERRITORY_TRANSFER' ] . includes ( tag . split ( '-' ) [ 0 ] )
97
106
98
- const mergeAndShowNotification = async ( sw , payload , currentNotifications , tag , nid ) => {
107
+ const mergeAndShowNotification = async ( sw , payload , currentNotifications , tag , nid , currentOS ) => {
99
108
// sanity check
100
109
const otherTagNotifications = currentNotifications . filter ( ( { tag : nTag } ) => nTag !== tag )
101
110
if ( otherTagNotifications . length > 0 ) {
@@ -120,7 +129,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
120
129
const SUM_SATS_TAGS = [ 'DEPOSIT' , 'WITHDRAWAL' ]
121
130
// this should reflect the amount of notifications that were already merged before
122
131
let initialAmount = currentNotifications [ 0 ] ?. data ?. amount || 1
123
- if ( iOS ( ) ) initialAmount = 1
132
+ if ( currentOS === ' iOS' ) initialAmount = 1
124
133
log ( `[sw:push] ${ nid } - initial amount: ${ initialAmount } ` )
125
134
const mergedPayload = currentNotifications . reduce ( ( acc , { data } ) => {
126
135
let newAmount , newSats
@@ -132,7 +141,7 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
132
141
}
133
142
const newPayload = { ...data , amount : newAmount , sats : newSats }
134
143
return newPayload
135
- } , { ...incomingData , amount : 1 } ) // 1 is hard coded to test iOS scenario, should be initialValue
144
+ } , { ...incomingData , amount : initialAmount } )
136
145
137
146
log ( `[sw:push] ${ nid } - merged payload: ${ JSON . stringify ( mergedPayload ) } ` )
138
147
@@ -177,7 +186,6 @@ const mergeAndShowNotification = async (sw, payload, currentNotifications, tag,
177
186
}
178
187
179
188
// 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
181
189
182
190
export function onNotificationClick ( sw ) {
183
191
return ( event ) => {
@@ -253,19 +261,21 @@ export function onPushSubscriptionChange (sw) {
253
261
}
254
262
255
263
export function onMessage ( sw ) {
256
- return ( event ) => {
264
+ return async ( event ) => {
257
265
if ( event . data . action === ACTION_PORT ) {
258
266
actionChannelPort = event . ports [ 0 ]
259
267
return
260
268
}
261
269
if ( event . data . action === STORE_OS ) {
262
- os = event . data . os
270
+ event . waitUntil ( storage . setItem ( 'os' , event . data . os ) )
263
271
return
264
272
}
265
273
if ( event . data . action === MESSAGE_PORT ) {
266
274
messageChannelPort = event . ports [ 0 ]
267
275
}
268
276
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 } )
269
279
if ( event . data . action === STORE_SUBSCRIPTION ) {
270
280
log ( '[sw:message] storing subscription in IndexedDB' , 'info' , { endpoint : event . data . subscription . endpoint } )
271
281
return event . waitUntil ( storage . setItem ( 'subscription' , { ...event . data . subscription , swVersion : 2 } ) )
0 commit comments