Skip to content

Commit c3ab33f

Browse files
authored
Fix way of saving redisplayMessages on UserDefaults (#634)
* Custom objects need to use NSKeyedArchiver and implement NSCoding * Fix implementation on OSInAppMessageDisplayStats and OSInAppMessage
1 parent 953865f commit c3ab33f

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

iOS_SDK/OneSignalSDK/Source/OSInAppMessage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
NS_ASSUME_NONNULL_BEGIN
3636

37-
@interface OSInAppMessage : NSObject <OSJSONDecodable, OSJSONEncodable>
37+
@interface OSInAppMessage : NSObject <NSCoding, OSJSONDecodable, OSJSONEncodable>
3838

3939
@property (strong, nonatomic, nonnull) NSString *messageId;
4040
@property (strong, nonatomic, nonnull) NSDictionary<NSString *, NSDictionary <NSString *, NSString *> *> *variants;

iOS_SDK/OneSignalSDK/Source/OSInAppMessage.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ + (instancetype)instanceWithJson:(NSDictionary * _Nonnull)json {
101101
message.displayStats = [OSInAppMessageDisplayStats instanceWithJson:json[@"redisplay"]];
102102
else
103103
message.displayStats = [[OSInAppMessageDisplayStats alloc] init];
104-
104+
105105
if (json[@"triggers"] && [json[@"triggers"] isKindOfClass:[NSArray class]]) {
106106
let triggers = [NSMutableArray new];
107107

@@ -195,13 +195,15 @@ - (void)encodeWithCoder:(NSCoder *)encoder {
195195
[encoder encodeObject:_messageId forKey:@"messageId"];
196196
[encoder encodeObject:_variants forKey:@"variants"];
197197
[encoder encodeObject:_triggers forKey:@"triggers"];
198+
[encoder encodeObject:_displayStats forKey:@"displayStats"];
198199
}
199200

200201
- (id)initWithCoder:(NSCoder *)decoder {
201202
if (self = [super init]) {
202203
_messageId = [decoder decodeObjectForKey:@"messageId"];
203204
_variants = [decoder decodeObjectForKey:@"variants"];
204205
_triggers = [decoder decodeObjectForKey:@"triggers"];
206+
_displayStats = [decoder decodeObjectForKey:@"displayStats"];
205207
}
206208
return self;
207209
}

iOS_SDK/OneSignalSDK/Source/OSInAppMessageDisplayStats.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#import "OneSignal.h"
2929
#import "OSJSONHandling.h"
3030

31-
@interface OSInAppMessageDisplayStats : NSObject <OSJSONDecodable, OSJSONEncodable>
31+
@interface OSInAppMessageDisplayStats : NSObject <NSCoding, OSJSONDecodable, OSJSONEncodable>
3232

3333
//Last IAM display time in seconds
3434
@property (nonatomic, readwrite) double lastDisplayTime;

iOS_SDK/OneSignalSDK/Source/OSInAppMessageDisplayStats.m

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ + (instancetype)instanceWithJson:(NSDictionary * _Nonnull)json {
8888
return displayStats;
8989
}
9090

91+
-(NSDictionary *)jsonRepresentation {
92+
let json = [NSMutableDictionary new];
93+
94+
json[@"limit"] = @(_displayLimit);
95+
json[@"delay"] = @(_displayDelay);
96+
97+
return json;
98+
}
99+
91100
- (BOOL)isDelayTimeSatisfied:(NSTimeInterval)date {
92101
if (_lastDisplayTime < 0) {
93102
return true;
@@ -113,13 +122,21 @@ - (NSString *)description {
113122
return [NSString stringWithFormat:@"OSInAppMessageDisplayStats: redisplayEnabled: %@ \nlastDisplayTime: %f \ndisplayDelay: %f \ndisplayQuantity: %ld \ndisplayLimit: %ld", self.redisplayEnabled ? @"YES" : @"NO", self.lastDisplayTime, self.displayDelay, (long)self.displayQuantity, (long)self.displayLimit];
114123
}
115124

116-
- (NSDictionary * _Nonnull)jsonRepresentation {
117-
let json = [NSMutableDictionary new];
118-
119-
json[@"limit"] = @(self.displayLimit);
120-
json[@"delay"] = @(self.displayDelay);
121-
122-
return json;
125+
- (void)encodeWithCoder:(NSCoder *)encoder {
126+
[encoder encodeInteger:_displayLimit forKey:@"displayLimit"];
127+
[encoder encodeInteger:_displayQuantity forKey:@"displayQuantity"];
128+
[encoder encodeDouble:_displayDelay forKey:@"displayDelay"];
129+
[encoder encodeDouble:_lastDisplayTime forKey:@"lastDisplayTime"];
130+
}
131+
132+
- (id)initWithCoder:(NSCoder *)decoder {
133+
if (self = [super init]) {
134+
_displayLimit = [decoder decodeIntegerForKey:@"displayLimit"];
135+
_displayQuantity = [decoder decodeIntegerForKey:@"displayQuantity"];
136+
_displayDelay = [decoder decodeDoubleForKey:@"displayDelay"];
137+
_lastDisplayTime = [decoder decodeDoubleForKey:@"lastDisplayTime"];
138+
}
139+
return self;
123140
}
124141

125142
@end

iOS_SDK/OneSignalSDK/Source/OSMessagingController.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ - (instancetype)init {
130130

131131
// Get all cached IAM data from NSUserDefaults for shown, impressions, and clicks
132132
self.seenInAppMessages = [[NSMutableSet alloc] initWithSet:[standardUserDefaults getSavedSetForKey:OS_IAM_SEEN_SET_KEY defaultValue:nil]];
133-
self.redisplayedInAppMessages = [[NSMutableDictionary alloc] initWithDictionary:[standardUserDefaults getSavedDictionaryForKey:OS_IAM_REDISPLAY_DICTIONARY defaultValue:[NSMutableDictionary new]]];
133+
self.redisplayedInAppMessages = [[NSMutableDictionary alloc] initWithDictionary:[standardUserDefaults getSavedCodeableDataForKey:OS_IAM_REDISPLAY_DICTIONARY defaultValue:[NSMutableDictionary new]]];
134+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"init redisplayedInAppMessages with: %@", [_redisplayedInAppMessages description]]];
134135
self.clickedClickIds = [[NSMutableSet alloc] initWithSet:[standardUserDefaults getSavedSetForKey:OS_IAM_CLICKED_SET_KEY defaultValue:nil]];
135136
self.impressionedInAppMessages = [[NSMutableSet alloc] initWithSet:[standardUserDefaults getSavedSetForKey:OS_IAM_IMPRESSIONED_SET_KEY defaultValue:nil]];
136137

@@ -330,8 +331,11 @@ - (void)setDataForRedisplay:(OSInAppMessage *)message {
330331

331332
BOOL messageDismissed = [_seenInAppMessages containsObject:message.messageId];
332333
let redisplayMessageSavedData = [_redisplayedInAppMessages objectForKey:message.messageId];
333-
334-
if (messageDismissed && redisplayMessageSavedData != nil) {
334+
335+
NSLog(@"Redisplay dismissed: %@ and data: %@", messageDismissed ? @"YES" : @"NO", redisplayMessageSavedData.jsonRepresentation.description);
336+
337+
if (messageDismissed && redisplayMessageSavedData) {
338+
NSLog(@"Redisplay IAM: %@", message.jsonRepresentation.description);
335339
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"setDataForRedisplay with message: %@", message]];
336340
message.displayStats.displayQuantity = redisplayMessageSavedData.displayStats.displayQuantity;
337341
message.displayStats.lastDisplayTime = redisplayMessageSavedData.displayStats.lastDisplayTime;
@@ -469,8 +473,8 @@ - (void)persistInAppMessageForRedisplay:(OSInAppMessage *)message {
469473
// Update the data to enable future re displays
470474
// Avoid calling the userdefault data again
471475
[_redisplayedInAppMessages setObject:message forKey:message.messageId];
472-
473-
[OneSignalUserDefaults.initStandard saveDictionaryForKey:OS_IAM_REDISPLAY_DICTIONARY withValue:self.redisplayedInAppMessages];
476+
477+
[OneSignalUserDefaults.initStandard saveCodeableDataForKey:OS_IAM_REDISPLAY_DICTIONARY withValue:_redisplayedInAppMessages];
474478
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"persistInAppMessageForRedisplay: %@ \nredisplayedInAppMessages: %@", [message description], [_redisplayedInAppMessages description]]];
475479
}
476480

0 commit comments

Comments
 (0)