The Notification Service Extension allows OneSignal to process notifications (such as rich media or badges) before they are displayed.
1. NotificationService.swift
Create this file within the OneSignalNotificationServiceExtension/ directory. It uses OneSignalExtension to handle the notification lifecycle.
2. Info.plist
Ensure the extension has a valid Info.plist with the correct NSExtensionPointIdentifier set to com.apple.usernotifications.service.
3. Entitlements
The OneSignalNotificationServiceExtension.entitlements file must contain an App Group that matches the one defined in your main Runner.entitlements.
// OneSignalNotificationServiceExtension/NotificationService.swift
import UserNotifications
import OneSignalExtension
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var receivedRequest: UNNotificationRequest!
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.receivedRequest = request
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
OneSignalExtension.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
}