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

Respect duration since last message trigger on app close and relaunch #1070

Merged
merged 1 commit into from
Apr 7, 2022
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
1 change: 1 addition & 0 deletions iOS_SDK/OneSignalSDK/Source/OSInAppMessagingDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef NS_ENUM(NSUInteger, OSTriggerOperatorType) {
#define OS_IAM_PAGE_IMPRESSIONED_SET_KEY @"OS_IAM_PAGE_IMPRESSIONED_SET"
#define OS_IAM_MESSAGES_ARRAY @"OS_IAM_MESSAGES_ARRAY"
#define OS_IAM_REDISPLAY_DICTIONARY @"OS_IAM_REDISPLAY_DICTIONARY"
#define OS_IAM_TIME_SINCE_LAST_MESSAGE_KEY @"OS_IAM_TIME_SINCE_LAST_MESSAGE"

// Dynamic trigger kind types
#define OS_DYNAMIC_TRIGGER_KIND_CUSTOM @"custom"
Expand Down
22 changes: 19 additions & 3 deletions iOS_SDK/OneSignalSDK/Source/OSMessagingController.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ - (instancetype)init {
};
self.messages = [OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_MESSAGES_ARRAY
defaultValue:[NSArray<OSInAppMessageInternal *> new]];
self.triggerController = [OSTriggerController new];
self.triggerController.delegate = self;
[self initializeTriggerController];
self.messageDisplayQueue = [NSMutableArray new];

let standardUserDefaults = OneSignalUserDefaults.initStandard;
Expand All @@ -160,6 +159,14 @@ - (instancetype)init {
return self;
}

- (void)initializeTriggerController {
self.triggerController = [OSTriggerController new];
self.triggerController.delegate = self;
NSString *timeSinceLastMessage = [OneSignalUserDefaults.initShared getSavedStringForKey:OS_IAM_TIME_SINCE_LAST_MESSAGE_KEY defaultValue:nil];
[self.triggerController timeSinceLastMessage:[[NSDateFormatter iso8601DateFormatter]
dateFromString:timeSinceLastMessage]];
}

- (void)updateInAppMessagesFromCache {
self.messages = [OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_MESSAGES_ARRAY defaultValue:[NSArray new]];
[self evaluateMessages];
Expand Down Expand Up @@ -602,7 +609,7 @@ - (void)messageViewControllerWasDismissed:(OSInAppMessageInternal *)message disp
// Reset the IAM viewController to prepare for next IAM if one exists
self.viewController = nil;
// Reset time since last IAM
[self.triggerController timeSinceLastMessage:[NSDate new]];
[self setAndPersistTimeSinceLastMessage];

if (!_currentPromptAction) {
[self evaluateMessageDisplayQueue];
Expand All @@ -612,6 +619,15 @@ - (void)messageViewControllerWasDismissed:(OSInAppMessageInternal *)message disp
}
}

- (void)setAndPersistTimeSinceLastMessage {
NSDate *timeSinceLastMessage = [NSDate new];
[self.triggerController timeSinceLastMessage:timeSinceLastMessage];
NSString *stringTimeSinceLastMessage = [[NSDateFormatter iso8601DateFormatter]
stringFromDate:timeSinceLastMessage];
[OneSignalUserDefaults.initShared saveStringForKey:OS_IAM_TIME_SINCE_LAST_MESSAGE_KEY
withValue:stringTimeSinceLastMessage];
}

- (void)evaluateMessageDisplayQueue {
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"Evaluating message display queue"];
// No IAMs are showing currently
Expand Down
3 changes: 3 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/OSTriggerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ - (BOOL)messageHasOnlyDynamicTriggers:(OSInAppMessageInternal *)message {
#pragma mark Private Methods

- (void)timeSinceLastMessage:(NSDate *)date {
if (date == nil) {
date = [NSDate distantPast];
}
self.dynamicTriggerController.timeSinceLastMessage = date;
}

Expand Down