Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Commit

Permalink
fix: updates to api
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Apr 13, 2024
1 parent b231b3c commit 9b9760f
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions ios/Push.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,41 @@ final public class Push: RCTEventEmitter {

}

enum NotificationKind {
case opened, foreground
case background((UIBackgroundFetchResult) -> Void)

var rawValue: String {
switch self {
case .foreground: "foreground"
case .opened: "opened"
case .background: "background"
}
}
}

extension Push {

@objc func handleRemoteNotificationReceived(
func handleRemoteNotificationReceived(
payload: [AnyHashable: Any],
kind: String,
completion: ((UIBackgroundFetchResult) -> Void)?
kind: NotificationKind
) {
guard shared.isObserving else {
let message = "Fatal: Not observing for kind: \(kind). \(#function)"
shared.logger?.track(event: message)
return
}
if kind == "foreground" {
switch kind {
case .foreground:
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "kind": kind])
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "kind": kind.rawValue])
} else {
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind). \(#function)")
}
} else if kind == "background" {
guard let completionHandler = completion else {
shared.logger?.track(event: "Fatal: Completion handler not found. \(#function)")
return
}
case .background(let completion):
let uuid = UUID().uuidString
shared.notificationCallbackDictionary[uuid] = {
completionHandler(.newData)
completion(.newData)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 29) { [weak self] in
if let callback = self?.shared.notificationCallbackDictionary[uuid] {
Expand All @@ -138,29 +147,29 @@ extension Push {
}
}
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "uuid": uuid, "kind": kind])
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "uuid": uuid, "kind": kind.rawValue])
} else {
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind). \(#function)")
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind.rawValue). \(#function)")
}
} else if kind == "opened" {
case .opened:
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "kind": kind])
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "kind": kind.rawValue])
} else {
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind). \(#function)")
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind.rawValue). \(#function)")
}
}

}

@objc func handleRemoteNotificationsRegistered(deviceToken: String) {
func handleRemoteNotificationsRegistered(deviceToken: String) {
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.deviceTokenReceived.rawValue, body: deviceToken)
} else {
shared.logger?.track(event: "Fatal: Emitter not found. \(#function)")
}
}

@objc func handleRemoteNotificationRegistrationError(error: String) {
func handleRemoteNotificationRegistrationError(error: String) {
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.errorReceived.rawValue, body: error)
} else {
Expand All @@ -177,7 +186,7 @@ extension Push: UNUserNotificationCenterDelegate {
return String(format: "%02x", data)
}
let token = tokenParts.joined()
shared.logger?.track(event: "Device token received \(#function).")
shared.logger?.track(event: "Device token received: \(token)")
handleRemoteNotificationsRegistered(deviceToken: token)
}

Expand All @@ -187,19 +196,18 @@ extension Push: UNUserNotificationCenterDelegate {
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
handleRemoteNotificationReceived(payload: notification.request.content.userInfo, kind: "foreground", completion: nil)
handleRemoteNotificationReceived(payload: notification.request.content.userInfo, kind: .foreground)
completionHandler([.banner, .sound, .badge, .list])
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
handleRemoteNotificationReceived(payload: response.notification.request.content.userInfo, kind: "opened", completion: nil)
handleRemoteNotificationReceived(payload: response.notification.request.content.userInfo, kind: .opened)
}

public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
handleRemoteNotificationReceived(
payload: userInfo,
kind: "background",
completion: completionHandler
kind: .background(completionHandler)
)
}

Expand Down

0 comments on commit 9b9760f

Please sign in to comment.