Skip to content

Commit caef21e

Browse files
Updated examples to use declarative push notifications
Fixes #71
1 parent 0860eef commit caef21e

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,22 @@ You'll also want to serve a `serviceWorker.mjs` file at the root of your server
204204
self.addEventListener('push', function(event) {
205205
const data = event.data?.json() ?? {};
206206
event.waitUntil((async () => {
207+
const notification = data?.notification ?? {}
207208
/// Try parsing the data, otherwise use fallback content. DO NOT skip sending the notification, as you must display one for every push message that is received or your subscription will be dropped.
208-
let title = data?.title ?? "Your App Name";
209-
const body = data?.body ?? "New Content Available!";
209+
const title = notification.title ?? "Your App Name";
210+
const body = notification.body ?? "New Content Available!";
210211

211212
await self.registration.showNotification(title, {
212-
body,
213-
icon: "/notification-icon.png", /// Only some browsers support this.
214-
data
213+
body,
214+
requireInteraction: notification.require_interaction ?? false,
215+
...notification,
215216
});
216217
})());
217218
});
218219
```
219220
221+
In the example above, we are using the new **Declarative Push Notification** format for our message payload so the browser can automatically skip requiring the service worker, but you can send send any data payload and interpret it in your service worker should you choose. Note that doing so will require more resources on your user's devices when Declarative Push Notifications are otherwise supported.
222+
220223
> [!NOTE]
221224
> `.mjs` here allows your code to import other js modules as needed. If you are not using Vapor, please make sure your server uses the correct mime type for this file extension.
222225
@@ -349,7 +352,12 @@ import WebPush
349352

350353
do {
351354
try await manager.send(
352-
json: ["title": "Test Notification", "body": "Hello, World!"],
355+
/// We use a declarative push notification to allow newer browsers to deliver the notification to users on our behalf.
356+
notification: PushMessage.Notification(
357+
destination: URL(string: "https://example.com/notificationDetails")!,
358+
title: "Test Notification",
359+
body: "Hello, World!"
360+
),
353361
to: subscriber
354362
/// If sent from a request, pass the request's logger here to maintain its metadata.
355363
// logger: request.logger
@@ -367,6 +375,8 @@ do {
367375
368376
Your service worker will receive this message, decode it, and present it to the user.
369377
378+
You can also send JSON (`send(json: ...)`), data (`send(data: ...)`), or text (`send(string: ...)`) and have your service worker interpret the payload as it sees fit.
379+
370380
> [!NOTE]
371381
> Although the spec supports it, most browsers do not support silent notifications, and will drop a subscription if they are used.
372382

Sources/WebPush/Push Message/Notification.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ extension PushMessage {
122122

123123

124124
/// Initialize a new Declarative Push Notification.
125+
///
126+
/// - Important: The entire notification must fit within ``WebPushManager/maximumMessageSize`` once encoded, or sending the notification will fail. Keep this in mind when specifying data to be sent along with the notification.
127+
///
125128
/// - Parameters:
126129
/// - kind: The kind of notification to send. Defaults to ``PushMessage/NotificationKind/declarative``.
127130
/// - destination: The destination URL that should be opened when the user interacts with the notification.
@@ -164,6 +167,9 @@ extension PushMessage {
164167

165168
extension PushMessage.Notification where Contents == Never {
166169
/// Initialize a new Declarative Push Notification.
170+
///
171+
/// - Important: The entire notification must fit within ``WebPushManager/maximumMessageSize`` once encoded, or sending the notification will fail. Keep this in mind when specifying data to be sent along with the notification.
172+
///
167173
/// - Parameters:
168174
/// - kind: The kind of notification to send. Defaults to ``PushMessage/NotificationKind/declarative``.
169175
/// - destination: The destination URL that should be opened when the user interacts with the notification.

Sources/WebPush/Push Message/PushMessage.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
//
88

99
/// Common Push Message types.
10+
///
11+
/// Currently, only ``PushMessage/Notification`` is defined.
1012
public enum PushMessage: Sendable {}

0 commit comments

Comments
 (0)