-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOneSignalNotificationSettings.m
More file actions
204 lines (163 loc) · 8.85 KB
/
Copy pathOneSignalNotificationSettings.m
File metadata and controls
204 lines (163 loc) · 8.85 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "OneSignalNotificationSettings.h"
#import <Foundation/Foundation.h>
#import <UserNotifications/UserNotifications.h>
#import "OneSignalCommonDefines.h"
#import "OSNotificationsManager.h"
#import <OneSignalCore/OSDeviceUtils.h>
#import "OSMacros.h"
#import <OneSignalCore/OneSignalCoreHelper.h>
@interface OneSignalNotificationSettings ()
// Used as both an optimization and to prevent queue deadlocks.
@property (atomic) BOOL useCachedStatus;
@end
@implementation OneSignalNotificationSettings
// Used to run all calls to getNotificationSettingsWithCompletionHandler sequentially
// This prevents any possible deadlocks due to race condiditions.
static dispatch_queue_t serialQueue;
+(dispatch_queue_t)getQueue {
return serialQueue;
}
- (instancetype)init {
serialQueue = dispatch_queue_create("com.onesignal.notification.settings.ios10", DISPATCH_QUEUE_SERIAL);
return [super init];
}
- (void)getNotificationPermissionState:(void (^)(OSPermissionStateInternal *subscriptionStatus))completionHandler {
if (self.useCachedStatus) {
completionHandler(OSNotificationsManager.currentPermissionState);
return;
}
// NOTE1: Never call currentUserNotificationSettings from the callback below! It will lock the main thread.
// NOTE2: Apple runs the callback on a background serial queue
dispatch_async(serialQueue, ^{
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings* settings) {
OSPermissionStateInternal* status = OSNotificationsManager.currentPermissionState;
//to avoid 'undeclared identifier' errors in older versions of Xcode,
//we do not directly reference UNAuthorizationStatusProvisional (which is only available in iOS 12/Xcode 10
UNAuthorizationStatus provisionalStatus = (UNAuthorizationStatus)3;
status.answeredPrompt = settings.authorizationStatus != UNAuthorizationStatusNotDetermined && settings.authorizationStatus != provisionalStatus;
status.provisional = (settings.authorizationStatus == 3);
status.accepted = settings.authorizationStatus == UNAuthorizationStatusAuthorized && !status.provisional;
if (@available(iOS 14.0, *)) {
status.ephemeral = (settings.authorizationStatus == AUTH_STATUS_EPHEMERAL);
status.accepted = status.accepted || status.ephemeral;
} else {
status.ephemeral = false;
}
status.notificationTypes = (settings.badgeSetting == UNNotificationSettingEnabled ? 1 : 0)
+ (settings.soundSetting == UNNotificationSettingEnabled ? 2 : 0)
+ (settings.alertSetting == UNNotificationSettingEnabled ? 4 : 0)
+ (settings.lockScreenSetting == UNNotificationSettingEnabled ? 8 : 0);
// check if using provisional notifications
if ([OSDeviceUtils isIOSVersionGreaterThanOrEqual:@"12.0"] && settings.authorizationStatus == provisionalStatus)
status.notificationTypes += PROVISIONAL_UNAUTHORIZATIONOPTION;
// also check if 'deliver quietly' is enabled.
if ([OSDeviceUtils isIOSVersionGreaterThanOrEqual:@"10.0"] && settings.notificationCenterSetting == UNNotificationSettingEnabled)
status.notificationTypes += 16;
self.useCachedStatus = true;
completionHandler(status);
self.useCachedStatus = false;
}];
});
}
// used only in cases where UNUserNotificationCenter getNotificationSettingsWith...
// callback does not get executed in a timely fashion. Rather than returning nil,
- (OSPermissionStateInternal *)defaultPermissionState {
if (OSNotificationsManager.currentPermissionState != nil) {
return OSNotificationsManager.currentPermissionState;
}
OSPermissionStateInternal *defaultState = [OSPermissionStateInternal new];
defaultState.notificationTypes = 0;
return defaultState;
}
- (OSPermissionStateInternal*)getNotificationPermissionState {
if (self.useCachedStatus)
return OSNotificationsManager.currentPermissionState;
__block OSPermissionStateInternal* returnState = OSNotificationsManager.currentPermissionState;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_sync(serialQueue, ^{
[self getNotificationPermissionState:^(OSPermissionStateInternal *state) {
returnState = state;
dispatch_semaphore_signal(semaphore);
}];
});
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(100 * NSEC_PER_MSEC)));
return returnState ?: self.defaultPermissionState;
}
- (int)getNotificationTypes {
return [self getNotificationPermissionState].notificationTypes;
}
// Prompt then run updateNotificationTypes on the main thread with the response.
// FUTURE: Add a 2nd seloctor with 'withOptions' for UNAuthorizationOptions*'s
- (void)promptForNotifications:(void(^)(BOOL accepted))completionHandler {
id responseBlock = ^(BOOL granted, NSError* error) {
// Run callback on main / UI thread
[OneSignalCoreHelper dispatch_async_on_main_queue: ^{ // OneSignalCoreHelper.dispatch_async_on_main_queue ??
OSNotificationsManager.currentPermissionState.provisional = false;
OSNotificationsManager.currentPermissionState.accepted = granted;
OSNotificationsManager.currentPermissionState.answeredPrompt = true;
[OSNotificationsManager updateNotificationTypes: granted ? 15 : 0];
if (completionHandler)
completionHandler(granted);
}];
};
UNAuthorizationOptions options = (UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge);
if ([OSDeviceUtils isIOSVersionGreaterThanOrEqual:@"12.0"] && [OSNotificationsManager providesAppNotificationSettings]) {
options += PROVIDES_SETTINGS_UNAUTHORIZATIONOPTION;
}
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:options completionHandler:responseBlock];
[OSNotificationsManager registerForAPNsToken];
}
- (void)registerForProvisionalAuthorization:(OSUserResponseBlock)block {
if ([OSDeviceUtils isIOSVersionLessThan:@"12.0"]) {
return;
}
OSPermissionStateInternal *state = [self getNotificationPermissionState];
//don't register for provisional if the user has already accepted the prompt
if (state.status != OSNotificationPermissionNotDetermined || state.answeredPrompt) {
if (block)
block(true);
return;
}
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
let options = PROVISIONAL_UNAUTHORIZATIONOPTION + DEFAULT_UNAUTHORIZATIONOPTIONS;
id responseBlock = ^(BOOL granted, NSError *error) {
[OneSignalCoreHelper dispatch_async_on_main_queue:^{ // OneSignalCoreHelper.dispatch_async_on_main_queue ??
OSNotificationsManager.currentPermissionState.provisional = true;
[OSNotificationsManager updateNotificationTypes: options];
if (block)
block(granted);
}];
};
[center requestAuthorizationWithOptions:options completionHandler:responseBlock];
}
// Ignore these 2 events, promptForNotifications: already takes care of these.
// Only iOS 9
- (void)onNotificationPromptResponse:(int)notificationTypes { }
@end