Skip to content

Commit 88829ff

Browse files
committed
checking for end_time before displaying IAM
This change involves reading in end_time from JSON when creating an IAM so that we can decide to not show the IAM (Cached case) if the IAM should be finished. The date comes in iso8601 format so I added a NSDateFormatter category with that format to easily retrieve it. Note we cannot use the Apple provided iso8601 date formatter class until we drop iOS 9 support.
1 parent e023ec8 commit 88829ff

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@
432432
DE20425F24E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
433433
DE20426024E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
434434
DE5EFECA24D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m in Sources */ = {isa = PBXBuildFile; fileRef = DE5EFEC924D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m */; };
435+
DE98772B2591656200DE07D5 /* NSDateFormatter+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE98772A2591655800DE07D5 /* NSDateFormatter+OneSignal.m */; };
436+
DE9877332591656200DE07D5 /* NSDateFormatter+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE98772A2591655800DE07D5 /* NSDateFormatter+OneSignal.m */; };
437+
DE9877342591656300DE07D5 /* NSDateFormatter+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE98772A2591655800DE07D5 /* NSDateFormatter+OneSignal.m */; };
438+
DE98773C2591656A00DE07D5 /* NSDateFormatter+OneSignal.h in Headers */ = {isa = PBXBuildFile; fileRef = DE9877292591654600DE07D5 /* NSDateFormatter+OneSignal.h */; };
435439
DEE8198D24E21DF000868CBA /* UIApplication+OneSignal.h in Headers */ = {isa = PBXBuildFile; fileRef = DE20425C24E21C1500350E4F /* UIApplication+OneSignal.h */; };
436440
/* End PBXBuildFile section */
437441

@@ -713,6 +717,8 @@
713717
DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+OneSignal.m"; sourceTree = "<group>"; };
714718
DE5EFEC924D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSInAppMessageViewControllerOverrider.m; sourceTree = "<group>"; };
715719
DE5EFECB24D8DC0E0032632D /* OSInAppMessageViewControllerOverrider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageViewControllerOverrider.h; sourceTree = "<group>"; };
720+
DE9877292591654600DE07D5 /* NSDateFormatter+OneSignal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSDateFormatter+OneSignal.h"; sourceTree = "<group>"; };
721+
DE98772A2591655800DE07D5 /* NSDateFormatter+OneSignal.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSDateFormatter+OneSignal.m"; sourceTree = "<group>"; };
716722
/* End PBXFileReference section */
717723

718724
/* Begin PBXFrameworksBuildPhase section */
@@ -1119,6 +1125,8 @@
11191125
CA1A6E6E20DC2E73001C41B9 /* OneSignalDialogRequest.m */,
11201126
DE20425C24E21C1500350E4F /* UIApplication+OneSignal.h */,
11211127
DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */,
1128+
DE9877292591654600DE07D5 /* NSDateFormatter+OneSignal.h */,
1129+
DE98772A2591655800DE07D5 /* NSDateFormatter+OneSignal.m */,
11221130
);
11231131
name = Categories;
11241132
sourceTree = "<group>";
@@ -1355,6 +1363,7 @@
13551363
CA1A6E7520DC2F04001C41B9 /* OneSignalDialogControllerOverrider.h in Headers */,
13561364
9129C6BD1E89E7AB009CB6A0 /* OSSubscription.h in Headers */,
13571365
CA7FC89F21927229002C4FD9 /* OSDynamicTriggerController.h in Headers */,
1366+
DE98773C2591656A00DE07D5 /* NSDateFormatter+OneSignal.h in Headers */,
13581367
);
13591368
runOnlyForDeploymentPostprocessing = 0;
13601369
};
@@ -1537,6 +1546,7 @@
15371546
files = (
15381547
9D1BD968237A28FC00A064F7 /* OSCachedUniqueOutcome.m in Sources */,
15391548
7A9173A2231971E5007848FA /* OneSignalReceiveReceiptsController.m in Sources */,
1549+
DE98772B2591656200DE07D5 /* NSDateFormatter+OneSignal.m in Sources */,
15401550
7AD172382416D53B00A78B19 /* OSInAppMessageLocationPrompt.m in Sources */,
15411551
9124120E1E73342200E41FD7 /* OneSignal.m in Sources */,
15421552
CACBAA97218A6243000ACAA5 /* OSMessagingController.m in Sources */,
@@ -1629,6 +1639,7 @@
16291639
files = (
16301640
9D1BD969237A28FC00A064F7 /* OSCachedUniqueOutcome.m in Sources */,
16311641
9124120F1E73342200E41FD7 /* OneSignal.m in Sources */,
1642+
DE9877332591656200DE07D5 /* NSDateFormatter+OneSignal.m in Sources */,
16321643
7AD172392416D53B00A78B19 /* OSInAppMessageLocationPrompt.m in Sources */,
16331644
CACBAA98218A6243000ACAA5 /* OSMessagingController.m in Sources */,
16341645
CA36F35A21C33A2500300C77 /* OSInAppMessageController.m in Sources */,
@@ -1734,6 +1745,7 @@
17341745
CA4743A02190FEA80020DC8C /* OSTrigger.m in Sources */,
17351746
912412201E73342200E41FD7 /* OneSignalJailbreakDetection.m in Sources */,
17361747
7AF9863D2444C43900C36EAE /* OSInAppMessageTracker.m in Sources */,
1748+
DE9877342591656300DE07D5 /* NSDateFormatter+OneSignal.m in Sources */,
17371749
CAAEA68921ED68A40049CF15 /* OneSignalNotificationCategoryController.m in Sources */,
17381750
7ABAF9D82457DD620074DFA0 /* SessionManagerTests.m in Sources */,
17391751
CA85C15320604AEA003AB529 /* RequestTests.m in Sources */,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2020 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
@interface NSDateFormatter (OneSignal)
30+
+ (instancetype)iso8601DateFormatter;
31+
@end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2020 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "NSDateFormatter+OneSignal.h"
29+
30+
@implementation NSDateFormatter (OneSignal)
31+
32+
+ (instancetype)iso8601DateFormatter {
33+
NSDateFormatter *dateFormatter = [NSDateFormatter new];
34+
[dateFormatter setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601]];
35+
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
36+
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
37+
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"];
38+
return dateFormatter;
39+
}
40+
41+
@end

iOS_SDK/OneSignalSDK/Source/OSInAppMessage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ NS_ASSUME_NONNULL_BEGIN
4747
@property (nonatomic) BOOL isDisplayedInSession;
4848
@property (nonatomic) BOOL isTriggerChanged;
4949
@property (nonatomic) NSNumber *height;
50+
@property (nonatomic, nullable) NSDate *endTime;
5051

5152
- (BOOL)isBanner;
5253
- (BOOL)takeActionAsUnique;
@@ -57,6 +58,8 @@ NS_ASSUME_NONNULL_BEGIN
5758
- (void)clearClickIds;
5859
- (void)addClickId:(NSString *)clickId;
5960

61+
- (BOOL)isFinished;
62+
6063
@end
6164

6265
NS_ASSUME_NONNULL_END

iOS_SDK/OneSignalSDK/Source/OSInAppMessage.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ + (instancetype)instanceWithJson:(NSDictionary * _Nonnull)json {
101101
message.displayStats = [OSInAppMessageDisplayStats instanceWithJson:json[@"redisplay"]];
102102
else
103103
message.displayStats = [[OSInAppMessageDisplayStats alloc] init];
104+
105+
if (json[@"end_time"] && [json[@"end_time"] isKindOfClass:[NSString class]]) {
106+
NSString *stringEndTime = json[@"end_time"];
107+
NSDateFormatter *dateFormatter = [NSDateFormatter iso8601DateFormatter];
108+
NSDate *endTime = [dateFormatter dateFromString:stringEndTime];
109+
message.endTime = endTime;
110+
}
104111

105112
if (json[@"triggers"] && [json[@"triggers"] isKindOfClass:[NSArray class]]) {
106113
let triggers = [NSMutableArray new];
@@ -160,11 +167,13 @@ -(NSDictionary *)jsonRepresentation {
160167
json[@"redisplay"] = [_displayStats jsonRepresentation];
161168
}
162169

170+
json[@"end_time"] = [[NSDateFormatter iso8601DateFormatter] stringFromDate:self.endTime];
171+
163172
return json;
164173
}
165174

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

170179
- (BOOL)isEqual:(id)object {
@@ -192,6 +201,7 @@ - (void)encodeWithCoder:(NSCoder *)encoder {
192201
[encoder encodeObject:_displayStats forKey:@"displayStats"];
193202
//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
194203
[encoder encodeBool:_isDisplayedInSession forKey:@"displayed_in_session"];
204+
[encoder encodeObject:_endTime forKey:@"endTime"];
195205
}
196206

197207
- (id)initWithCoder:(NSCoder *)decoder {
@@ -202,9 +212,17 @@ - (id)initWithCoder:(NSCoder *)decoder {
202212
_displayStats = [decoder decodeObjectForKey:@"displayStats"];
203213
//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
204214
_isDisplayedInSession = [decoder decodeBoolForKey:@"displayed_in_session"];
215+
_endTime = [decoder decodeObjectForKey:@"endTime"];
205216
}
206217
return self;
207218
}
208219

220+
- (BOOL)isFinished {
221+
if (!self.endTime) {
222+
return NO;
223+
}
224+
return [self.endTime compare:[NSDate date]] == NSOrderedAscending;
225+
}
226+
209227
@end
210228

iOS_SDK/OneSignalSDK/Source/OSMessagingController.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ - (BOOL)hasMessageTriggerChanged:(OSInAppMessage *)message {
396396
*/
397397
- (BOOL)shouldShowInAppMessage:(OSInAppMessage *)message {
398398
return ![self.seenInAppMessages containsObject:message.messageId] &&
399-
[self.triggerController messageMatchesTriggers:message];
399+
[self.triggerController messageMatchesTriggers:message] &&
400+
![message isFinished];
400401
}
401402

402403
- (void)handleMessageActionWithURL:(OSInAppMessageAction *)action {

iOS_SDK/OneSignalSDK/Source/OneSignalHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "OneSignal.h"
2929
#import "OneSignalWebView.h"
3030
#import "UIApplication+OneSignal.h"
31+
#import "NSDateFormatter+OneSignal.h"
3132

3233
#pragma clang diagnostic push
3334
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

0 commit comments

Comments
 (0)