@@ -34,6 +34,7 @@ import { OSMinifiedNotificationPayload, OSMinifiedNotificationPayloadHelper } fr
3434import { bowserCastle } from "../../shared/utils/bowserCastle" ;
3535import { OSWebhookNotificationEventSender } from "../webhooks/notifications/OSWebhookNotificationEventSender" ;
3636import { OSNotificationButtonsConverter } from "../models/OSNotificationButtonsConverter" ;
37+ import { ModelCacheDirectAccess } from "../helpers/ModelCacheDirectAccess" ;
3738
3839declare const self : ServiceWorkerGlobalScope & OSServiceWorkerFields ;
3940
@@ -80,6 +81,15 @@ export class ServiceWorker {
8081 return new OSWebhookNotificationEventSender ( ) ;
8182 }
8283
84+ static async getPushSubscriptionId ( ) : Promise < string | undefined > {
85+ const pushSubscription = await self . registration . pushManager . getSubscription ( ) ;
86+ const pushToken = pushSubscription ?. endpoint ;
87+ if ( ! pushToken ) {
88+ return undefined ;
89+ }
90+ return ModelCacheDirectAccess . getPushSubscriptionIdByToken ( pushToken ) ;
91+ }
92+
8393 /**
8494 * Allows message passing between this service worker and pages on the same domain.
8595 * Clients include any HTTPS site page, or the nested iFrame pointing to OneSignal on any HTTP site. This allows
@@ -302,7 +312,7 @@ export class ServiceWorker {
302312 return ;
303313
304314 const appId = await ServiceWorker . getAppId ( ) ;
305- const { deviceId } = await Database . getSubscription ( ) ;
315+ const pushSubscriptionId = await this . getPushSubscriptionId ( ) ;
306316
307317 // app and notification ids are required, decided to exclude deviceId from required params
308318 // In rare case we don't have it we can still report as confirmed to backend to increment count
@@ -314,7 +324,7 @@ export class ServiceWorker {
314324 // JSON.stringify() does not include undefined values
315325 // Our response will not contain those fields here which have undefined values
316326 const postData = {
317- player_id : deviceId ,
327+ player_id : pushSubscriptionId ,
318328 app_id : appId ,
319329 device_type : DeviceRecord . prototype . getDeliveryPlatform ( )
320330 } ;
@@ -728,8 +738,8 @@ export class ServiceWorker {
728738
729739 // Start making REST API requests BEFORE self.clients.openWindow is called.
730740 // It will cause the service worker to stop on Chrome for Android when site is added to the home screen.
731- const { deviceId } = await Database . getSubscription ( ) ;
732- const convertedAPIRequests = ServiceWorker . sendConvertedAPIRequests ( appId , deviceId , notificationClickEvent , deviceType ) ;
741+ const pushSubscriptionId = await this . getPushSubscriptionId ( ) ;
742+ const convertedAPIRequests = ServiceWorker . sendConvertedAPIRequests ( appId , pushSubscriptionId , notificationClickEvent , deviceType ) ;
733743
734744 /*
735745 Check if we can focus on an existing tab instead of opening a new url.
@@ -846,7 +856,7 @@ export class ServiceWorker {
846856 */
847857 static async sendConvertedAPIRequests (
848858 appId : string | undefined | null ,
849- deviceId : string | undefined ,
859+ pushSubscriptionId : string | undefined ,
850860 notificationClickEvent : NotificationClickEventInternal ,
851861 deviceType : DeliveryPlatformKind ,
852862 ) : Promise < void > {
@@ -862,7 +872,7 @@ export class ServiceWorker {
862872 if ( appId ) {
863873 onesignalRestPromise = OneSignalApiBase . put ( `notifications/${ notificationData . notificationId } ` , {
864874 app_id : appId ,
865- player_id : deviceId ,
875+ player_id : pushSubscriptionId ,
866876 opened : true ,
867877 device_type : deviceType
868878 } ) ;
0 commit comments