Skip to content

Commit

Permalink
Merge pull request #1175 from OneSignal/fix/webhook-payload-missing-f…
Browse files Browse the repository at this point in the history
…ields

[Fix] add subscriptionId and url to webhook payloads
  • Loading branch information
jkasten2 authored May 2, 2024
2 parents 4472be3 + 0332ee1 commit 79d3c4b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
17 changes: 11 additions & 6 deletions src/sw/serviceWorker/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -1066,6 +1070,7 @@ export class ServiceWorker {

await ServiceWorker.webhookNotificationEventSender.click(
notificationClickEvent,
pushSubscriptionId,
);
if (onesignalRestPromise) await onesignalRestPromise;
}
Expand Down
23 changes: 17 additions & 6 deletions src/sw/webhooks/notifications/OSWebhookNotificationEventSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,30 @@ export class OSWebhookNotificationEventSender {
private readonly sender: OSWebhookSender = new OSWebhookSender(),
) {}

async click(event: NotificationClickEvent): Promise<void> {
return await this.sender.send(new OSWebhookPayloadNotificationClick(event));
async click(
event: NotificationClickEvent,
subscriptionId: string | undefined,
): Promise<void> {
return await this.sender.send(
new OSWebhookPayloadNotificationClick(event, subscriptionId),
);
}

async willDisplay(notification: IOSNotification): Promise<void> {
async willDisplay(
notification: IOSNotification,
subscriptionId: string | undefined,
): Promise<void> {
return await this.sender.send(
new OSWebhookPayloadNotificationWillDisplay(notification),
new OSWebhookPayloadNotificationWillDisplay(notification, subscriptionId),
);
}

async dismiss(notification: IOSNotification): Promise<void> {
async dismiss(
notification: IOSNotification,
subscriptionId: string | undefined,
): Promise<void> {
return await this.sender.send(
new OSWebhookPayloadNotificationDismiss(notification),
new OSWebhookPayloadNotificationDismiss(notification, subscriptionId),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ 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;
this.content = notification.body;
this.additionalData = notification.additionalData;

this.actionId = notificationClickEvent.result.actionId;
this.url = notificationClickEvent.result.url;

this.subscriptionId = subscriptionId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 79d3c4b

Please sign in to comment.