Skip to content

Commit

Permalink
Merge pull request #719 from OneSignal/fix/notif_received_when_app_in…
Browse files Browse the repository at this point in the history
…active

Fixing notification received when App is Inactive
  • Loading branch information
emawby authored Aug 6, 2020
2 parents cd42203 + 0a9fadd commit 2e4a36c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
11 changes: 5 additions & 6 deletions iOS_SDK/OneSignalSDK/Source/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -2243,15 +2243,14 @@ + (BOOL)remoteSilentNotification:(UIApplication*)application UserInfo:(NSDiction
}
}
// Method was called due to a tap on a notification - Fire open notification
else if (application.applicationState != UIApplicationStateBackground) {
else if (application.applicationState == UIApplicationStateActive) {
[OneSignalHelper lastMessageReceived:userInfo];

if (application.applicationState == UIApplicationStateActive)
[OneSignalHelper handleNotificationReceived:OSNotificationDisplayTypeNotification fromBackground:NO];

if (![OneSignalHelper isRemoteSilentNotification:userInfo])
[OneSignal notificationReceived:userInfo foreground:application.applicationState == UIApplicationStateActive isActive:NO wasOpened:YES];
[OneSignalHelper handleNotificationReceived:OSNotificationDisplayTypeNotification fromBackground:NO];

if (![OneSignalHelper isRemoteSilentNotification:userInfo]) {
[OneSignal notificationReceived:userInfo foreground:YES isActive:NO wasOpened:YES];
}
return startedBackgroundJob;
}
// content-available notification received in the background - Fire handleNotificationReceived block in app
Expand Down
3 changes: 2 additions & 1 deletion iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ NSString * serverUrlWithPath(NSString *path);
+ (void)beforeEachTest:(XCTestCase *)testCase;
+ (void)clearStateForAppRestart:(XCTestCase *)testCase;
+ (UNNotificationResponse*)createBasiciOSNotificationResponseWithPayload:(NSDictionary*)userInfo;
+ (UNNotification *)createBasiciOSNotificationWithPayload:(NSDictionary *)userInfo;
+ (void)answerNotificationPrompt:(BOOL)accept;
+ (void)setCurrentNotificationPermission:(BOOL)accepted;
+ (void)receiveNotification:(NSString*)notificationId wasOpened:(BOOL)opened;
+ (void)handleNotificationReceived:(NSString*)notificationId messageDict:(NSDictionary*)messageDict wasOpened:(BOOL)opened;
+ (void)handleNotificationReceived:(NSDictionary*)messageDict wasOpened:(BOOL)opened;
+ (XCTestCase*)currentXCTestCase;
@end

Expand Down
15 changes: 9 additions & 6 deletions iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,21 @@ + (UNNotificationResponse*)createBasiciOSNotificationResponseWithPayload:(NSDict
// Normal tap on notification
[notifResponse setValue:@"com.apple.UNNotificationDefaultActionIdentifier" forKeyPath:@"actionIdentifier"];

[notifResponse setValue:[self createBasiciOSNotificationWithPayload:userInfo] forKeyPath:@"notification"];

return notifResponse;
}

+ (UNNotification *)createBasiciOSNotificationWithPayload:(NSDictionary *)userInfo {
UNNotificationContent *unNotifContent = [UNNotificationContent alloc];
UNNotification *unNotif = [UNNotification alloc];
UNNotificationRequest *unNotifRequqest = [UNNotificationRequest alloc];
// Set as remote push type
[unNotifRequqest setValue:[UNPushNotificationTrigger alloc] forKey:@"trigger"];

[unNotif setValue:unNotifRequqest forKeyPath:@"request"];
[notifResponse setValue:unNotif forKeyPath:@"notification"];
[unNotifRequqest setValue:unNotifContent forKeyPath:@"content"];
[unNotifContent setValue:userInfo forKey:@"userInfo"];

return notifResponse;
[unNotifRequqest setValue:unNotifContent forKeyPath:@"content"];
[unNotif setValue:unNotifRequqest forKeyPath:@"request"];
return unNotif;
}

+ (void)clearStateForAppRestart:(XCTestCase *)testCase {
Expand Down
36 changes: 36 additions & 0 deletions iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,42 @@ - (void)testNotificationOpenFromButtonPressWithNewformat {
XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 3);
}

// Testing receiving a notification while the app is in the foreground but inactive.
// Received should be called but opened should not be called
- (void)testNotificationReceivedWhileAppInactive {
__block BOOL openedWasFired = false;
__block BOOL receivedWasFired = false;

[OneSignal initWithLaunchOptions:nil appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba" handleNotificationReceived:^(OSNotification *notification) {
receivedWasFired = true;
} handleNotificationAction:^(OSNotificationOpenedResult *result) {
openedWasFired = true;
} settings:nil];

[UnitTestCommonMethods runBackgroundThreads];
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateInactive;

id userInfo = @{@"aps": @{
@"mutable-content": @1,
@"alert": @"Message Body"
},
@"os_data": @{
@"i": @"b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
@"buttons": @[@{@"i": @"id1", @"n": @"text1"}],
}};

UNNotification *notif = [UnitTestCommonMethods createBasiciOSNotificationWithPayload:userInfo];

UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
id notifCenterDelegate = notifCenter.delegate;

[notifCenterDelegate userNotificationCenter:notifCenter willPresentNotification:notif withCompletionHandler:^(UNNotificationPresentationOptions options) {}];


XCTAssertEqual(openedWasFired, false);
XCTAssertEqual(receivedWasFired, true);
}

// Testing iOS 10 - 2.4.0+ button fromat - with os_data aps payload format
- (void)notificationAlertButtonsDisplayWithFormat:(NSDictionary *)userInfo {
[[OneSignalDialogController sharedInstance] clearQueue];
Expand Down

0 comments on commit 2e4a36c

Please sign in to comment.