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

Commit

Permalink
fix: add queue incase no observers
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Apr 13, 2024
1 parent 824d880 commit e18694f
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions ios/Push.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ public protocol Logger {
func track(event: String)
}

struct NotificationPayload {
let kind: NotificationKind
let userInfo: [AnyHashable: Any]
}

public class SharedPush: NSObject {
static var sharedInstance: SharedPush = .init()
var isObserving: Bool = false
var notificationCallbackDictionary: [String: () -> Void] = [:]
public var emitter: RCTEventEmitter?
public var logger: Logger?
var queue: [NotificationPayload] = []
}

@objc(Push)
Expand All @@ -34,6 +40,10 @@ final public class Push: RCTEventEmitter {
@objc public override func startObserving() {
super.startObserving()
shared.isObserving = true
if !shared.queue.isEmpty {
shared.queue.forEach(handleRemoteNotificationReceived(payload:))
shared.queue.removeAll()
}
shared.logger?.track(event: #function)
}

Expand Down Expand Up @@ -119,18 +129,21 @@ enum NotificationKind {
extension Push {

func handleRemoteNotificationReceived(
payload: [AnyHashable: Any],
kind: NotificationKind
payload: NotificationPayload
) {
guard shared.isObserving else {
let message = "Fatal: Not observing for kind: \(kind.rawValue). \(#function)"
let message = "Warning: Not observing for kind: \(payload.kind.rawValue). \(#function). Adding to the queue."
shared.queue.append(payload)
shared.logger?.track(event: message)
return
}
let kind = payload.kind
let userInfo = payload.userInfo

switch kind {
case .foreground:
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "kind": kind.rawValue])
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": userInfo, "kind": kind.rawValue])
} else {
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind). \(#function)")
}
Expand All @@ -147,21 +160,21 @@ extension Push {
}
}
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "uuid": uuid, "kind": kind.rawValue])
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": userInfo, "uuid": uuid, "kind": kind.rawValue])
} else {
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind.rawValue). \(#function)")
}
case .opened:
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": payload, "kind": kind.rawValue])
emitter.sendEvent(withName: NotificationType.notificationReceived.rawValue, body: ["payload": userInfo, "kind": kind.rawValue])
} else {
shared.logger?.track(event: "Fatal: Emitter not found for kind: \(kind.rawValue). \(#function)")
}
}

}

func handleRemoteNotificationsRegistered(deviceToken: String) {
func handleRemoteNotificationsRegistered(deviceToken: String) {
if let emitter = shared.emitter {
emitter.sendEvent(withName: NotificationType.deviceTokenReceived.rawValue, body: deviceToken)
} else {
Expand Down Expand Up @@ -196,18 +209,23 @@ extension Push: UNUserNotificationCenterDelegate {
}

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

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

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

Expand Down

0 comments on commit e18694f

Please sign in to comment.