diff --git a/src/sw/serviceWorker/ServiceWorker.ts b/src/sw/serviceWorker/ServiceWorker.ts index 320c8d274..3d0990f26 100755 --- a/src/sw/serviceWorker/ServiceWorker.ts +++ b/src/sw/serviceWorker/ServiceWorker.ts @@ -117,9 +117,8 @@ export class ServiceWorker { static run() { self.addEventListener('activate', ServiceWorker.onServiceWorkerActivated); self.addEventListener('push', ServiceWorker.onPushReceived); - self.addEventListener( - 'notificationclose', - ServiceWorker.onNotificationClosed, + self.addEventListener('notificationclose', (event: NotificationEvent) => + event.waitUntil(ServiceWorker.onNotificationClosed(event)), ); self.addEventListener('notificationclick', (event: NotificationEvent) => event.waitUntil(ServiceWorker.onNotificationClicked(event)), @@ -392,8 +391,11 @@ export class ServiceWorker { event, ) .catch((e) => Log.error(e)); + const pushSubscriptionId = + await ServiceWorker.getPushSubscriptionId(); ServiceWorker.webhookNotificationEventSender.willDisplay( notif, + pushSubscriptionId, ); return ServiceWorker.displayNotification(notif) @@ -794,7 +796,7 @@ export class ServiceWorker { * Occurs when a notification is dismissed by the user (clicking the 'X') or all notifications are cleared. * Supported on: Chrome 50+ only */ - static onNotificationClosed(event) { + static async onNotificationClosed(event: NotificationEvent) { Log.debug( `Called onNotificationClosed(${JSON.stringify(event, null, 4)}):`, event, @@ -804,8 +806,10 @@ export class ServiceWorker { ServiceWorker.workerMessenger .broadcast(WorkerMessengerCommand.NotificationDismissed, notification) .catch((e) => Log.error(e)); - event.waitUntil( - ServiceWorker.webhookNotificationEventSender.dismiss(notification), + const pushSubscriptionId = await ServiceWorker.getPushSubscriptionId(); + ServiceWorker.webhookNotificationEventSender.dismiss( + notification, + pushSubscriptionId, ); } @@ -1066,6 +1070,7 @@ export class ServiceWorker { await ServiceWorker.webhookNotificationEventSender.click( notificationClickEvent, + pushSubscriptionId, ); if (onesignalRestPromise) await onesignalRestPromise; } diff --git a/src/sw/webhooks/notifications/OSWebhookNotificationEventSender.ts b/src/sw/webhooks/notifications/OSWebhookNotificationEventSender.ts index 78a7b2a97..c243542fd 100644 --- a/src/sw/webhooks/notifications/OSWebhookNotificationEventSender.ts +++ b/src/sw/webhooks/notifications/OSWebhookNotificationEventSender.ts @@ -11,19 +11,30 @@ export class OSWebhookNotificationEventSender { private readonly sender: OSWebhookSender = new OSWebhookSender(), ) {} - async click(event: NotificationClickEvent): Promise { - return await this.sender.send(new OSWebhookPayloadNotificationClick(event)); + async click( + event: NotificationClickEvent, + subscriptionId: string | undefined, + ): Promise { + return await this.sender.send( + new OSWebhookPayloadNotificationClick(event, subscriptionId), + ); } - async willDisplay(notification: IOSNotification): Promise { + async willDisplay( + notification: IOSNotification, + subscriptionId: string | undefined, + ): Promise { return await this.sender.send( - new OSWebhookPayloadNotificationWillDisplay(notification), + new OSWebhookPayloadNotificationWillDisplay(notification, subscriptionId), ); } - async dismiss(notification: IOSNotification): Promise { + async dismiss( + notification: IOSNotification, + subscriptionId: string | undefined, + ): Promise { return await this.sender.send( - new OSWebhookPayloadNotificationDismiss(notification), + new OSWebhookPayloadNotificationDismiss(notification, subscriptionId), ); } } diff --git a/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationClick.ts b/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationClick.ts index d9082ff02..40a848261 100644 --- a/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationClick.ts +++ b/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationClick.ts @@ -10,8 +10,14 @@ export class OSWebhookPayloadNotificationClick readonly content: string; readonly additionalData?: object; readonly actionId?: string; + readonly url?: string; - constructor(notificationClickEvent: NotificationClickEvent) { + readonly subscriptionId?: string; + + constructor( + notificationClickEvent: NotificationClickEvent, + subscriptionId: string | undefined, + ) { const notification = notificationClickEvent.notification; this.notificationId = notification.notificationId; this.heading = notification.title; @@ -19,5 +25,8 @@ export class OSWebhookPayloadNotificationClick this.additionalData = notification.additionalData; this.actionId = notificationClickEvent.result.actionId; + this.url = notificationClickEvent.result.url; + + this.subscriptionId = subscriptionId; } } diff --git a/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationDismiss.ts b/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationDismiss.ts index 783f3c619..5ff13c6bd 100644 --- a/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationDismiss.ts +++ b/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationDismiss.ts @@ -10,11 +10,20 @@ export class OSWebhookPayloadNotificationDismiss readonly content: string; readonly additionalData?: object; readonly actionId?: string; + readonly url?: string; - constructor(notification: IOSNotification) { + readonly subscriptionId?: string; + + constructor( + notification: IOSNotification, + subscriptionId: string | undefined, + ) { this.notificationId = notification.notificationId; this.heading = notification.title; this.content = notification.body; this.additionalData = notification.additionalData; + this.url = notification.launchURL; + + this.subscriptionId = subscriptionId; } } diff --git a/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationWillDisplay.ts b/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationWillDisplay.ts index cb476fca8..3aeb7f2a9 100644 --- a/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationWillDisplay.ts +++ b/src/sw/webhooks/notifications/payloads/OSWebhookPayloadNotificationWillDisplay.ts @@ -10,11 +10,20 @@ export class OSWebhookPayloadNotificationWillDisplay readonly content: string; readonly additionalData?: object; readonly actionId?: string; + readonly url?: string; - constructor(notification: IOSNotification) { + readonly subscriptionId?: string; + + constructor( + notification: IOSNotification, + subscriptionId: string | undefined, + ) { this.notificationId = notification.notificationId; this.heading = notification.title; this.content = notification.body; this.additionalData = notification.additionalData; + this.url = notification.launchURL; + + this.subscriptionId = subscriptionId; } }