Skip to content

Commit 5de720f

Browse files
emawbyJeasmine
authored andcommitted
Fix showing IAMs past their end time
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.
1 parent 84a0524 commit 5de720f

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessage.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
import org.json.JSONException;
77
import org.json.JSONObject;
88

9+
import java.text.ParseException;
10+
import java.text.SimpleDateFormat;
911
import java.util.ArrayList;
12+
import java.util.Date;
1013
import java.util.HashMap;
1114
import java.util.HashSet;
1215
import java.util.Iterator;
16+
import java.util.Locale;
1317
import java.util.Set;
1418

1519
class OSInAppMessage {
@@ -18,7 +22,8 @@ class OSInAppMessage {
1822
private static final String IAM_VARIANTS = "variants";
1923
private static final String IAM_TRIGGERS = "triggers";
2024
private static final String IAM_REDISPLAY_STATS = "redisplay";
21-
private static final String DISPLAY_DURATION = "display_duration";
25+
private static final String DISPLAY_DURATION = "displayDuration";
26+
private static final String END_TIME = "end_time";
2227

2328
/**
2429
* The unique identifier for this in-app message
@@ -57,6 +62,7 @@ class OSInAppMessage {
5762
private boolean displayedInSession = false;
5863
private boolean triggerChanged = false;
5964
private boolean actionTaken;
65+
private Date endTime;
6066
boolean isPreview;
6167

6268
OSInAppMessage(boolean isPreview) {
@@ -76,11 +82,31 @@ class OSInAppMessage {
7682
this.variants = parseVariants(json.getJSONObject(IAM_VARIANTS));
7783
this.triggers = parseTriggerJson(json.getJSONArray(IAM_TRIGGERS));
7884
this.clickedClickIds = new HashSet<>();
85+
this.endTime = parseEndTimeJson(json);
7986

8087
if (json.has(IAM_REDISPLAY_STATS))
8188
this.redisplayStats = new OSInAppMessageRedisplayStats(json.getJSONObject(IAM_REDISPLAY_STATS));
8289
}
8390

91+
private Date parseEndTimeJson(JSONObject json) {
92+
String endTimeString;
93+
try {
94+
endTimeString = json.getString(END_TIME);
95+
} catch (JSONException e) {
96+
return null;
97+
}
98+
99+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
100+
try {
101+
Date date = format.parse(endTimeString);
102+
return date;
103+
} catch (ParseException e) {
104+
e.printStackTrace();
105+
}
106+
107+
return null;
108+
}
109+
84110
private HashMap<String, HashMap<String, String>> parseVariants(JSONObject json) throws JSONException {
85111
HashMap<String, HashMap<String, String>> variantTypes = new HashMap<>();
86112

@@ -152,6 +178,12 @@ JSONObject toJSONObject() {
152178

153179
json.put(IAM_TRIGGERS, orConditions);
154180

181+
if (this.endTime != null) {
182+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
183+
String endTimeString = format.format(this.endTime);
184+
json.put(END_TIME, endTimeString);
185+
}
186+
155187
} catch (JSONException exception) {
156188
exception.printStackTrace();
157189
}
@@ -231,6 +263,7 @@ public String toString() {
231263
", triggerChanged=" + triggerChanged +
232264
", actionTaken=" + actionTaken +
233265
", isPreview=" + isPreview +
266+
", endTime=" + endTime +
234267
'}';
235268
}
236269

@@ -248,4 +281,11 @@ public int hashCode() {
248281
return result;
249282
}
250283

284+
public boolean isFinished() {
285+
if (this.endTime == null) {
286+
return false;
287+
}
288+
Date now = new Date();
289+
return this.endTime.before(now);
290+
}
251291
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessageController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void evaluateInAppMessages() {
201201
if (triggerController.evaluateMessageTriggers(message)) {
202202
setDataForRedisplay(message);
203203

204-
if (!dismissedMessages.contains(message.messageId)) {
204+
if (!dismissedMessages.contains(message.messageId) && !message.isFinished()) {
205205
queueMessageForDisplay(message);
206206
}
207207
}

0 commit comments

Comments
 (0)