You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+16-6Lines changed: 16 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -204,19 +204,22 @@ You'll also want to serve a `serviceWorker.mjs` file at the root of your server
204
204
self.addEventListener('push', function(event) {
205
205
constdata=event.data?.json() ?? {};
206
206
event.waitUntil((async () => {
207
+
constnotification= data?.notification?? {}
207
208
/// 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.
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
+
220
223
> [!NOTE]
221
224
> `.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.
/// If sent from a request, pass the request's logger here to maintain its metadata.
355
363
// logger: request.logger
@@ -367,6 +375,8 @@ do {
367
375
368
376
Your service worker will receive this message, decode it, and present it to the user.
369
377
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
+
370
380
> [!NOTE]
371
381
> Although the spec supports it, most browsers do not support silent notifications, and will drop a subscription if they are used.
Copy file name to clipboardExpand all lines: Sources/WebPush/Push Message/Notification.swift
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -122,6 +122,9 @@ extension PushMessage {
122
122
123
123
124
124
/// 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
+
///
125
128
/// - Parameters:
126
129
/// - kind: The kind of notification to send. Defaults to ``PushMessage/NotificationKind/declarative``.
127
130
/// - destination: The destination URL that should be opened when the user interacts with the notification.
/// 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
+
///
167
173
/// - Parameters:
168
174
/// - kind: The kind of notification to send. Defaults to ``PushMessage/NotificationKind/declarative``.
169
175
/// - destination: The destination URL that should be opened when the user interacts with the notification.
0 commit comments