@@ -362,6 +362,34 @@ - (void)runBackgroundThreads {
362362 [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture ]];
363363}
364364
365+
366+ - (UNNotificationResponse *)createBasiciOSNotificationResponseWithPayload : (NSDictionary *)userInfo {
367+ // Mocking an iOS 10 notification
368+ // Setting response.notification.request.content.userInfo
369+ UNNotificationResponse *notifResponse = [UNNotificationResponse alloc ];
370+ // Normal tap on notification
371+ [notifResponse setValue: @" com.apple.UNNotificationDefaultActionIdentifier" forKeyPath: @" actionIdentifier" ];
372+
373+ UNNotificationContent *unNotifContent = [UNNotificationContent alloc ];
374+ UNNotification *unNotif = [UNNotification alloc ];
375+ UNNotificationRequest *unNotifRequqest = [UNNotificationRequest alloc ];
376+
377+ [unNotif setValue: unNotifRequqest forKeyPath: @" request" ];
378+ [notifResponse setValue: unNotif forKeyPath: @" notification" ];
379+ [unNotifRequqest setValue: unNotifContent forKeyPath: @" content" ];
380+ [unNotifContent setValue: userInfo forKey: @" userInfo" ];
381+
382+ return notifResponse;
383+ }
384+
385+ - (UNNotificationResponse *)createBasiciOSNotificationResponse {
386+ id userInfo = @{@" custom" : @{
387+ @" i" : @" b2f7f966-d8cc-11e4-bed1-df8f05be55bb"
388+ }};
389+
390+ return [self createBasiciOSNotificationResponseWithPayload: userInfo];
391+ }
392+
365393- (void )testBasicInitTest {
366394 NSLog (@" iOS VERSION: %@ " , [[UIDevice currentDevice ] systemVersion ]);
367395
@@ -453,8 +481,6 @@ - (void)testIdsAvailableNotAcceptingNotifications {
453481
454482// Tests that a normal notification opened on iOS 10 triggers the handleNotificationAction.
455483- (void )testNotificationOpen {
456- notifTypesOverride = 7 ;
457-
458484 __block BOOL openedWasFire = false ;
459485
460486 [OneSignal initWithLaunchOptions: nil appId: @" b2f7f966-d8cc-11e4-bed1-df8f05be55ba" handleNotificationAction: ^(OSNotificationOpenedResult *result) {
@@ -465,27 +491,10 @@ - (void)testNotificationOpen {
465491 }];
466492 [self runBackgroundThreads ];
467493
468- // Setting response.notification.request.content.userInfo
469- UNNotificationResponse *notifResponse = [UNNotificationResponse alloc ];
470- // Normal tap on notification
471- [notifResponse setValue: @" com.apple.UNNotificationDefaultActionIdentifier" forKeyPath: @" actionIdentifier" ];
472-
473- id userInfo = @{@" custom" : @{
474- @" i" : @" b2f7f966-d8cc-11e4-bed1-df8f05be55bb"
475- }};
476-
477- UNNotificationContent *unNotifContent = [UNNotificationContent alloc ];
478- UNNotification *unNotif = [UNNotification alloc ];
479- UNNotificationRequest *unNotifRequqest = [UNNotificationRequest alloc ];
480- [unNotif setValue: unNotifRequqest forKeyPath: @" request" ];
481- [notifResponse setValue: unNotif forKeyPath: @" notification" ];
482- [unNotifRequqest setValue: unNotifContent forKeyPath: @" content" ];
483- [unNotifContent setValue: userInfo forKey: @" userInfo" ];
484-
485- // Call iOS 10 selector entry point for a notification that was opened.
494+ id notifResponse = [self createBasiciOSNotificationResponse ];
486495 UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter ];
487496 id notifCenterDelegate = notifCenter.delegate ;
488-
497+ // UNUserNotificationCenterDelegate method iOS 10 calls directly when a notification is opend.
489498 [notifCenterDelegate userNotificationCenter: notifCenter didReceiveNotificationResponse: notifResponse withCompletionHandler: ^() {}];
490499
491500 // Make sure open tracking network call was made.
@@ -504,6 +513,48 @@ - (void)testNotificationOpen {
504513 XCTAssertEqual (networkRequestCount, 2 );
505514}
506515
516+ - (void )testOpeningWithAdditionalData {
517+ __block BOOL openedWasFire = false ;
518+
519+ [OneSignal initWithLaunchOptions: nil appId: @" b2f7f966-d8cc-11e4-bed1-df8f05be55ba" handleNotificationAction: ^(OSNotificationOpenedResult *result) {
520+ XCTAssertEqualObjects (result.notification .payload .additionalData [@" foo" ], @" bar" );
521+ XCTAssertEqual (result.action .type , OSNotificationActionTypeOpened);
522+ XCTAssertNil (result.action .actionID );
523+ openedWasFire = true ;
524+ }];
525+ [self runBackgroundThreads ];
526+
527+ id userInfo = @{@" custom" : @{
528+ @" i" : @" b2f7f966-d8cc-11e4-bed1-df8f05be55bb" ,
529+ @" a" : @{ @" foo" : @" bar" }
530+ }};
531+
532+ id notifResponse = [self createBasiciOSNotificationResponseWithPayload: userInfo];
533+ UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter ];
534+ id notifCenterDelegate = notifCenter.delegate ;
535+
536+ // UNUserNotificationCenterDelegate method iOS 10 calls directly when a notification is opend.
537+ [notifCenterDelegate userNotificationCenter: notifCenter didReceiveNotificationResponse: notifResponse withCompletionHandler: ^() {}];
538+ XCTAssertEqual (openedWasFire, true );
539+
540+
541+ // Part 2 - New paylaod test
542+ // Current mocking isn't able to setup this test correctly.
543+ // In an app AppDelete selectors fire instead of UNUserNotificationCenter
544+ // SDK could also used some refactoring as this should't have an effect.
545+ /*
546+ openedWasFire = false;
547+ userInfo = @{@"alert": @"body",
548+ @"os_data": @{
549+ @"i": @"b2f7f966-d8cc-11e4-bed1-df8f05be55bc"
550+ },
551+ @"foo": @"bar"};
552+ notifResponse = [self createBasiciOSNotificationResponseWithPayload:userInfo];
553+ [notifCenterDelegate userNotificationCenter:notifCenter didReceiveNotificationResponse:notifResponse withCompletionHandler:^() {}];
554+ XCTAssertEqual(openedWasFire, true);
555+ */
556+ }
557+
507558- (void )testSendTags {
508559 [OneSignal initWithLaunchOptions: nil appId: @" b2f7f966-d8cc-11e4-bed1-df8f05be55ba" ];
509560 [self runBackgroundThreads ];
0 commit comments