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:

This class is thread-safe.

Stored Properties

  • The notification content, as a mutable instance.

    This property is designed to update the title, subtitle, body… of the notification from the didReceive(_:withContentHandler:) method or from asynchronous tasks started by it.

    Declaration

    Swift

    public let mutableContent: UNMutableNotificationContent

Initializers

  • Creates a new instance.

    This instance is designed to be stored when deriving the UNNotificationServiceExtension class, and to be created in the didReceive(_: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.

API

  • 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 to 0 (indicating that there is no other running asynchronous task) and complete(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 to 0 (indicating that there is no running asynchronous task) or if forces is true, the completion handler is called. Otherwise, the completion handler will be called by further calls to notify() 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.

CustomDebugStringConvertible Protocol

  • Declaration

    Swift

    public var debugDescription: String { get }