@@ -4,7 +4,7 @@ import { CLEAR_NOTIFICATIONS, clearAppBadge, setAppBadge } from '@/lib/badge'
4
4
import { ACTION_PORT , DELETE_SUBSCRIPTION , MESSAGE_PORT , STORE_OS , STORE_SUBSCRIPTION , SYNC_SUBSCRIPTION } from '@/components/serviceworker'
5
5
// import { getLogger } from '@/lib/logger'
6
6
7
- // we store existing push subscriptions to keep them in sync with server
7
+ // we store existing push subscriptions and OS to keep them in sync with server
8
8
const storage = new ServiceWorkerStorage ( 'sw:storage' , 1 )
9
9
10
10
// for communication between app and service worker
@@ -24,17 +24,19 @@ async function getOS () {
24
24
// current push notification count for badge purposes
25
25
let activeCount = 0
26
26
27
+ // message event listener for communication between app and service worker
27
28
const log = ( message , level = 'info' , context ) => {
28
29
messageChannelPort ?. postMessage ( { level, message, context } )
29
30
}
30
31
31
32
export function onPush ( sw ) {
32
33
return ( event ) => {
34
+ // in case of push notifications, make sure that the logger has an HTTPS endpoint
33
35
// const logger = getLogger('sw:push', ['onPush'])
34
36
let payload = event . data ?. json ( )
35
- if ( ! payload ) return
37
+ if ( ! payload ) return // ignore push events without payload, like isTrusted events
36
38
const { tag } = payload . options
37
- const nid = crypto . randomUUID ( )
39
+ const nid = crypto . randomUUID ( ) // notification id for tracking
38
40
39
41
// iOS requirement: group all promises
40
42
const promises = [ ]
@@ -43,8 +45,9 @@ export function onPush (sw) {
43
45
if ( immediatelyShowNotification ( tag ) ) {
44
46
// logger.info(`[${nid}] showing immediate notification with title: ${payload.title}`)
45
47
promises . push ( setAppBadge ( sw , ++ activeCount ) )
46
- } else { // Check if there are already notifications with the same tag and merge them
48
+ } else {
47
49
// logger.info(`[${nid}] checking for existing notification with tag ${tag}`)
50
+ // Check if there are already notifications with the same tag and merge them
48
51
promises . push ( sw . registration . getNotifications ( { tag } ) . then ( ( notifications ) => {
49
52
// logger.info(`[${nid}] found ${notifications.length} notifications with tag ${tag}`)
50
53
if ( notifications . length ) {
@@ -54,20 +57,22 @@ export function onPush (sw) {
54
57
} ) )
55
58
}
56
59
57
- // Apple requirement: wait for all promises to resolve
60
+ // iOS requirement: wait for all promises to resolve before showing the notification
58
61
event . waitUntil ( Promise . all ( promises ) . then ( ( ) => {
59
62
sw . registration . showNotification ( payload . title , payload . options )
60
63
} ) )
61
64
}
62
65
}
63
66
64
- // if there is no tag or it's a TIP, FORWARDEDTIP or EARN notification
65
- // we don't need to merge notifications and thus the notification should be immediately shown using `showNotification`
67
+ // if there is no tag or the tag is one of the following
68
+ // we show the notification immediately
66
69
const immediatelyShowNotification = ( tag ) =>
67
70
! tag || [ 'TIP' , 'FORWARDEDTIP' , 'EARN' , 'STREAK' , 'TERRITORY_TRANSFER' ] . includes ( tag . split ( '-' ) [ 0 ] )
68
71
72
+ // merge notifications with the same tag
69
73
const mergeNotification = ( event , sw , payload , currentNotifications , tag , nid ) => {
70
74
// const logger = getLogger('sw:push:mergeNotification', ['mergeNotification'])
75
+
71
76
// sanity check
72
77
const otherTagNotifications = currentNotifications . filter ( ( { tag : nTag } ) => nTag !== tag )
73
78
if ( otherTagNotifications . length > 0 ) {
0 commit comments