Skip to content

Commit d00db6d

Browse files
authored
Merge pull request #1079 from OneSignal/fix/sw-click-and-confirm-delivery-calls
[User Model] [Fix] Notification Open & Confirm Delivery REST API calls failing
2 parents 92608b8 + e1547e3 commit d00db6d

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import EncodedModel from "../../core/caching/EncodedModel";
2+
import { ModelName } from "../../core/models/SupportedModels";
3+
import Database from "../../shared/services/Database";
4+
5+
/**
6+
* WARNING: This is a temp workaround for the ServiceWorker context only!
7+
* PURPOSE: CoreModuleDirector doesn't work in the SW context.
8+
* TODO: This is duplicated logic tech debt to address later
9+
*/
10+
export class ModelCacheDirectAccess {
11+
static async getPushSubscriptionIdByToken(token: string): Promise<string | undefined> {
12+
const pushSubscriptions = await Database.getAll<EncodedModel>(ModelName.PushSubscriptions);
13+
for(const pushSubscription of pushSubscriptions) {
14+
if (pushSubscription["token"] === token) {
15+
return pushSubscription["id"] as string;
16+
}
17+
}
18+
return undefined;
19+
}
20+
}

src/sw/serviceWorker/ServiceWorker.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { OSMinifiedNotificationPayload, OSMinifiedNotificationPayloadHelper } fr
3434
import { bowserCastle } from "../../shared/utils/bowserCastle";
3535
import { OSWebhookNotificationEventSender } from "../webhooks/notifications/OSWebhookNotificationEventSender";
3636
import { OSNotificationButtonsConverter } from "../models/OSNotificationButtonsConverter";
37+
import { ModelCacheDirectAccess } from "../helpers/ModelCacheDirectAccess";
3738

3839
declare 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

Comments
 (0)