Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] add subscriptionId and url to webhook payloads #1175

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading