Skip to content

Commit

Permalink
Fix redisplay on same session with no triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeasmine committed Mar 24, 2020
1 parent a673945 commit 76e9f0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 6 additions & 8 deletions iOS_SDK/OneSignalSDK/Source/OSInAppMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ + (instancetype)instanceWithJson:(NSDictionary * _Nonnull)json {
}
else
return nil;

//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
if (json[@"displayed_in_session"]) {
message.isDisplayedInSession = json[@"displayed_in_session"];
}

return message;
}

Expand Down Expand Up @@ -163,14 +159,12 @@ -(NSDictionary *)jsonRepresentation {
if ([_displayStats isRedisplayEnabled]) {
json[@"redisplay"] = [_displayStats jsonRepresentation];
}
//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
json[@"displayed_in_session"] = @(_isDisplayedInSession);

return json;
}

- (NSString *)description {
return [NSString stringWithFormat:@"OSInAppMessage: \nmessageId: %@ \ntriggers: %@ \ndisplayStats: %@", self.messageId, self.triggers, self.displayStats];
return [NSString stringWithFormat:@"OSInAppMessage: \nmessageId: %@ \ntriggers: %@ \ndisplayed_in_session: %@ \ndisplayStats: %@", self.messageId, self.triggers, self.isDisplayedInSession ? @"YES" : @"NO", self.displayStats];
}

- (BOOL)isEqual:(id)object {
Expand All @@ -196,6 +190,8 @@ - (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:_variants forKey:@"variants"];
[encoder encodeObject:_triggers forKey:@"triggers"];
[encoder encodeObject:_displayStats forKey:@"displayStats"];
//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
[encoder encodeBool:_isDisplayedInSession forKey:@"displayed_in_session"];
}

- (id)initWithCoder:(NSCoder *)decoder {
Expand All @@ -204,6 +200,8 @@ - (id)initWithCoder:(NSCoder *)decoder {
_variants = [decoder decodeObjectForKey:@"variants"];
_triggers = [decoder decodeObjectForKey:@"triggers"];
_displayStats = [decoder decodeObjectForKey:@"displayStats"];
//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
_isDisplayedInSession = [decoder decodeBoolForKey:@"displayed_in_session"];
}
return self;
}
Expand Down
14 changes: 12 additions & 2 deletions iOS_SDK/OneSignalSDK/Source/OSMessagingController.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ - (instancetype)init {

- (void)updateInAppMessagesFromCache {
self.messages = [OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_MESSAGES_ARRAY defaultValue:[NSArray new]];

[self evaluateMessages];
}

- (void)updateInAppMessagesFromOnSession:(NSArray<OSInAppMessage *> *)newMessages {
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"updateInAppMessagesFromOnSession"];
self.messages = newMessages;

// Cache if messages passed in are not null, this method is called from on_session for
Expand All @@ -167,6 +169,8 @@ - (void)updateInAppMessagesFromOnSession:(NSArray<OSInAppMessage *> *)newMessage
}

- (void)resetRedisplayMessagesBySession {
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"resetRedisplayMessagesBySession with redisplayedInAppMessages: %@", [_redisplayedInAppMessages description]]];

for (NSString *messageId in _redisplayedInAppMessages) {
[_redisplayedInAppMessages objectForKey:messageId].isDisplayedInSession = false;
}
Expand Down Expand Up @@ -347,12 +351,13 @@ - (void)setDataForRedisplay:(OSInAppMessage *)message {

if (messageDismissed && redisplayMessageSavedData) {
NSLog(@"Redisplay IAM: %@", message.jsonRepresentation.description);
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"setDataForRedisplay with message: %@", message]];
message.displayStats.displayQuantity = redisplayMessageSavedData.displayStats.displayQuantity;
message.displayStats.lastDisplayTime = redisplayMessageSavedData.displayStats.lastDisplayTime;

// Message that don't have triggers should display only once per session
BOOL triggerHasChanged = message.isTriggerChanged || (!redisplayMessageSavedData.isDisplayedInSession && [message.triggers count] == 0);

[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"setDataForRedisplay with message: %@ \ntriggerHasChanged: %@ \nno triggers: %@ \ndisplayed in session saved: %@", message, message.isTriggerChanged ? @"YES" : @"NO", [message.triggers count] == 0 ? @"YES" : @"NO", redisplayMessageSavedData.isDisplayedInSession ? @"YES" : @"NO"]];
// Check if conditions are correct for redisplay
if (triggerHasChanged &&
[message.displayStats isDelayTimeSatisfied:self.dateGenerator()] &&
Expand Down Expand Up @@ -495,7 +500,12 @@ - (void)persistInAppMessageForRedisplay:(OSInAppMessage *)message {
[_redisplayedInAppMessages setObject:message forKey:message.messageId];

[OneSignalUserDefaults.initStandard saveCodeableDataForKey:OS_IAM_REDISPLAY_DICTIONARY withValue:_redisplayedInAppMessages];
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"persistInAppMessageForRedisplay: %@ \nredisplayedInAppMessages: %@", [message description], [_redisplayedInAppMessages description]]];
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"persistInAppMessageForRedisplay: %@ \nredisplayedInAppMessages: %@", [message description], _redisplayedInAppMessages]];

let standardUserDefaults = OneSignalUserDefaults.initStandard;
let redisplayedInAppMessages = [[NSMutableDictionary alloc] initWithDictionary:[standardUserDefaults getSavedCodeableDataForKey:OS_IAM_REDISPLAY_DICTIONARY defaultValue:[NSMutableDictionary new]]];

[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"persistInAppMessageForRedisplay saved redisplayedInAppMessages: %@", [redisplayedInAppMessages description]]];
}

- (void)handlePromptActions:(NSArray<NSObject<OSInAppMessagePrompt> *> *)promptActions {
Expand Down

0 comments on commit 76e9f0c

Please sign in to comment.