Skip to content

Commit 4164b0f

Browse files
committed
return early from manual APIs if swizzling is enabled
1 parent 81fbfe7 commit 4164b0f

5 files changed

Lines changed: 70 additions & 17 deletions

File tree

iOS_SDK/OneSignalSDK/OneSignalNotifications/Categories/UIApplicationDelegate+OneSignalNotifications.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ + (BOOL)swizzledClassInHeirarchy:(Class)delegateClass {
111111
return false;
112112
}
113113

114-
- (void)oneSignalDidRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSData*)inDeviceToken {
114+
- (void)oneSignalDidRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSData*)deviceToken {
115115
[OneSignalNotificationsAppDelegate traceCall:@"oneSignalDidRegisterForRemoteNotifications:deviceToken:"];
116116

117-
[OSNotificationsManager didRegisterForRemoteNotificationsWithDeviceToken:inDeviceToken];
117+
[OSNotificationsManager processRegisteredDeviceToken:deviceToken];
118118

119119
SwizzlingForwarder *forwarder = [[SwizzlingForwarder alloc]
120120
initWithTarget:self
@@ -125,14 +125,14 @@ - (void)oneSignalDidRegisterForRemoteNotifications:(UIApplication*)app deviceTok
125125
application:didRegisterForRemoteNotificationsWithDeviceToken:
126126
)
127127
];
128-
[forwarder invokeWithArgs:@[app, inDeviceToken]];
128+
[forwarder invokeWithArgs:@[app, deviceToken]];
129129
}
130130

131131
- (void)oneSignalDidFailRegisterForRemoteNotification:(UIApplication*)app error:(NSError*)err {
132132
[OneSignalNotificationsAppDelegate traceCall:@"oneSignalDidFailRegisterForRemoteNotification:error:"];
133133

134134
if ([OneSignalConfigManager getAppId])
135-
[OSNotificationsManager didFailToRegisterForRemoteNotificationsWithError:err];
135+
[OSNotificationsManager processFailedRemoteNotificationsRegistration:err];
136136

137137
SwizzlingForwarder *forwarder = [[SwizzlingForwarder alloc]
138138
initWithTarget:self
@@ -180,7 +180,7 @@ - (void) oneSignalReceiveRemoteNotification:(UIApplication*)application UserInfo
180180
else if (appState == UIApplicationStateActive && isVisibleNotification)
181181
[OSNotificationsManager notificationReceived:userInfo wasOpened:NO];
182182
else
183-
startedBackgroundJob = [OSNotificationsManager didReceiveRemoteNotification:userInfo completionHandler:forwarder.hasReceiver ? nil : completionHandler];
183+
startedBackgroundJob = [OSNotificationsManager processReceivedRemoteNotification:userInfo completionHandler:forwarder.hasReceiver ? nil : completionHandler];
184184
}
185185

186186
if (forwarder.hasReceiver) {

iOS_SDK/OneSignalSDK/OneSignalNotifications/Categories/UNUserNotificationCenter+OneSignalNotifications.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ - (void)onesignalUserNotificationCenter:(UNUserNotificationCenter *)center
293293

294294
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler: Fired! %@", notification.request.content.body]];
295295

296-
[OSNotificationsManager willPresentNotificationWithPayload:notification.request.content.userInfo completion:^(OSNotification *responseNotif) {
296+
[OSNotificationsManager processWillPresentNotificationWithPayload:notification.request.content.userInfo completion:^(OSNotification *responseNotif) {
297297
UNNotificationPresentationOptions displayType = responseNotif != nil ? (UNNotificationPresentationOptions)7 : (UNNotificationPresentationOptions)0;
298298
finishProcessingNotification(notification, center, displayType, completionHandler, self);
299299
}];
@@ -377,7 +377,7 @@ + (BOOL)isDismissEvent:(UNNotificationResponse *)response {
377377
}
378378

379379
+ (void)processiOS10Open:(UNNotificationResponse*)response {
380-
[OSNotificationsManager didReceiveNotificationResponse:response];
380+
[OSNotificationsManager processNotificationResponse:response];
381381
}
382382

383383
// Calls depercated pre-iOS 10 selector if one is set on the AppDelegate.

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,14 @@ NS_SWIFT_NAME(onClick(event:));
131131
+ (void)notificationReceived:(NSDictionary* _Nonnull)messageDict wasOpened:(BOOL)opened;
132132
+ (void)checkProvisionalAuthorizationStatus;
133133
+ (void)registerLifecycleObserver;
134+
+ (BOOL)isSwizzlingDisabled;
135+
136+
// Internal entry points called by swizzled delegate paths
137+
// These bypass the swizzling-active guard so the SDK doesn't block its own calls
138+
+ (void)processRegisteredDeviceToken:(NSData *_Nonnull)deviceToken;
139+
+ (void)processFailedRemoteNotificationsRegistration:(NSError *_Nonnull)error;
140+
+ (BOOL)processReceivedRemoteNotification:(NSDictionary *_Nonnull)userInfo completionHandler:(void (^_Nonnull)(UIBackgroundFetchResult))completionHandler;
141+
+ (void)processWillPresentNotificationWithPayload:(NSDictionary *_Nonnull)payload completion:(OSNotificationDisplayResponse _Nonnull)completion;
142+
+ (void)processNotificationResponse:(UNNotificationResponse *_Nonnull)response;
134143

135144
@end

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ +(void)setDelegate:(id<OneSignalNotificationsDelegate>)delegate {
161161
static UIBackgroundTaskIdentifier _mediaBackgroundTask;
162162
static BOOL _disableBadgeClearing = NO;
163163

164+
+ (BOOL)isSwizzlingDisabled {
165+
static BOOL _cached = NO;
166+
static BOOL _disabled = NO;
167+
if (!_cached) {
168+
id plistValue = [[NSBundle mainBundle] objectForInfoDictionaryKey:ONESIGNAL_DISABLE_SWIZZLING];
169+
_disabled = plistValue != nil && [plistValue boolValue];
170+
_cached = YES;
171+
}
172+
return _disabled;
173+
}
164174

165175
static BOOL _coldStartFromTapOnNotification = NO;
166176
// Set to false as soon as it's read.
@@ -441,8 +451,16 @@ + (BOOL)registerForAPNsToken {
441451
return true;
442452
}
443453

444-
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)inDeviceToken {
445-
let parsedDeviceToken = [NSString hexStringFromData:inDeviceToken];
454+
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
455+
if (![self isSwizzlingDisabled]) {
456+
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"OneSignal: didRegisterForRemoteNotificationsWithDeviceToken: ignored because swizzling is active. Remove the manual call or set OneSignal_disable_swizzling to true in Info.plist."];
457+
return;
458+
}
459+
[self processRegisteredDeviceToken:deviceToken];
460+
}
461+
462+
+ (void)processRegisteredDeviceToken:(NSData *)deviceToken {
463+
let parsedDeviceToken = [NSString hexStringFromData:deviceToken];
446464

447465
[OneSignalLog onesignalLog:ONE_S_LL_INFO message: [NSString stringWithFormat:@"Device Registered with Apple: %@", parsedDeviceToken]];
448466

@@ -462,6 +480,14 @@ + (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)inDeviceToken
462480
}
463481

464482
+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError*)err {
483+
if (![self isSwizzlingDisabled]) {
484+
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"OneSignal: didFailToRegisterForRemoteNotificationsWithError: ignored because swizzling is active. Remove the manual call or set OneSignal_disable_swizzling to true in Info.plist."];
485+
return;
486+
}
487+
[self processFailedRemoteNotificationsRegistration:err];
488+
}
489+
490+
+ (void)processFailedRemoteNotificationsRegistration:(NSError*)err {
465491
OSNotificationsManager.waitingForApnsResponse = false;
466492

467493
if (err.code == 3000) {
@@ -648,6 +674,15 @@ + (NSString*)checkForProcessedDups:(NSDictionary*)customDict lastMessageId:(NSSt
648674
}
649675

650676
+ (void)willPresentNotificationWithPayload:(NSDictionary *)payload completion:(OSNotificationDisplayResponse)completion {
677+
if (![self isSwizzlingDisabled]) {
678+
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"OneSignal: willPresentNotificationWithPayload:completion: ignored because swizzling is active. Remove the manual call or set OneSignal_disable_swizzling to true in Info.plist."];
679+
completion([OSNotification new]);
680+
return;
681+
}
682+
[self processWillPresentNotificationWithPayload:payload completion:completion];
683+
}
684+
685+
+ (void)processWillPresentNotificationWithPayload:(NSDictionary *)payload completion:(OSNotificationDisplayResponse)completion {
651686
// check to make sure the app is in focus and it's a OneSignal notification
652687
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil]
653688
|| ![OneSignalCoreHelper isOneSignalPayload:payload]
@@ -902,6 +937,14 @@ + (void)fireClickListenersForUnprocessedEvents {
902937
}
903938

904939
+ (BOOL)didReceiveRemoteNotification:(NSDictionary*)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
940+
if (![self isSwizzlingDisabled]) {
941+
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"OneSignal: didReceiveRemoteNotification:completionHandler: ignored because swizzling is active. Remove the manual call or set OneSignal_disable_swizzling to true in Info.plist."];
942+
return NO;
943+
}
944+
return [self processReceivedRemoteNotification:userInfo completionHandler:completionHandler];
945+
}
946+
947+
+ (BOOL)processReceivedRemoteNotification:(NSDictionary*)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
905948
var startedBackgroundJob = false;
906949

907950
NSDictionary* richData = nil;
@@ -996,6 +1039,14 @@ + (UNNotificationRequest*)prepareUNNotificationRequest:(OSNotification*)notifica
9961039
#pragma mark - Manual Integration APIs (for use when swizzling is disabled)
9971040

9981041
+ (void)didReceiveNotificationResponse:(UNNotificationResponse *)response {
1042+
if (![self isSwizzlingDisabled]) {
1043+
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"OneSignal: didReceiveNotificationResponse: ignored because swizzling is active. Remove the manual call or set OneSignal_disable_swizzling to true in Info.plist."];
1044+
return;
1045+
}
1046+
[self processNotificationResponse:response];
1047+
}
1048+
1049+
+ (void)processNotificationResponse:(UNNotificationResponse *)response {
9991050
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil])
10001051
return;
10011052
if (![OneSignalConfigManager getAppId])

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ + (void)load {
752752
[OSDialogInstanceManager setSharedOSDialogInstance:[OneSignalDialogController sharedInstance]];
753753
[OSNotificationsManager registerLifecycleObserver];
754754

755-
if ([self shouldDisableSwizzling]) {
755+
if ([OSNotificationsManager isSwizzlingDisabled]) {
756756
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"OneSignal method swizzling is disabled via Info.plist. Developers must manually forward notification delegate methods to OneSignal."];
757757
return;
758758
}
@@ -797,13 +797,6 @@ - (void)onesignalSetApplicationIconBadgeNumber:(NSInteger)badge {
797797
[self onesignalSetApplicationIconBadgeNumber:badge];
798798
}
799799

800-
+ (BOOL)shouldDisableSwizzling {
801-
id plistValue = [[NSBundle mainBundle] objectForInfoDictionaryKey:ONESIGNAL_DISABLE_SWIZZLING];
802-
if (plistValue != nil && [plistValue boolValue]) {
803-
return YES;
804-
}
805-
return NO;
806-
}
807800
@end
808801

809802
#pragma clang diagnostic pop

0 commit comments

Comments
 (0)