-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUserNotificationCenterDelegateCacher.swift
122 lines (112 loc) · 5.08 KB
/
UserNotificationCenterDelegateCacher.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import Foundation
import UserNotifications
public class UserNotificationCenterDelegateCacher: NSObject, UNUserNotificationCenterDelegate {
private var didReceiveCache = [[String: Any]]()
private var willPresentCache = [[String: Any]]()
private var openSettingsCache = [[String: Any]]()
private var customDelegates = [any UNUserNotificationCenterDelegate]()
private var emarsysNotificationCenterDelegate: UNUserNotificationCenterDelegate?
private var caching = true
public static let instance = UserNotificationCenterDelegateCacher()
public func userNotificationCenter(
_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
if caching {
didReceiveCache.append([
"center": center,
"response": response,
"completionHandler": completionHandler,
])
}
if isEmarsysNotification(notification: response.notification) {
emarsysNotificationCenterDelegate?.userNotificationCenter?(
center, didReceive: response, withCompletionHandler: completionHandler)
} else {
customDelegates.forEach { delegate in
delegate.userNotificationCenter?(
center, didReceive: response, withCompletionHandler: completionHandler)
}
}
}
public func userNotificationCenter(
_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
if caching {
willPresentCache.append([
"center": center,
"notification": notification,
"completionHandler": completionHandler,
])
}
if isEmarsysNotification(notification: notification) {
emarsysNotificationCenterDelegate?.userNotificationCenter?(
center, willPresent: notification, withCompletionHandler: completionHandler)
} else {
customDelegates.forEach { delegate in
delegate.userNotificationCenter?(
center, willPresent: notification, withCompletionHandler: completionHandler)
}
}
}
public func userNotificationCenter(
_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?
) {
if caching {
var dict = [String: Any]()
dict["center"] = center
if let noti = notification {
dict["notification"] = noti
}
openSettingsCache.append(dict)
}
if isEmarsysNotification(notification: notification) {
if #available(iOS 12.0, *) {
emarsysNotificationCenterDelegate?.userNotificationCenter?(center, openSettingsFor: notification)
}
} else {
customDelegates.forEach { delegate in
if #available(iOS 12.0, *) {
delegate.userNotificationCenter?(center, openSettingsFor: notification)
}
}
}
}
public func addDelegate(notificationCenterDelegate: UNUserNotificationCenterDelegate) {
self.customDelegates.append(notificationCenterDelegate)
}
func emptyCache(with notificationCenterDelegate: UNUserNotificationCenterDelegate) {
self.emarsysNotificationCenterDelegate = notificationCenterDelegate
willPresentCache.forEach { cachedDict in
notificationCenterDelegate.userNotificationCenter?(
cachedDict["center"] as! UNUserNotificationCenter,
willPresent: cachedDict["notification"] as! UNNotification,
withCompletionHandler: cachedDict["completionHandler"]
as! (UNNotificationPresentationOptions) -> Void)
}
didReceiveCache.forEach { cachedDict in
notificationCenterDelegate.userNotificationCenter?(
cachedDict["center"] as! UNUserNotificationCenter,
didReceive: cachedDict["response"] as! UNNotificationResponse,
withCompletionHandler: cachedDict["completionHandler"] as! () -> Void)
}
if #available(iOS 12.0, *) {
openSettingsCache.forEach { cachedDict in
var notification: UNNotification? = nil
if let noti = cachedDict["notification"] as? UNNotification {
notification = noti
}
notificationCenterDelegate.userNotificationCenter?(
cachedDict["center"] as! UNUserNotificationCenter, openSettingsFor: notification)
}
openSettingsCache.removeAll()
}
willPresentCache.removeAll()
didReceiveCache.removeAll()
self.caching = false
}
private func isEmarsysNotification(notification: UNNotification?) -> Bool {
return ((notification?.request.content.userInfo.contains(where: { $0.key as! String == "ems"})) != nil)
}
}