Skip to content

Commit

Permalink
Added delegate that handles rich push from another provider (#625)
Browse files Browse the repository at this point in the history
* Adding delegate to NotificationServiceExtension to handle multiple providers

* Added error handling

---------

Co-authored-by: Jay Kim <jay@nullavenue.com>
  • Loading branch information
christinaschell and roninopf authored Apr 20, 2023
1 parent 6d9b979 commit 33d88c9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions notification-extension/ITBNotificationServiceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,37 @@

import UserNotifications

public protocol ITBNotificationServiceExtensionDelegate {
func notificationServiceDidReceive(_ request: UNNotificationRequest, bestAttemptContent: UNMutableNotificationContent, contentHandler: @escaping (UNNotificationContent) -> Void)
}

@objc open class ITBNotificationServiceExtension: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
public static var itblNotificationDelegate: ITBNotificationServiceExtensionDelegate?

@objc override open func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

if let itblNotificationDelegate = ITBNotificationServiceExtension.itblNotificationDelegate,
let bestAttemptContent = bestAttemptContent {

let result = request.content.userInfo.contains {
guard let itbl = $0.key as? String else {
print("Iterable SDK notification service extension: failed to cast JSON key to string")
return false
}
return itbl == JsonKey.Payload.metadata
}

if !result {
itblNotificationDelegate.notificationServiceDidReceive(request, bestAttemptContent: bestAttemptContent, contentHandler: contentHandler)
return
}
}

resolveCategory(from: request.content)

retrieveAttachment(from: request.content)
Expand Down

0 comments on commit 33d88c9

Please sign in to comment.