Skip to content

Commit 944a761

Browse files
Merge pull request #344 from Iterable/bug/mob-1795-crash
[MOB-1795] - Check notificationsEnabled synchronously
1 parent 63cb7db commit 944a761

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,12 @@ final class IterableAPIInternal: NSObject, PushTrackerProtocol, AuthProvider {
101101
return
102102
}
103103

104-
// check notificationsEnabled then call register with enabled/not-not enabled
105-
notificationStateProvider.notificationsEnabled.onSuccess { enabled in
106-
self.register(token: token,
107-
appName: appName,
108-
pushServicePlatform: self.config.pushPlatform,
109-
notificationsEnabled: enabled,
110-
onSuccess: onSuccess,
111-
onFailure: onFailure)
112-
}.onError { _ in
113-
self.register(token: token,
114-
appName: appName,
115-
pushServicePlatform: self.config.pushPlatform,
116-
notificationsEnabled: false,
117-
onSuccess: onSuccess,
118-
onFailure: onFailure)
119-
}
104+
self.register(token: token,
105+
appName: appName,
106+
pushServicePlatform: self.config.pushPlatform,
107+
notificationsEnabled: notificationStateProvider.notificationsEnabled,
108+
onSuccess: onSuccess,
109+
onFailure: onFailure)
120110
}
121111

122112
@discardableResult private func register(token: Data,

swift-sdk/Internal/IterableAppIntegrationInternal.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,35 @@ import UserNotifications
99

1010
// Returns whether notifications are enabled
1111
protocol NotificationStateProviderProtocol {
12-
var notificationsEnabled: Promise<Bool, Error> { get }
12+
var notificationsEnabled: Bool { get }
1313

1414
func registerForRemoteNotifications()
1515
}
1616

1717
struct SystemNotificationStateProvider: NotificationStateProviderProtocol {
18-
var notificationsEnabled: Promise<Bool, Error> {
19-
let result = Promise<Bool, Error>()
20-
18+
var notificationsEnabled: Bool {
2119
if #available(iOS 10.0, *) {
22-
UNUserNotificationCenter.current().getNotificationSettings { settings in
23-
if settings.authorizationStatus == .authorized {
24-
result.resolve(with: true)
25-
} else {
26-
result.resolve(with: false)
20+
var notificationSettings: UNNotificationSettings?
21+
let semasphore = DispatchSemaphore(value: 0)
22+
23+
DispatchQueue.global().async {
24+
UNUserNotificationCenter.current().getNotificationSettings { setttings in
25+
notificationSettings = setttings
26+
semasphore.signal()
2727
}
2828
}
29+
30+
semasphore.wait()
31+
guard let authorizationStatus = notificationSettings?.authorizationStatus else { return false }
32+
return authorizationStatus == .authorized
2933
} else {
3034
// Fallback on earlier versions
3135
if let currentSettings = UIApplication.shared.currentUserNotificationSettings, currentSettings.types != [] {
32-
result.resolve(with: true)
36+
return true
3337
} else {
34-
result.resolve(with: false)
38+
return false
3539
}
3640
}
37-
38-
return result
3941
}
4042

4143
func registerForRemoteNotifications() {

tests/swift-sdk-swift-tests/Mocks.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ import XCTest
1111

1212
// Note: This is used only by swift tests. So can't put this in Common
1313
class MockNotificationStateProvider: NotificationStateProviderProtocol {
14-
var notificationsEnabled: Promise<Bool, Error> {
15-
let promise = Promise<Bool, Error>()
16-
DispatchQueue.main.async {
17-
promise.resolve(with: self.enabled)
18-
}
19-
return promise
14+
var notificationsEnabled: Bool {
15+
return enabled
2016
}
2117

2218
func registerForRemoteNotifications() {

0 commit comments

Comments
 (0)