-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathCTPushPrimerManager.m
171 lines (152 loc) · 7.9 KB
/
CTPushPrimerManager.m
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
//
// CTPushPrimerManager.m
// CleverTapSDK
//
// Created by Akash Malhotra on 04/07/23.
// Copyright © 2023 CleverTap. All rights reserved.
//
#import "CTPushPrimerManager.h"
#import "CTUtils.h"
#import "CTUIUtils.h"
#import "CTConstants.h"
#import "CTLocalInApp.h"
#import "CTInAppDisplayManager.h"
@interface CTPushPrimerManager ()
@property (nonatomic, strong) CleverTapInstanceConfig *config;
@property (nonatomic, strong) CTDispatchQueueManager *dispatchQueueManager;
@end
@implementation CTPushPrimerManager
@synthesize pushPermissionDelegate=_pushPermissionDelegate;
#if !CLEVERTAP_NO_INAPP_SUPPORT
- (instancetype)initWithConfig:(CleverTapInstanceConfig *)config inAppDisplayManager:(CTInAppDisplayManager*)inAppDisplayManagerObj dispatchQueueManager:(CTDispatchQueueManager*)dispatchQueueManager {
if ((self = [super init])) {
self.config = config;
inAppDisplayManager = inAppDisplayManagerObj;
self.dispatchQueueManager = dispatchQueueManager;
}
return self;
}
- (void)setPushPermissionDelegate:(id<CleverTapPushPermissionDelegate>)delegate {
if ([CTUIUtils runningInsideAppExtension]){
CleverTapLogDebug(self.config.logLevel, @"%@: setPushPermissionDelegate is a no-op in an app extension.", self);
return;
}
if (delegate && [delegate conformsToProtocol:@protocol(CleverTapPushPermissionDelegate)]) {
_pushPermissionDelegate = delegate;
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: CleverTap Push Permission Delegate does not conform to the CleverTapPushPermissionDelegate protocol", self);
}
}
- (id<CleverTapPushPermissionDelegate>)pushPermissionDelegate {
return _pushPermissionDelegate;
}
- (void)promptPushPrimer:(NSDictionary *_Nonnull)json {
if (@available(iOS 10.0, *)) {
[self getNotificationPermissionStatusWithCompletionHandler:^(UNAuthorizationStatus status) {
if (status == UNAuthorizationStatusNotDetermined || status == UNAuthorizationStatusDenied) {
[self->inAppDisplayManager prepareNotificationForDisplay:json];
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Push Notification permission is already granted.", self);
}
}];
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Push Notification is avaliable from iOS v10.0 or later", self);
}
}
- (void)promptForPushPermission:(BOOL)isFallbackToSettings {
[self promptForOSPushNotificationWithFallbackToSettings:isFallbackToSettings
andSkipSettingsAlert:NO];
}
- (void)getNotificationPermissionStatusWithCompletionHandler:(void (^)(UNAuthorizationStatus))completion {
if (@available(iOS 10.0, *)) {
[self.dispatchQueueManager runSerialAsync:^{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings* settings) {
completion(settings.authorizationStatus);
}];
}];
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Push Notification is avaliable from iOS v10.0 or later", self);
completion(UNAuthorizationStatusDenied);
}
}
- (void)notifyPushPermissionResponse:(BOOL)accepted {
CleverTapLogInternal(self.config.logLevel, @"%@: Push Permission Response: %s", self, (accepted ? "accepted" : "denied"));
if (self.pushPermissionDelegate && [self.pushPermissionDelegate respondsToSelector:@selector(onPushPermissionResponse:)]) {
[self.pushPermissionDelegate onPushPermissionResponse:accepted];
}
}
- (void)promptForOSPushNotificationWithFallbackToSettings:(BOOL)isFallbackToSettings
andSkipSettingsAlert:(BOOL)skipSettingsAlert {
if (@available(iOS 10.0, *)) {
[self.dispatchQueueManager runSerialAsync:^{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings* settings) {
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
[self notifyPushPermissionResponse:YES];
} else {
[self notifyPushPermissionResponse:NO];
}
if (!error) {
[CTUtils runSyncMainQueue: ^{
UIApplication *sharedApplication = [CTUIUtils getSharedApplication];
if (sharedApplication == nil) {
return;
}
[sharedApplication registerForRemoteNotifications];
}];
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Error in request authorization for remote notification: %@", self, error);
}
}];
} else if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
if (isFallbackToSettings) {
if (skipSettingsAlert) {
[self openAppSettingsForPushNotification];
} else {
[self showFallbackToSettingsAlertDialog];
}
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Notification permission is denied. Please grant notification permission access in your app's settings to send notifications.", self);
}
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Push Notification permission is already granted.", self);
}
}];
}];
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Push Notification is avaliable from iOS v10.0 or later", self);
}
}
- (void)showFallbackToSettingsAlertDialog {
NSString *alertTitle = @"Permission Not Available";
NSString *alertMessage = @"You have previously denied notification permission. Please go to settings to enable notifications.";
NSString *positiveBtnText = @"Settings";
NSString *negativeBtntext = @"Cancel";
CTLocalInApp *localInAppBuilder = [[CTLocalInApp alloc] initWithInAppType:ALERT
titleText:alertTitle
messageText:alertMessage
followDeviceOrientation:YES
positiveBtnText:positiveBtnText
negativeBtnText:negativeBtntext];
[localInAppBuilder setFallbackToSettings:YES];
[localInAppBuilder setSkipSettingsAlert:YES];
NSMutableDictionary *alertSettings = [NSMutableDictionary dictionaryWithDictionary:localInAppBuilder.getLocalInAppSettings];
// Update isPushSettingsSoftAlert key as it is internal alert in-app, so that local in-app count will not increase.
alertSettings[@"isPushSettingsSoftAlert"] = @1;
[inAppDisplayManager prepareNotificationForDisplay:alertSettings];
}
- (void)openAppSettingsForPushNotification {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if (!url) {
CleverTapLogDebug(self.config.logLevel, @"%@: Unable to retrieve URL from OpenSettingsURL string", self);
return;
}
[CTUtils runSyncMainQueue:^{
[CTUIUtils openURL:url forModule:@"PushPermission"];
}];
}
#endif
@end