Skip to content

Commit 429c258

Browse files
authored
Merge pull request #1326 from OneSignal/5.x.x/fix/forward_opens_from_non_onesignal_notifs
[v5] Fix forwarding notification opens from non onesignal notifs
2 parents f358fc1 + 35c4875 commit 429c258

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -308,37 +308,13 @@ void finishProcessingNotification(UNNotification *notification,
308308
completionHandler(completionHandlerOptions);
309309
}
310310

311-
// Apple's docs - Called to let your app know which action was selected by the user for a given notification.
312-
- (void)onesignalUserNotificationCenter:(UNUserNotificationCenter *)center
313-
didReceiveNotificationResponse:(UNNotificationResponse *)response
314-
withCompletionHandler:(void(^)(void))completionHandler {
315-
[OneSignalNotificationsUNUserNotificationCenter traceCall:@"onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:"];
316-
// return if the user has not granted privacy permissions or if not a OneSignal payload
317-
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil] || ![OneSignalCoreHelper isOneSignalPayload:response.notification.request.content.userInfo]) {
318-
SwizzlingForwarder *forwarder = [[SwizzlingForwarder alloc]
319-
initWithTarget:self
320-
withYourSelector:@selector(
321-
onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
322-
)
323-
withOriginalSelector:@selector(
324-
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
325-
)
326-
];
327-
if (forwarder.hasReceiver) {
328-
[forwarder invokeWithArgs:@[center, response, completionHandler]];
329-
} else {
330-
completionHandler();
331-
}
332-
return;
333-
}
334-
335-
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: Fired!"];
336-
337-
[OneSignalNotificationsUNUserNotificationCenter processiOS10Open:response];
338-
339-
// Call orginal selector if one was set.
311+
+ (void)forwardReceivedNotificationResponseWithCenter:(UNUserNotificationCenter *)center
312+
didReceiveNotificationResponse:(UNNotificationResponse *)response
313+
OneSignalCenter:(id)instance
314+
withCompletionHandler:(void(^)(void))completionHandler {
315+
// Call original selector if one was set.
340316
SwizzlingForwarder *forwarder = [[SwizzlingForwarder alloc]
341-
initWithTarget:self
317+
initWithTarget:instance
342318
withYourSelector:@selector(
343319
onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
344320
)
@@ -365,6 +341,21 @@ - (void)onesignalUserNotificationCenter:(UNUserNotificationCenter *)center
365341
completionHandler();
366342
}
367343

344+
// Apple's docs - Called to let your app know which action was selected by the user for a given notification.
345+
- (void)onesignalUserNotificationCenter:(UNUserNotificationCenter *)center
346+
didReceiveNotificationResponse:(UNNotificationResponse *)response
347+
withCompletionHandler:(void(^)(void))completionHandler {
348+
[OneSignalNotificationsUNUserNotificationCenter traceCall:@"onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:"];
349+
350+
if (![OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil] && [OneSignalCoreHelper isOneSignalPayload:response.notification.request.content.userInfo]) {
351+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: Fired!"];
352+
353+
[OneSignalNotificationsUNUserNotificationCenter processiOS10Open:response];
354+
}
355+
356+
[OneSignalNotificationsUNUserNotificationCenter forwardReceivedNotificationResponseWithCenter:center didReceiveNotificationResponse:response OneSignalCenter:self withCompletionHandler:completionHandler];
357+
}
358+
368359
+ (BOOL)isDismissEvent:(UNNotificationResponse *)response {
369360
return [@"com.apple.UNNotificationDismissActionIdentifier" isEqual:response.actionIdentifier];
370361
}

iOS_SDK/OneSignalSDK/UnitTests/UIApplicationDelegateSwizzlingTests.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,47 @@ - (void)testCallsDepercatedDidReceiveRemoteNotification {
562562
XCTAssertTrue(myAppDelegate->selectorCalled);
563563
}
564564

565+
- (UNNotificationResponse*)createOneSignalNotificationResponse {
566+
id userInfo = @{@"custom":
567+
@{ @"i": @"b2f7f966-d8cc-11e4-bed1-df8f05be55ba" }
568+
};
569+
570+
return [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:userInfo];
571+
}
572+
573+
- (UNNotificationResponse*)createNonOneSignalNotificationResponse {
574+
return [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:@{}];
575+
}
576+
577+
- (void)testNotificationOpenForwardsToLegacySelector {
578+
AppDelegateForExistingSelectorsTest* myAppDelegate = [AppDelegateForExistingSelectorsTest new];
579+
UIApplication.sharedApplication.delegate = myAppDelegate;
580+
581+
id notifResponse = [self createOneSignalNotificationResponse];
582+
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
583+
id notifCenterDelegate = notifCenter.delegate;
584+
// UNUserNotificationCenterDelegate method iOS 10 calls directly when a notification is opened.
585+
[notifCenterDelegate userNotificationCenter:notifCenter didReceiveNotificationResponse:notifResponse withCompletionHandler:^() {}];
586+
XCTAssertTrue([myAppDelegate->selectorCallsDict
587+
objectForKey:NSStringFromSelector(
588+
@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)
589+
)
590+
]);
591+
XCTAssertEqual([OneSignalAppDelegateOverrider callCountForSelector:@"oneSignalReceiveRemoteNotification:UserInfo:fetchCompletionHandler:"], 1);
592+
593+
notifResponse = [self createNonOneSignalNotificationResponse];
594+
notifCenter = [UNUserNotificationCenter currentNotificationCenter];
595+
notifCenterDelegate = notifCenter.delegate;
596+
// UNUserNotificationCenterDelegate method iOS 10 calls directly when a notification is opened.
597+
[notifCenterDelegate userNotificationCenter:notifCenter didReceiveNotificationResponse:notifResponse withCompletionHandler:^() {}];
598+
XCTAssertTrue([myAppDelegate->selectorCallsDict
599+
objectForKey:NSStringFromSelector(
600+
@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)
601+
)
602+
]);
603+
XCTAssertEqual([OneSignalAppDelegateOverrider callCountForSelector:@"oneSignalReceiveRemoteNotification:UserInfo:fetchCompletionHandler:"], 2);
604+
}
605+
565606
- (void)testAppDelegateInheritsFromBaseMissingSelectors {
566607
id myAppDelegate = [AppDelegateInheritsFromBaseMissingSelectorsTest new];
567608
UIApplication.sharedApplication.delegate = myAppDelegate;

0 commit comments

Comments
 (0)