Skip to content

Late setNotificationOpenedHandler call processing #826

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,17 @@ @implementation OneSignalHelper
NSDictionary* lastMessageReceived;
UIBackgroundTaskIdentifier mediaBackgroundTask;

static NSMutableArray<OSNotificationOpenedResult*> *unprocessedOpenedNotifis;

+ (void)resetLocals {
[OneSignalHelper lastMessageReceived:nil];
_lastMessageIdFromAction = nil;
lastMessageID = @"";

notificationWillShowInForegroundHandler = nil;
notificationOpenedHandler = nil;

unprocessedOpenedNotifis = nil;
}

OSNotificationWillShowInForegroundBlock notificationWillShowInForegroundHandler;
Expand All @@ -231,6 +235,27 @@ + (void)setNotificationWillShowInForegroundBlock:(OSNotificationWillShowInForegr
OSNotificationOpenedBlock notificationOpenedHandler;
+ (void)setNotificationOpenedBlock:(OSNotificationOpenedBlock)block {
notificationOpenedHandler = block;
[self fireNotificationOpenedHandlerForUnprocessedEvents];
}

+ (NSMutableArray<OSNotificationOpenedResult*>*)getUnprocessedOpenedNotifis {
if (!unprocessedOpenedNotifis)
unprocessedOpenedNotifis = [NSMutableArray new];
return unprocessedOpenedNotifis;
}

+ (void)addUnprocessedOpenedNotifi:(OSNotificationOpenedResult*)result {
[[self getUnprocessedOpenedNotifis] addObject:result];
}

+ (void)fireNotificationOpenedHandlerForUnprocessedEvents {
if (!notificationOpenedHandler)
return;

for (OSNotificationOpenedResult* notification in [self getUnprocessedOpenedNotifis]) {
notificationOpenedHandler(notification);
}
unprocessedOpenedNotifis = [NSMutableArray new];
}

//Passed to the OnFocus to make sure dismissed when coming back into app
Expand Down Expand Up @@ -358,8 +383,10 @@ + (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(

[OneSignalTrackFirebaseAnalytics trackOpenEvent:result];

if (!notificationOpenedHandler)
if (!notificationOpenedHandler) {
[self addUnprocessedOpenedNotifi:result];
return;
}
notificationOpenedHandler(result);
}

Expand Down
27 changes: 27 additions & 0 deletions iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,33 @@ - (void)testNotificationOpen {
XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 3);
}

/**
Ensures that if a developer calls OneSignal.setNotificationOpenedHandler late (after didFinishLaunchingWithOptionst)
and the app was cold started from opening a notficiation open that the developer's handler will still fire.
This is particularly helpful for the OneSignal wrapper SDKs so special logic isn't needed in each one.
*/
- (void)testNotificationOpenedHandler_setAfter_didFinishLaunchingWithOptions {
// 1. Init OneSignal with app start
[UnitTestCommonMethods initOneSignal];
[UnitTestCommonMethods runBackgroundThreads];

// 2. Simulate a notification being opened
let notifResponse = [self createBasiciOSNotificationResponse];
let notifCenter = UNUserNotificationCenter.currentNotificationCenter;
let notifCenterDelegate = notifCenter.delegate;
[notifCenterDelegate userNotificationCenter:notifCenter didReceiveNotificationResponse:notifResponse withCompletionHandler:^() {}];

// 3. Setup OneSignal.setNotificationOpenedHandler
__block BOOL openedWasFire = false;
[OneSignal setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
openedWasFire = true;
}];
// 4. Wait for open event to fire
[UnitTestCommonMethods runBackgroundThreads];

// 5. Ensure the OneSignal public callback fired
XCTAssertTrue(openedWasFire);
}

- (UNNotificationResponse*)createNotificationResponseForAnalyticsTests {
id userInfo = @{@"custom":
Expand Down