-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(web-ui): send notification subscription to backend (#894)
* chore(web-ui): send notification subscription to backend * refactor(web-ui): use vapid key from backend * feat(api): add endpoint to save subscription --------- Co-authored-by: Johan Book <{ID}+{username}@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...re/notifications/application/contracts/commands/save-notification-subscription.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
IsNumber, | ||
IsOptional, | ||
IsString, | ||
IsUrl, | ||
ValidateNested, | ||
} from "class-validator"; | ||
|
||
import { NotificationSubscriptionKeysDetails } from "../dtos/notification-subscription-keys.dto"; | ||
|
||
export class SaveNotificationSubscriptionCommand { | ||
@IsUrl() | ||
@IsString() | ||
endpoint?: string; | ||
|
||
@IsOptional() | ||
@IsNumber() | ||
expirationTime?: number | null; | ||
|
||
@ValidateNested() | ||
keys?: NotificationSubscriptionKeysDetails; | ||
} |
9 changes: 9 additions & 0 deletions
9
...i/src/core/notifications/application/contracts/dtos/notification-subscription-keys.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IsString } from "class-validator"; | ||
|
||
export class NotificationSubscriptionKeysDetails { | ||
@IsString() | ||
auth!: string; | ||
|
||
@IsString() | ||
p256dh!: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { notificationsApi } from "./apis"; | ||
|
||
const SERVICE_WORKER_PATH = "service-worker.js"; | ||
|
||
export async function registerServiceWorker() { | ||
const registration = await navigator.serviceWorker.register( | ||
SERVICE_WORKER_PATH | ||
); | ||
|
||
let subscription = await registration.pushManager.getSubscription(); | ||
|
||
if (!subscription) { | ||
await Notification.requestPermission(); | ||
|
||
const vapid = await notificationsApi.getVapid(); | ||
|
||
subscription = await registration.pushManager.subscribe({ | ||
userVisibleOnly: true, | ||
applicationServerKey: vapid.key, | ||
}); | ||
} | ||
|
||
const saveNotificationSubscriptionCommand = subscription.toJSON(); | ||
|
||
await notificationsApi.saveSubscription({ | ||
// @ts-expect-error // TODO: Fix this type | ||
saveNotificationSubscriptionCommand, | ||
}); | ||
} |