@@ -136,6 +136,24 @@ - (void)applicationWillTerminate:(UIApplication *)application
136136}
137137@end
138138
139+ @interface AppDelegateForDepercatedDidReceiveRemoteNotificationTest : UIResponder <UIApplicationDelegate> {
140+ @public BOOL selectorCalled;
141+ }
142+ @end
143+
144+ @implementation AppDelegateForDepercatedDidReceiveRemoteNotificationTest
145+ - (instancetype )init {
146+ self = [super init ];
147+ selectorCalled = false ;
148+ return self;
149+ }
150+ -(void )application : (UIApplication *)application
151+ didReceiveRemoteNotification : (NSDictionary *)userInfo
152+ {
153+ selectorCalled = true ;
154+ }
155+ @end
156+
139157static id <UIApplicationDelegate> orignalDelegate;
140158
141159@interface UIApplicationDelegateSwizzlingTest : XCTestCase
@@ -280,4 +298,30 @@ - (void)testSwizzleExistingSelectors {
280298 )
281299 ]);
282300}
301+
302+ // OneSignal adds application:didReceiveRemoteNotification:fetchCompletionHandler: however
303+ // this causes iOS to no longer call application:didReceiveRemoteNotification: since it sees
304+ // the delegate is using the newer method. To prevent OneSignal from creating side effects we
305+ // need to forward this event to the deprecated application:didReceiveRemoteNotification:.
306+ /* * From Apple's documenation:
307+ Implement the application:didReceiveRemoteNotification:fetchCompletionHandler:
308+ method instead of this one whenever possible. If your delegate implements both
309+ methods, the app object calls the
310+ application:didReceiveRemoteNotification:fetchCompletionHandler: method.
311+ */
312+ - (void )testCallsDepercatedDidReceiveRemoteNotification {
313+ AppDelegateForDepercatedDidReceiveRemoteNotificationTest* myAppDelegate =
314+ [AppDelegateForDepercatedDidReceiveRemoteNotificationTest new ];
315+ UIApplication.sharedApplication .delegate = myAppDelegate;
316+ id <UIApplicationDelegate> appDelegate = UIApplication.sharedApplication .delegate ;
317+
318+ // Apple will call this AppDelegate method
319+ [appDelegate
320+ application: UIApplication.sharedApplication
321+ didReceiveRemoteNotification: @{}
322+ fetchCompletionHandler: ^(UIBackgroundFetchResult result){}];
323+ // Ensures the OneSignal swizzling code forwarded it to
324+ // application:didReceiveRemoteNotification:
325+ XCTAssertTrue (myAppDelegate->selectorCalled );
326+ }
283327@end
0 commit comments