-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOSNotificationsManager.m
More file actions
1009 lines (806 loc) · 40.9 KB
/
OSNotificationsManager.m
File metadata and controls
1009 lines (806 loc) · 40.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Modified MIT License
Copyright 2022 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 "OSNotificationsManager.h"
#import <OneSignalCore/OneSignalCore.h>
#import "OSMacros.h"
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
#import "OSNotification+OneSignal.h"
#import <OneSignalExtension/OneSignalAttachmentHandler.h>
#import "OneSignalWebViewManager.h"
#import "UNUserNotificationCenter+OneSignalNotifications.h"
#import "UIApplicationDelegate+OneSignalNotifications.h"
#import <OneSignalOutcomes/OSSessionManager.h>
@implementation OSNotificationClickEvent
@synthesize notification = _notification, result = _result;
- (id)initWithNotification:(OSNotification*)notification result:(OSNotificationClickResult*)result {
self = [super init];
if(self) {
_notification = notification;
_result = result;
}
return self;
}
- (NSString*)stringify {
NSError * err;
NSDictionary *jsonDictionary = [self jsonRepresentation];
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&err];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
// Convert the class into a NSDictionary
- (NSDictionary *_Nonnull)jsonRepresentation {
NSError * jsonError = nil;
NSData *objectData = [[self.notification stringify] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *notifDict = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
NSMutableDictionary* obj = [NSMutableDictionary new];
NSMutableDictionary* result = [NSMutableDictionary new];
[result setObject:self.result.actionId forKeyedSubscript:@"actionID"];
[result setObject:self.result.url forKeyedSubscript:@"url"];
[obj setObject:result forKeyedSubscript:@"result"];
[obj setObject:notifDict forKeyedSubscript:@"notification"];
return obj;
}
@end
@implementation OSNotificationClickResult
@synthesize url = _url, actionId = _actionId;
-(id)initWithUrl:(NSString*)url :(NSString*)actionID {
self = [super init];
if(self) {
_url = url;
_actionId = actionID;
}
return self;
}
@end
@interface OSDisplayableNotification ()
- (void)startTimeoutTimer;
- (void)setCompletionBlock:(OSNotificationDisplayResponse)completion;
- (void)complete:(OSDisplayableNotification *)notification;
- (BOOL)wantsToDisplay;
- (void)setWantsToDisplay:(BOOL)display;
@end
@implementation OSNotificationWillDisplayEvent
- (id)initWithDisplayableNotification:(OSDisplayableNotification*)notification {
self = [super init];
if(self) {
_notification = notification;
}
return self;
}
- (BOOL)isPreventDefault {
return !_notification.wantsToDisplay;
}
- (void)preventDefault {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"OSNotificationWillDisplayEvent.preventDefault called."]];
_notification.wantsToDisplay = false;
}
@end
@implementation OSNotificationsManager
+ (Class<OSNotifications>)Notifications {
return self;
}
static id<OneSignalNotificationsDelegate> _delegate;
+ (id<OneSignalNotificationsDelegate>)delegate {
return _delegate;
}
+(void)setDelegate:(id<OneSignalNotificationsDelegate>)delegate {
_delegate = delegate;
}
static NSMutableArray<NSObject<OSNotificationLifecycleListener> *> *_lifecycleListeners;
+ (NSMutableArray<NSObject<OSNotificationLifecycleListener> *>*)lifecycleListeners {
if (!_lifecycleListeners)
_lifecycleListeners = [NSMutableArray new];
return _lifecycleListeners;
}
// UIApplication-registerForRemoteNotifications has been called but a success or failure has not triggered yet.
static BOOL _waitingForApnsResponse = false;
static BOOL _providesAppNotificationSettings = false;
BOOL requestedProvisionalAuthorization = false;
static int mSubscriptionStatus = -1;
static NSMutableArray<OSNotificationClickEvent*> *_unprocessedClickEvents;
static NSMutableArray<NSObject<OSNotificationClickListener> *> *_clickListeners;
+ (NSMutableArray<NSObject<OSNotificationClickListener> *>*)clickListeners {
if (!_clickListeners)
_clickListeners = [NSMutableArray new];
return _clickListeners;
}
static NSDictionary* _lastMessageReceived;
static NSString *_lastMessageID = @"";
static NSString *_lastMessageIdFromAction;
static UIBackgroundTaskIdentifier _mediaBackgroundTask;
static BOOL _disableBadgeClearing = NO;
static BOOL _coldStartFromTapOnNotification = NO;
// Set to false as soon as it's read.
+ (BOOL)getColdStartFromTapOnNotification {
BOOL val = _coldStartFromTapOnNotification;
_coldStartFromTapOnNotification = NO;
return val;
}
+ (void)setColdStartFromTapOnNotification:(BOOL)coldStartFromTapOnNotification {
_coldStartFromTapOnNotification = coldStartFromTapOnNotification;
}
+ (void)setMSubscriptionStatus:(NSNumber*)status {
mSubscriptionStatus = [status intValue];
}
static OneSignalNotificationSettings *_osNotificationSettings;
+ (OneSignalNotificationSettings *)osNotificationSettings {
if (!_osNotificationSettings) {
_osNotificationSettings = [OneSignalNotificationSettings new];
}
return _osNotificationSettings;
}
// static property def to add developer's OSPermissionStateChanges observers to.
static ObservablePermissionStateChangesType* _permissionStateChangesObserver;
+ (ObservablePermissionStateChangesType*)permissionStateChangesObserver {
if (!_permissionStateChangesObserver)
_permissionStateChangesObserver = [[OSBoolObservable alloc] initWithChangeSelector:@selector(onNotificationPermissionDidChange:)];
return _permissionStateChangesObserver;
}
// static property def for currentPermissionState
static OSPermissionStateInternal* _currentPermissionState;
+ (OSPermissionStateInternal*)currentPermissionState {
if (!_currentPermissionState) {
_currentPermissionState = [OSPermissionStateInternal alloc];
_currentPermissionState = [_currentPermissionState initAsTo];
[self lastPermissionState]; // Trigger creation
[_currentPermissionState.observable addObserver:[OSPermissionChangedInternalObserver alloc]];
}
return _currentPermissionState;
}
// static property def for previous OSPermissionState
static OSPermissionStateInternal* _lastPermissionState;
+ (OSPermissionStateInternal*)lastPermissionState {
if (!_lastPermissionState)
_lastPermissionState = [[OSPermissionStateInternal alloc] initAsFrom];
return _lastPermissionState;
}
// TODO: pushToken, pushSubscriptionId needs to be available... is this the right setup
static NSString *_pushToken;
+ (NSString*)pushToken {
if (!_pushToken) {
_pushToken = [OneSignalUserDefaults.initShared getSavedStringForKey:OSUD_PUSH_TOKEN defaultValue:nil];
}
return _pushToken;
}
static NSString *_pushSubscriptionId;
+ (NSString*)pushSubscriptionId {
if (!_pushSubscriptionId) {
_pushSubscriptionId = [OneSignalUserDefaults.initShared getSavedStringForKey:OSUD_PUSH_SUBSCRIPTION_ID defaultValue:nil];
}
return _pushSubscriptionId;
}
+ (void)setPushSubscriptionId:(NSString *)pushSubscriptionId {
_pushSubscriptionId = pushSubscriptionId;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
+ (void)start {
// Swizzle - UIApplication delegate
//TODO: do the equivalent in the notificaitons module
injectSelector(
[UIApplication class],
@selector(setDelegate:),
[OneSignalNotificationsAppDelegate class],
@selector(setOneSignalDelegate:)
);
//TODO: This swizzling is done from notifications module
injectSelector(
[UIApplication class],
@selector(setApplicationIconBadgeNumber:),
[OneSignalNotificationsAppDelegate class],
@selector(onesignalSetApplicationIconBadgeNumber:)
);
[OneSignalNotificationsUNUserNotificationCenter setup];
[self registerLifecycleObserver];
}
#pragma clang diagnostic pop
+ (void)registerLifecycleObserver {
// Replacing swizzled lifecycle selectors with notification center observers for scene based Apps
if ([OSBundleUtils isAppUsingUIScene]) {
[self registerLifecycleObserverAsUIScene];
} else {
[self registerLifecycleObserverAsUIApplication];
}
}
+ (void)registerLifecycleObserverAsUIScene {
if (@available(iOS 13.0, *)) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"OSNotificationManager registering for Scene Lifecycle notifications"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:@"UISceneWillEnterForegroundNotification" object:nil];
}
}
+ (void)registerLifecycleObserverAsUIApplication {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"OSNotificationManager registering for Application Lifecycle notifications"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
+ (void)willEnterForeground {
[OSNotificationsManager clearBadgeCount:false fromClearAll:false];
[OSNotificationsManager sendNotificationTypesUpdateToDelegate];
}
+ (void)resetLocals {
_lastMessageReceived = nil;
_lastMessageIdFromAction = nil;
_lastMessageID = @"";
_unprocessedClickEvents = nil;
}
+ (void)setLastPermissionState:(OSPermissionStateInternal *)lastPermissionState {
_lastPermissionState = lastPermissionState;
}
+ (BOOL)permission {
return self.currentPermissionState.reachable;
}
+ (BOOL)canRequestPermission {
return !self.currentPermissionState.answeredPrompt;
}
+ (OSNotificationPermission)permissionNative {
return self.currentPermissionState.status;
}
+ (void)requestPermission:(OSUserResponseBlock)block {
// return if the user has not granted privacy permissions
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:@"requestPermission:"])
return;
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"requestPermission Called"]];
self.currentPermissionState.hasPrompted = true;
[self.osNotificationSettings promptForNotifications:block];
}
// if user has disabled push notifications & fallback == true,
// the SDK will prompt the user to open notification Settings for this app
+ (void)requestPermission:(OSUserResponseBlock)block fallbackToSettings:(BOOL)fallback {
if (self.currentPermissionState.hasPrompted == true && self.osNotificationSettings.getNotificationTypes == 0 && fallback) {
//show settings
let localizedTitle = NSLocalizedString(@"Open Settings", @"A title saying that the user can open iOS Settings");
let localizedSettingsActionTitle = NSLocalizedString(@"Open Settings", @"A button allowing the user to open the Settings app");
let localizedCancelActionTitle = NSLocalizedString(@"Cancel", @"A button allowing the user to close the Settings prompt");
//the developer can provide a custom message in Info.plist if they choose.
var localizedMessage = (NSString *)[[NSBundle mainBundle] objectForInfoDictionaryKey:FALLBACK_TO_SETTINGS_MESSAGE];
if (!localizedMessage)
localizedMessage = NSLocalizedString(@"You currently have notifications turned off for this application. You can open Settings to re-enable them", @"A message explaining that users can open Settings to re-enable push notifications");
/*
Provide a protocol for this and inject it rather than referencing dialogcontroller directly. This is is because it uses UIApplication sharedApplication
*/
[[OSDialogInstanceManager sharedInstance] presentDialogWithTitle:localizedTitle withMessage:localizedMessage withActions:@[localizedSettingsActionTitle] cancelTitle:localizedCancelActionTitle withActionCompletion:^(int tappedActionIndex) {
if (block)
block(false);
//completion is called on the main thread
if (tappedActionIndex > -1)
[self presentAppSettings];
}];
return;
}
[self requestPermission:block];
}
+ (void)registerForProvisionalAuthorization:(OSUserResponseBlock)block {
if ([OSDeviceUtils isIOSVersionGreaterThanOrEqual:@"12.0"])
[self.osNotificationSettings registerForProvisionalAuthorization:block];
else
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"registerForProvisionalAuthorization is only available in iOS 12+."];
}
// Checks to see if we should register for APNS' new Provisional authorization
// (also known as Direct to History).
// This behavior is determined by the OneSignal Parameters request
+ (void)checkProvisionalAuthorizationStatus {
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil])
return;
BOOL usesProvisional = [OneSignalUserDefaults.initStandard getSavedBoolForKey:OSUD_USES_PROVISIONAL_PUSH_AUTHORIZATION defaultValue:false];
// if iOS parameters for this app have never downloaded, this method
// should return
if (!usesProvisional || requestedProvisionalAuthorization)
return;
requestedProvisionalAuthorization = true;
[self.osNotificationSettings registerForProvisionalAuthorization:nil];
}
// iOS 12+ only
// A boolean indicating if the app provides its own custom Notifications Settings UI
// If this is set to TRUE via the kOSSettingsKeyProvidesAppNotificationSettings init
// parameter, the SDK will request authorization from the User Notification Center
+ (BOOL)providesAppNotificationSettings {
return _providesAppNotificationSettings;
}
+ (void)setProvidesNotificationSettingsView:(BOOL)providesView {
_providesAppNotificationSettings = providesView;
}
//presents the settings page to control/customize push notification settings
+ (void)presentAppSettings {
//only supported in 10+
if ([OSDeviceUtils isIOSVersionLessThan:@"10.0"])
return;
let url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if (!url)
return;
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:@"Unable to open settings for this application"];
}
}
+ (void)clearAll {
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
// removing delivered notifications doesn't update the badge count
[self clearBadgeCount:false fromClearAll:true];
}
+ (BOOL)registerForAPNsToken {
if (self.waitingForApnsResponse)
return true;
id backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
BOOL backgroundModesEnabled = (backgroundModes && [backgroundModes containsObject:@"remote-notification"]);
// Only try to register for a pushToken if:
// - The user accepted notifications
// - "Background Modes" > "Remote Notifications" are enabled in Xcode
if (![self.osNotificationSettings getNotificationPermissionState].accepted && !backgroundModesEnabled)
return false;
// Don't attempt to register again if there was a non-recoverable error.
if (mSubscriptionStatus < -9)
return false;
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Firing registerForRemoteNotifications"];
self.waitingForApnsResponse = true;
[OneSignalCoreHelper dispatch_async_on_main_queue:^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
}];
return true;
}
+ (void)didRegisterForRemoteNotifications:(UIApplication *)app
deviceToken:(NSData *)inDeviceToken {
let parsedDeviceToken = [NSString hexStringFromData:inDeviceToken];
[OneSignalLog onesignalLog:ONE_S_LL_INFO message: [NSString stringWithFormat:@"Device Registered with Apple: %@", parsedDeviceToken]];
if (!parsedDeviceToken) {
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:@"Unable to convert APNS device token to a string"];
return;
}
self.waitingForApnsResponse = false;
_pushToken = parsedDeviceToken;
// Cache push token
[OneSignalUserDefaults.initShared saveStringForKey:OSUD_PUSH_TOKEN withValue:_pushToken];
[self sendPushTokenToDelegate];
}
+ (void)handleDidFailRegisterForRemoteNotification:(NSError*)err {
OSNotificationsManager.waitingForApnsResponse = false;
if (err.code == 3000) {
[self setSubscriptionErrorStatus:ERROR_PUSH_CAPABLILITY_DISABLED];
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:@"ERROR! 'Push Notifications' capability missing! Add the capability in Xcode under 'Target' -> '<MyAppName(MainTarget)>' -> 'Signing & Capabilities' then click the '+ Capability' button."];
}
else if (err.code == 3010) {
[self setSubscriptionErrorStatus:ERROR_PUSH_SIMULATOR_NOT_SUPPORTED];
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"Error! iOS Simulator does not support push! Please test on a real iOS device. Error: %@", err]];
}
else {
[self setSubscriptionErrorStatus:ERROR_PUSH_UNKNOWN_APNS_ERROR];
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"Error registering for Apple push notifications! Error: %@", err]];
}
}
+ (void)sendPushTokenToDelegate {
// TODO: Keep this as a check on _pushToken instead of self.pushToken?
if (_pushToken != nil && self.delegate && [self.delegate respondsToSelector:@selector(setPushToken:)]) {
[self.delegate setPushToken:_pushToken];
}
}
+ (void)setSubscriptionErrorStatus:(int)errorType {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message: [NSString stringWithFormat:@"setSubscriptionErrorStatus: %d", errorType]];
mSubscriptionStatus = errorType;
[self sendNotificationTypesUpdateToDelegate];
}
// onNotificationPermissionDidChange should only fire if the reachable property changed.
+ (void)addPermissionObserver:(NSObject<OSNotificationPermissionObserver>*)observer {
[self.permissionStateChangesObserver addObserver:observer];
if (self.currentPermissionState.reachable != self.lastPermissionState.reachable)
[OSPermissionChangedInternalObserver fireChangesObserver:self.currentPermissionState];
}
+ (void)removePermissionObserver:(NSObject<OSNotificationPermissionObserver>*)observer {
[self.permissionStateChangesObserver removeObserver:observer];
}
// User just responed to the iOS native notification permission prompt.
+ (void)updateNotificationTypes:(int)notificationTypes {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"updateNotificationTypes called: %d", notificationTypes]];
// TODO: Dropped support, can remove below?
if ([OSDeviceUtils isIOSVersionLessThan:@"10.0"])
[OneSignalUserDefaults.initStandard saveBoolForKey:OSUD_WAS_NOTIFICATION_PROMPT_ANSWERED_TO withValue:true];
BOOL startedRegister = [OSNotificationsManager registerForAPNsToken];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"startedRegister: %d", startedRegister]];
// TODO: Dropped support, can remove below?
[self.osNotificationSettings onNotificationPromptResponse:notificationTypes]; // iOS 9 only
// TODO: This can be called before the User Manager sets itself as the delegate
[self sendNotificationTypesUpdateToDelegate];
}
+ (void)sendNotificationTypesUpdateToDelegate {
// We don't delay observer update to wait until the OneSignal server is notified
// TODO: We can do the above and delay observers until server is updated.
if (self.delegate && [self.delegate respondsToSelector:@selector(setNotificationTypes:)]) {
[self.delegate setNotificationTypes:[self getNotificationTypes]];
}
}
// Accounts for manual disabling by the app developer
+ (int)getNotificationTypes:(BOOL)pushDisabled {
if (pushDisabled) {
return -2;
}
return [self getNotificationTypes];
}
// Device notification types, that doesn't account for manual disabling by the app developer
+ (int)getNotificationTypes {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message: [NSString stringWithFormat:@"getNotificationTypes:mSubscriptionStatus: %d", mSubscriptionStatus]];
if (mSubscriptionStatus < -9)
return mSubscriptionStatus;
// This was previously nil if just accessing _pushToken
if (OSNotificationsManager.waitingForApnsResponse && !self.pushToken)
return ERROR_PUSH_DELEGATE_NEVER_FIRED;
OSPermissionStateInternal* permissionStatus = [OSNotificationsManager.osNotificationSettings getNotificationPermissionState];
//only return the error statuses if not provisional
if (!permissionStatus.provisional && !permissionStatus.hasPrompted)
return ERROR_PUSH_NEVER_PROMPTED;
if (!permissionStatus.provisional && !permissionStatus.answeredPrompt)
return ERROR_PUSH_PROMPT_NEVER_ANSWERED;
return permissionStatus.notificationTypes;
}
+ (BOOL)waitingForApnsResponse {
return _waitingForApnsResponse;
}
+ (void)setWaitingForApnsResponse:(BOOL)value {
_waitingForApnsResponse = value;
}
+ (void)clearStatics {
_waitingForApnsResponse = false;
_currentPermissionState = nil;
_lastPermissionState = nil;
requestedProvisionalAuthorization = false;
// and more...
}
static NSString *_lastAppActiveMessageId;
+ (void)setLastAppActiveMessageId:(NSString*)value { _lastAppActiveMessageId = value; }
static NSString *_lastnonActiveMessageId;
+ (void)setLastnonActiveMessageId:(NSString*)value { _lastnonActiveMessageId = value; }
// Entry point for the following:
// - 1. (iOS all) - Opening notifications
// - 2. Notification received
// - 2A. iOS 9 - Notification received while app is in focus.
// - 2B. iOS 10 - Notification received/displayed while app is in focus.
// isActive is not always true for when the application is on foreground, we need differentiation
// between foreground and isActive
+ (void)notificationReceived:(NSDictionary*)messageDict wasOpened:(BOOL)opened {
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil])
return;
if (![OneSignalConfigManager getAppId]) {
return;
}
// This method should not continue to be executed for non-OS push notifications
if (![OneSignalCoreHelper isOneSignalPayload:messageDict])
return;
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"notificationReceived called! opened: %@", opened ? @"YES" : @"NO"]];
NSDictionary* customDict = [messageDict objectForKey:@"os_data"] ?: [messageDict objectForKey:@"custom"];
// Should be called first, other methods relay on this global state below.
[self lastMessageReceived:messageDict];
BOOL isPreview = [[OSNotification parseWithApns:messageDict] additionalData][ONESIGNAL_IAM_PREVIEW] != nil;
if (opened) {
// Prevent duplicate calls
let newId = [self checkForProcessedDups:customDict lastMessageId:_lastnonActiveMessageId];
if ([@"dup" isEqualToString:newId]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Duplicate notif received. Not calling opened handler."];
return;
}
if (newId)
_lastnonActiveMessageId = newId;
//app was in background / not running and opened due to a tap on a notification or an action check what type
OSNotificationActionType type = OSNotificationActionTypeOpened;
if (messageDict[@"custom"][@"a"][@"actionSelected"] || messageDict[@"actionSelected"])
type = OSNotificationActionTypeActionTaken;
// Call Action Block
[self handleNotificationOpened:messageDict actionType:type];
} else if (isPreview && [OSDeviceUtils isIOSVersionGreaterThanOrEqual:@"10.0"]) {
let notification = [OSNotification parseWithApns:messageDict];
[self handleIAMPreview:notification];
}
}
+ (NSString*)checkForProcessedDups:(NSDictionary*)customDict lastMessageId:(NSString*)lastMessageId {
if (customDict && customDict[@"i"]) {
NSString* currentNotificationId = customDict[@"i"];
if ([currentNotificationId isEqualToString:lastMessageId])
return @"dup";
return customDict[@"i"];
}
return nil;
}
+ (void)handleWillPresentNotificationInForegroundWithPayload:(NSDictionary *)payload withCompletion:(OSNotificationDisplayResponse)completion {
// check to make sure the app is in focus and it's a OneSignal notification
if (![OneSignalCoreHelper isOneSignalPayload:payload]
|| UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
completion([OSNotification new]);
return;
}
//Only call the OSNotificationLifecycleListener for notifications not preview IAMs
OSDisplayableNotification *osNotification = [OSDisplayableNotification parseWithApns:payload];
if ([osNotification additionalData][ONESIGNAL_IAM_PREVIEW]) {
completion(nil);
return;
}
[self handleWillShowInForegroundForNotification:osNotification completion:completion];
}
+ (void)handleWillShowInForegroundForNotification:(OSDisplayableNotification *)notification completion:(OSNotificationDisplayResponse)completion {
[notification setCompletionBlock:completion];
if (self.lifecycleListeners.count == 0) {
completion(notification);
return;
}
[notification startTimeoutTimer];
OSNotificationWillDisplayEvent *event = [[OSNotificationWillDisplayEvent alloc] initWithDisplayableNotification:notification];
for (NSObject<OSNotificationLifecycleListener> *listener in self.lifecycleListeners) {
if ([listener respondsToSelector:@selector(onWillDisplayNotification:)]) {
[listener onWillDisplayNotification:event];
}
}
if (![event isPreventDefault]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"OSNotificationWillDisplayEvent's preventDefault not called, now display notification with notificationId %@.", notification.notificationId]];
[notification complete:notification];
}
}
+ (void)handleNotificationOpened:(NSDictionary*)messageDict
actionType:(OSNotificationActionType)actionType {
// return if the user has not granted privacy permissions
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:@"handleNotificationOpened:actionType:"])
return;
OSNotification *notification = [OSNotification parseWithApns:messageDict];
if ([self handleIAMPreview:notification])
return;
NSDictionary* customDict = [messageDict objectForKey:@"custom"] ?: [messageDict objectForKey:@"os_data"];
// Notify backend that user opened the notification
NSString* messageId = [customDict objectForKey:@"i"];
[self submitNotificationOpened:messageId];
let isActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"handleNotificationOpened called! isActive: %@ notificationId: %@",
isActive ? @"YES" : @"NO", messageId]];
if (![self shouldSuppressURL]) {
// Try to fetch the open url to launch
[self launchWebURL:notification.launchURL]; //TODO: where should this live?
}
[self clearBadgeCount:true fromClearAll:false];
NSString* actionID = NULL;
if (actionType == OSNotificationActionTypeActionTaken) {
actionID = messageDict[@"custom"][@"a"][@"actionSelected"];
if(!actionID)
actionID = messageDict[@"actionSelected"];
}
// Call Action Block
[self lastMessageReceived:messageDict];
if (!isActive) {
OSSessionManager.sharedSessionManager.appEntryState = NOTIFICATION_CLICK;
[[OSSessionManager sharedSessionManager] onDirectInfluenceFromNotificationOpen:NOTIFICATION_CLICK withNotificationId:messageId];
}
[self handleNotificationActionWithUrl:notification.launchURL actionID:actionID];
}
+ (void)submitNotificationOpened:(NSString*)messageId {
// return if the user has not granted privacy permissions
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil])
return;
let standardUserDefaults = OneSignalUserDefaults.initStandard;
//(DUPLICATE Fix): Make sure we do not upload a notification opened twice for the same messageId
//Keep track of the Id for the last message sent
NSString* lastMessageId = [standardUserDefaults getSavedStringForKey:OSUD_LAST_MESSAGE_OPENED defaultValue:nil];
//Only submit request if messageId not nil and: (lastMessage is nil or not equal to current one)
if(messageId && (!lastMessageId || ![lastMessageId isEqualToString:messageId])) {
[OneSignalCoreImpl.sharedClient executeRequest:[OSRequestSubmitNotificationOpened withUserId:[self pushSubscriptionId]
appId:[OneSignalConfigManager getAppId]
wasOpened:YES
messageId:messageId
withDeviceType:[NSNumber numberWithInt:DEVICE_TYPE_PUSH]]
onSuccess:nil
onFailure:nil];
[standardUserDefaults saveStringForKey:OSUD_LAST_MESSAGE_OPENED withValue:messageId];
}
}
+ (void)launchWebURL:(NSString*)openUrl {
NSString* toOpenUrl = [OneSignalCoreHelper trimURLSpacing:openUrl];
if (toOpenUrl && [OneSignalCoreHelper verifyURL:toOpenUrl]) {
NSURL *url = [NSURL URLWithString:toOpenUrl];
// Give the app resume animation time to finish when tapping on a notification from the notification center.
// Isn't a requirement but improves visual flow.
[self performSelector:@selector(displayWebView:) withObject:url afterDelay:0.5];
}
}
+ (void)displayWebView:(NSURL*)url {
__block let openUrlBlock = ^void(BOOL shouldOpen) {
if (!shouldOpen)
return;
[OneSignalCoreHelper dispatch_async_on_main_queue: ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// Keep dispatch_async. Without this the url can take an extra 2 to 10 secounds to open.
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
});
}];
};
openUrlBlock(true);
}
+ (void)clearBadgeCount:(BOOL)fromNotifOpened fromClearAll:(BOOL)fromClearAll {
NSNumber *disableBadgeNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:ONESIGNAL_DISABLE_BADGE_CLEARING];
if (disableBadgeNumber)
_disableBadgeClearing = [disableBadgeNumber boolValue];
else
_disableBadgeClearing = NO;
if (_disableBadgeClearing && !fromClearAll) {
// The developer could have manually changed the badge value but we cannot read the current state.
// We swizzle badge count setters, which should be sufficient to ensure the cached value matches the current state.
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"clearBadgeCount called but badge clearing is disabled, not updating cached badge count"];
return;
}
if (@available(iOS 16.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:0 withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"clearBadgeCount encountered error setting badge count: %@", error]];
}
}];
} else {
[OneSignalCoreHelper runOnMainThread:^{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}];
}
}
+ (BOOL)handleIAMPreview:(OSNotification *)notification {
NSString *uuid = [notification additionalData][ONESIGNAL_IAM_PREVIEW];
if (uuid) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"IAM Preview Detected, Begin Handling"];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:notification forKey:@"notification"];
[[NSNotificationCenter defaultCenter] postNotificationName:ONESIGNAL_POST_PREVIEW_IAM object:nil userInfo:userInfo];
return YES;
}
return NO;
}
+ (void)handleNotificationActionWithUrl:(NSString*)url actionID:(NSString*)actionID {
if (![OneSignalCoreHelper isOneSignalPayload:_lastMessageReceived])
return;
OSNotificationClickResult *result = [[OSNotificationClickResult alloc] initWithUrl:url :actionID];
OSNotification *notification = [OSNotification parseWithApns:_lastMessageReceived];
OSNotificationClickEvent *event = [[OSNotificationClickEvent alloc] initWithNotification:notification result:result];
// Prevent duplicate calls to same action
if ([notification.notificationId isEqualToString:_lastMessageIdFromAction])
return;
_lastMessageIdFromAction = notification.notificationId;
[OneSignalTrackFirebaseAnalytics trackOpenEvent:event];
if (self.clickListeners.count == 0) {
[self addUnprocessedClickEvent:event];
return;
}
[self fireClickListenersForEvent:event];
}
+ (void)fireClickListenersForEvent:(OSNotificationClickEvent*)event {
for (NSObject<OSNotificationClickListener> *listener in self.clickListeners) {
if ([listener respondsToSelector:@selector(onClickNotification:)]) {
[listener onClickNotification:event];
}
}
}
+ (void)lastMessageReceived:(NSDictionary*)message {
_lastMessageReceived = message;
}
+ (BOOL)shouldSuppressURL {
// if the plist key does not exist default to false
// the plist value specifies whether the user wants to open an url using default browser or OSWebView
NSDictionary *bundleDict = [[NSBundle mainBundle] infoDictionary];
BOOL shouldSuppress = [bundleDict[ONESIGNAL_SUPRESS_LAUNCH_URLS] boolValue];
return shouldSuppress ?: false;
}
+ (void)addClickListener:(NSObject<OSNotificationClickListener>*)listener {
[self.clickListeners addObject:listener];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification click listener added successfully"];
[self fireClickListenersForUnprocessedEvents];
}
+ (void)removeClickListener:(NSObject<OSNotificationClickListener>*)listener {
[self.clickListeners removeObject:listener];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification click listener removed successfully"];
}
+ (void)addForegroundLifecycleListener:(NSObject<OSNotificationLifecycleListener> *_Nullable)listener {
[self.lifecycleListeners addObject:listener];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"ForegroundLifecycleListener added successfully"];
}
+ (void)removeForegroundLifecycleListener:(NSObject<OSNotificationLifecycleListener> * _Nullable)listener {
[self.lifecycleListeners removeObject:listener];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"ForegroundLifecycleListener removed successfully"];
}
+ (NSMutableArray<OSNotificationClickEvent*>*)getUnprocessedClickEvents {
if (!_unprocessedClickEvents)
_unprocessedClickEvents = [NSMutableArray new];
return _unprocessedClickEvents;
}
+ (void)addUnprocessedClickEvent:(OSNotificationClickEvent*)event {
[[self getUnprocessedClickEvents] addObject:event];
}
+ (void)fireClickListenersForUnprocessedEvents {
if (self.clickListeners.count == 0) {
return;
}
for (OSNotificationClickEvent* event in [self getUnprocessedClickEvents]) {
[self fireClickListenersForEvent:event];
}
_unprocessedClickEvents = [NSMutableArray new];
}
+ (BOOL)receiveRemoteNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
var startedBackgroundJob = false;
NSDictionary* richData = nil;
// TODO: Look into why the userInfo payload would be different here for displaying vs opening....
// Check for buttons or attachments pre-2.4.0 version
if ((userInfo[@"os_data"][@"buttons"] && [userInfo[@"os_data"][@"buttons"] isKindOfClass:[NSDictionary class]]) || userInfo[@"at"] || userInfo[@"o"])
richData = userInfo;
// Generate local notification for action button and/or attachments.
if (richData) {
let osNotification = [OSNotification parseWithApns:userInfo];
if ([OSDeviceUtils isIOSVersionGreaterThanOrEqual:@"10.0"]) {
startedBackgroundJob = true;
[self addNotificationRequest:osNotification completionHandler:completionHandler];
}
}
// Method was called due to a tap on a notification - Fire open notification
else if (application.applicationState == UIApplicationStateActive) {
_lastMessageReceived = userInfo;
if ([OneSignalCoreHelper isDisplayableNotification:userInfo]) {
[self notificationReceived:userInfo wasOpened:YES];
}
return startedBackgroundJob;
}
// content-available notification received in the background
else {
_lastMessageReceived = userInfo;
}
return startedBackgroundJob;
}
+ (void)addNotificationRequest:(OSNotification*)notification
completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Start background thread to download media so we don't lock the main UI thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundMediaTask];
let notificationRequest = [self prepareUNNotificationRequest:notification];
[[UNUserNotificationCenter currentNotificationCenter]
addNotificationRequest:notificationRequest
withCompletionHandler:^(NSError * _Nullable error) {}];
if (completionHandler)
completionHandler(UIBackgroundFetchResultNewData);
[self endBackgroundMediaTask];
});
}
+ (void)beginBackgroundMediaTask {
_mediaBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundMediaTask];
}];
}
+ (void)endBackgroundMediaTask {
[[UIApplication sharedApplication] endBackgroundTask: _mediaBackgroundTask];
_mediaBackgroundTask = UIBackgroundTaskInvalid;
}
+ (UNNotificationRequest*)prepareUNNotificationRequest:(OSNotification*)notification {
let content = [UNMutableNotificationContent new];
[OneSignalAttachmentHandler addActionButtons:notification toNotificationContent:content];
content.title = notification.title;
content.subtitle = notification.subtitle;
content.body = notification.body;
content.userInfo = notification.rawPayload;
if (notification.sound)
content.sound = [UNNotificationSound soundNamed:notification.sound];
else
content.sound = UNNotificationSound.defaultSound;
if (notification.hasBadge)
content.badge = [NSNumber numberWithInteger:notification.badge];
// Check if media attached
[OneSignalAttachmentHandler addAttachments:notification toNotificationContent:content];
let trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.25 repeats:NO];
let identifier = [OneSignalCoreHelper randomStringWithLength:16];
return [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];