NotificationServiceManager
public final class NotificationServiceManager : CustomDebugStringConvertible
An instance that facilitate the writing of a UNNotificationServiceExtension
extension.
This instance :
- stores the notification request and the content handler from the
didReceive(_:withContentHandler:)
method, - manages a counter of running asynchronous tasks started by that method.
It is designed to deliver the possibly modified notification to the user when:
- there is no more running asynchronous tasks,
- or the system is about to terminate the extension.
In order to do that:
- The
didReceive(_:withContentHandler:)
method should callNotificationServiceManager.wait()
or each asynchronous task it starts. - Asynchronous tasks should call
NotificationServiceManager.notify()
when they terminate. - The
didReceive(_:withContentHandler:)
should callNotificationServiceManager.complete(forces:)
method withfalse
argument before returning. - The
serviceExtensionTimeWillExpire()
method should call theNotificationServiceManager.complete(forces:)
method withtrue
argument to force the delivery of the notification.
This class is thread-safe.
-
The notification content, as a mutable instance.
This property is designed to update the
title
,subtitle
,body
… of the notification from thedidReceive(_:withContentHandler:)
method or from asynchronous tasks started by it.Declaration
Swift
public let mutableContent: UNMutableNotificationContent
-
Creates a new instance.
This instance is designed to be stored when deriving the
UNNotificationServiceExtension
class, and to be created in thedidReceive(_:withContentHandler:)
method.Declaration
Swift
public init?(request: UNNotificationRequest, contentHandler: @escaping (_ contentToDeliver: UNNotificationContent) -> Void)
Parameters
request
The original notification request that trigger the call of the extension.
contentHandler
The content handler closure to execute with the modified content. It takes the following parameter:
contentToDeliver
The notification content to deliver to the user.
-
Indicates that an asynchronous task is about to start.
This method is designed to be called before starting an asynchronous task.
If
complete()
has already been called, it is a no-operation. Otherwise, it increments the underlying counter.Declaration
Swift
public func wait()
-
Indicates that an asynchronous task has terminated.
This method is designed to be called at the end of the completion handler of an asynchronous task.
If the counter is equal to
0
before calling this method, it is a no-operation. Otherwise, it decrements the underlying counter. If the counter is then equal to0
(indicating that there is no other running asynchronous task) andcomplete(forces: false)
was previously called, the completion handler is called.Declaration
Swift
public func notify()
-
Asks to complete the processing of the notification request and to class the content handler.
If
complete()
has already been called, it is a no-operation. If the counter is equal to0
(indicating that there is no running asynchronous task) or ifforces
istrue
, the completion handler is called. Otherwise, the completion handler will be called by further calls tonotify()
as soon as the counter will be decremented to 0.Declaration
Swift
public func complete(forces: Bool)
Parameters
forces
Indicates whether the content handler must be called even when asynchronous tasks are still running.
-
Declaration
Swift
public var debugDescription: String { get }