Я понял (почти), как это сделать на Android: ссылка. С другой стороны, я полный нуб в iOS. Вот мой код:
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)
/* added by me*/
let rawPayload = request.content.userInfo
print("NSE full raw payload: \(rawPayload)")
var additionalData: [AnyHashable: Any]? = nil
if let customData = rawPayload["custom"] as? [AnyHashable: Any] {
if let aData = customData["a"] as? [AnyHashable: Any] {
additionalData = aData
print("NSE: Found Additional Data: \(additionalData ?? [:])")
}
}
/* added by me*/
if let bestAttemptContent = bestAttemptContent {
/* DEBUGGING: Uncomment the 2 lines below to check this extension is executing
Note, this extension only runs when mutable-content is set
Setting an attachment or action buttons automatically adds this */
// print("Running NotificationServiceExtension")
// bestAttemptContent.body = "[Modified] " + bestAttemptContent.body
/* added by me */
print("Running NSE: "+bestAttemptContent.body)
/* added by me*/
OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
OneSignalExtension.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... ata-on-ios
Мобильная версия