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

Commit

Permalink
fix: crash fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Feb 11, 2024
1 parent dbcdd25 commit 5170e37
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions ios/Push.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import NotificationCenter
import React

@objc(Push)
public class Push: RCTEventEmitter {
final public class Push: RCTEventEmitter {

private var notificationCallbackDictionary: [String: () -> Void] = [:]
private var isInitialized = false
private var queue: [Action] = []

@objc public override init() {
super.init()
UNUserNotificationCenter.current().delegate = self
private static var isInitialized = false
@objc public static var emitter: RCTEventEmitter?
private static var queue: [Action] = []

override public init() {
super.init()
Self.emitter = self
}

enum NotificationType: String, CaseIterable {
Expand All @@ -23,30 +24,32 @@ public class Push: RCTEventEmitter {
let payload: Any!
}

private func sendStoreAction(_ action: Action) {
self.sendEvent(withName: action.type.rawValue, body: action.payload)
private static func sendStoreAction(_ action: Action) {
if let emitter = self.emitter {
emitter.sendEvent(withName: action.type.rawValue, body: action.payload)
}
}

@objc public func dispatch(type: String, payload: Any!) {
@objc public static func dispatch(type: String, payload: Any!) {
let actionObj = Action(type: .init(rawValue: type) ?? .errorReceived, payload: payload)
if isInitialized {
self.sendStoreAction(actionObj)
sendStoreAction(actionObj)
} else {
self.queue.append(actionObj)
queue.append(actionObj)
}
}

@objc public override func startObserving() {
isInitialized = true
for event in queue {
sendStoreAction(event)
}
queue = []
}

@objc public override func stopObserving() {
isInitialized = false
}
Self.isInitialized = true
for event in Self.queue {
Self.sendStoreAction(event)
}
Self.queue = []
}
@objc public override func stopObserving() {
Self.isInitialized = false
}

public override func supportedEvents() -> [String]! {
return NotificationType.allCases.map { $0.rawValue }
Expand Down Expand Up @@ -104,58 +107,58 @@ public class Push: RCTEventEmitter {

extension Push: UNUserNotificationCenterDelegate {

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02x", data)
}
let token = tokenParts.joined()
dispatch(type: NotificationType.deviceTokenReceived.rawValue, payload: token)
Self.dispatch(type: NotificationType.deviceTokenReceived.rawValue, payload: token)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
dispatch(
public func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Self.dispatch(
type: NotificationType.errorReceived.rawValue,
payload: error.localizedDescription
)
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let uuid = UUID().uuidString
dispatch(
Self.dispatch(
type: NotificationType.notificationReceived.rawValue,
payload: ["payload": notification.request.content.userInfo, "uuid": uuid, "kind": "foreground"]
)
notificationCallbackDictionary[uuid] = {
completionHandler([.badge, .banner, .sound, .list])
}
DispatchQueue.main.asyncAfter(deadline: .now() + 29) { [weak self] in
if let callback = self?.notificationCallbackDictionary[uuid] {
DispatchQueue.main.asyncAfter(deadline: .now() + 29) {
if let callback = self.notificationCallbackDictionary[uuid] {
callback()
self?.notificationCallbackDictionary.removeValue(forKey: uuid)
self.notificationCallbackDictionary.removeValue(forKey: uuid)
}
}
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
dispatch(
Self.dispatch(
type: NotificationType.notificationReceived.rawValue,
payload: ["payload": response.notification.request.content.userInfo, "kind": "opened"]
)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let uuid = UUID().uuidString
dispatch(
Self.dispatch(
type: NotificationType.notificationReceived.rawValue,
payload: ["payload": userInfo, "uuid": uuid, "kind": "background"]
)
notificationCallbackDictionary[uuid] = {
completionHandler(.newData)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 29) { [weak self] in
if let callback = self?.notificationCallbackDictionary[uuid] {
DispatchQueue.main.asyncAfter(deadline: .now() + 29) {
if let callback = self.notificationCallbackDictionary[uuid] {
callback()
self?.notificationCallbackDictionary.removeValue(forKey: uuid)
self.notificationCallbackDictionary.removeValue(forKey: uuid)
}
}
}
Expand Down

0 comments on commit 5170e37

Please sign in to comment.