Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing notification received when App is Inactive #719

Merged
merged 2 commits into from Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions iOS_SDK/OneSignalSDK/Source/OneSignal.m
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
jkasten2 marked this conversation as resolved.
Show resolved Hide resolved
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
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
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
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