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: Sources/WebPush/WebPushManager.swift
+30-16Lines changed: 30 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ public actor WebPushManager: Sendable {
57
57
/// - Note: On debug builds, this initializer will assert if VAPID authorization header expiration times are inconsistently set.
58
58
/// - Parameters:
59
59
/// - vapidConfiguration: The VAPID configuration to use when identifying the application server.
60
-
/// - backgroundActivityLogger: The logger to use for misconfiguration and background activity. By default, a print logger will be used, and if set to `nil`, a no-op logger will be used in release builds. When running in a server environment, your shared logger should be used instead giving you full control of logging.
60
+
/// - backgroundActivityLogger: The logger to use for misconfiguration and background activity. By default, a print logger will be used, and if set to `nil`, a no-op logger will be used in release builds. When running in a server environment, your shared logger should be used instead giving you full control of logging and metadata.
61
61
/// - eventLoopGroupProvider: The event loop to use for the internal HTTP client.
62
62
publicinit(
63
63
vapidConfiguration:VAPID.Configuration,
@@ -241,11 +241,13 @@ public actor WebPushManager: Sendable {
241
241
/// - subscriber: The subscriber to send the push message to.
242
242
/// - expiration: The expiration of the push message, after wich delivery will no longer be attempted.
243
243
/// - urgency: The urgency of the delivery of the push message.
244
+
/// - logger: The logger to use for status updates. If not provided, the background activity logger will be used instead. When running in a server environment, your contextual logger should be used instead giving you full control of logging and metadata.
244
245
publicfunc send(
245
246
data message:someDataProtocol,
246
247
to subscriber:someSubscriberProtocol,
247
248
expiration:Expiration=.recommendedMaximum,
248
-
urgency:Urgency=.high
249
+
urgency:Urgency=.high,
250
+
logger:Logger?=nil
249
251
)asyncthrows{
250
252
switch executor {
251
253
case.httpClient(let httpClient):
@@ -254,7 +256,8 @@ public actor WebPushManager: Sendable {
@@ -270,17 +273,20 @@ public actor WebPushManager: Sendable {
270
273
/// - subscriber: The subscriber to send the push message to.
271
274
/// - expiration: The expiration of the push message, after wich delivery will no longer be attempted.
272
275
/// - urgency: The urgency of the delivery of the push message.
276
+
/// - logger: The logger to use for status updates. If not provided, the background activity logger will be used instead. When running in a server environment, your contextual logger should be used instead giving you full control of logging and metadata.
273
277
publicfunc send(
274
278
string message:someStringProtocol,
275
279
to subscriber:someSubscriberProtocol,
276
280
expiration:Expiration=.recommendedMaximum,
277
-
urgency:Urgency=.high
281
+
urgency:Urgency=.high,
282
+
logger:Logger?=nil
278
283
)asyncthrows{
279
284
tryawaitrouteMessage(
280
285
message:.string(String(message)),
281
286
to: subscriber,
282
287
expiration: expiration,
283
-
urgency: urgency
288
+
urgency: urgency,
289
+
logger: logger ?? backgroundActivityLogger
284
290
)
285
291
}
286
292
@@ -293,17 +299,20 @@ public actor WebPushManager: Sendable {
293
299
/// - subscriber: The subscriber to send the push message to.
294
300
/// - expiration: The expiration of the push message, after wich delivery will no longer be attempted.
295
301
/// - urgency: The urgency of the delivery of the push message.
302
+
/// - logger: The logger to use for status updates. If not provided, the background activity logger will be used instead. When running in a server environment, your contextual logger should be used instead giving you full control of logging and metadata.
296
303
publicfunc send(
297
304
json message:someEncodable&Sendable,
298
305
to subscriber:someSubscriberProtocol,
299
306
expiration:Expiration=.recommendedMaximum,
300
-
urgency:Urgency=.high
307
+
urgency:Urgency=.high,
308
+
logger:Logger?=nil
301
309
)asyncthrows{
302
310
tryawaitrouteMessage(
303
311
message:.json(message),
304
312
to: subscriber,
305
313
expiration: expiration,
306
-
urgency: urgency
314
+
urgency: urgency,
315
+
logger: logger ?? backgroundActivityLogger
307
316
)
308
317
}
309
318
@@ -313,11 +322,13 @@ public actor WebPushManager: Sendable {
313
322
/// - subscriber: The subscriber to sign the message against.
314
323
/// - expiration: The expiration of the message.
315
324
/// - urgency: The urgency of the message.
325
+
/// - logger: The logger to use for status updates.
316
326
func routeMessage(
317
327
message:_Message,
318
328
to subscriber:someSubscriberProtocol,
319
329
expiration:Expiration,
320
-
urgency:Urgency
330
+
urgency:Urgency,
331
+
logger:Logger
321
332
)asyncthrows{
322
333
switch executor {
323
334
case.httpClient(let httpClient):
@@ -326,7 +337,8 @@ public actor WebPushManager: Sendable {
326
337
data: message.data,
327
338
subscriber: subscriber,
328
339
expiration: expiration,
329
-
urgency: urgency
340
+
urgency: urgency,
341
+
logger: logger
330
342
)
331
343
case.handler(let handler):
332
344
tryawaithandler(
@@ -345,16 +357,18 @@ public actor WebPushManager: Sendable {
345
357
/// - subscriber: The subscriber to sign the message against.
346
358
/// - expiration: The expiration of the message.
347
359
/// - urgency: The urgency of the message.
360
+
/// - logger: The logger to use for status updates.
backgroundActivityLogger.warning("Push message is longer than the maximum guarantee made by the spec: \(Self.maximumMessageSize) bytes. Sending this message may fail, and its size will be leaked despite being encrypted. Please consider sending less data to keep your communications secure.", metadata:["message":"\(message)"])
391
+
logger.warning("Push message is longer than the maximum guarantee made by the spec: \(Self.maximumMessageSize) bytes. Sending this message may fail, and its size will be leaked despite being encrypted. Please consider sending less data to keep your communications secure.", metadata:["message":"\(message)"])
378
392
}
379
393
380
394
/// Prepare the payload by padding it so the final message is 4KB.
@@ -424,9 +438,9 @@ public actor WebPushManager: Sendable {
backgroundActivityLogger.error("The message expiration must be greater than or equal to \(Expiration.dropIfUndeliverable) seconds.", metadata:["expiration":"\(expiration)"])
441
+
logger.error("The message expiration must be greater than or equal to \(Expiration.dropIfUndeliverable) seconds.", metadata:["expiration":"\(expiration)"])
backgroundActivityLogger.warning("The message expiration should be less than \(Expiration.recommendedMaximum) seconds.", metadata:["expiration":"\(expiration)"])
443
+
logger.warning("The message expiration should be less than \(Expiration.recommendedMaximum) seconds.", metadata:["expiration":"\(expiration)"])
430
444
}
431
445
432
446
/// Add the VAPID authorization and corrent content encoding and type.
@@ -440,7 +454,7 @@ public actor WebPushManager: Sendable {
0 commit comments