Skip to content

Commit 24ab633

Browse files
committed
Merge pull request #310 from Iterable/MOB-2456-in-app-priority
[MOB-2456] in-app priority support
1 parent d538da4 commit 24ab633

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public final class IterableConstants {
185185
public static final String ITERABLE_IN_APP_CUSTOM_PAYLOAD = "customPayload";
186186
public static final String ITERABLE_IN_APP_TRIGGER = "trigger";
187187
public static final String ITERABLE_IN_APP_TRIGGER_TYPE = "type";
188+
public static final String ITERABLE_IN_APP_PRIORITY_LEVEL = "priorityLevel";
188189
public static final String ITERABLE_IN_APP_SAVE_TO_INBOX = "saveToInbox";
189190
public static final String ITERABLE_IN_APP_SILENT_INBOX = "silentInbox";
190191
public static final String ITERABLE_IN_APP_INBOX_METADATA = "inboxMetadata";
@@ -210,6 +211,12 @@ public final class IterableConstants {
210211
public static final int ITERABLE_IN_APP_ANIMATION_DURATION = 500;
211212
public static final int ITERABLE_IN_APP_BACKGROUND_ANIMATION_DURATION = 300;
212213

214+
public static final double ITERABLE_IN_APP_PRIORITY_LEVEL_LOW = 400.0;
215+
public static final double ITERABLE_IN_APP_PRIORITY_LEVEL_MEDIUM = 300.0;
216+
public static final double ITERABLE_IN_APP_PRIORITY_LEVEL_HIGH = 200.0;
217+
public static final double ITERABLE_IN_APP_PRIORITY_LEVEL_CRITICAL = 100.0;
218+
public static final double ITERABLE_IN_APP_PRIORITY_LEVEL_UNASSIGNED = 300.5;
219+
213220
public static final String ITERABLE_IN_APP_TYPE_BOTTOM = "BOTTOM";
214221
public static final String ITERABLE_IN_APP_TYPE_CENTER = "MIDDLE";
215222
public static final String ITERABLE_IN_APP_TYPE_FULL = "FULL";

iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppManager.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.json.JSONObject;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.Comparator;
2123
import java.util.HashMap;
2224
import java.util.List;
2325
import java.util.Map;
@@ -345,6 +347,25 @@ private void syncWithRemoteQueue(List<IterableInAppMessage> remoteQueue) {
345347
}
346348
}
347349

350+
private List<IterableInAppMessage> getMessagesSortedByPriorityLevel(List<IterableInAppMessage> messages) {
351+
List<IterableInAppMessage> messagesByPriorityLevel = messages;
352+
353+
Collections.sort(messagesByPriorityLevel, new Comparator<IterableInAppMessage>() {
354+
@Override
355+
public int compare(IterableInAppMessage message1, IterableInAppMessage message2) {
356+
if (message1.getPriorityLevel() < message2.getPriorityLevel()) {
357+
return -1;
358+
} else if (message1.getPriorityLevel() == message2.getPriorityLevel()) {
359+
return 0;
360+
} else {
361+
return 1;
362+
}
363+
}
364+
});
365+
366+
return messagesByPriorityLevel;
367+
}
368+
348369
private void processMessages() {
349370
if (!activityMonitor.isInForeground() || isShowingInApp() || !canShowInAppAfterPrevious() || isAutoDisplayPaused()) {
350371
return;
@@ -353,7 +374,10 @@ private void processMessages() {
353374
IterableLogger.printInfo();
354375

355376
List<IterableInAppMessage> messages = getMessages();
356-
for (IterableInAppMessage message : messages) {
377+
378+
List<IterableInAppMessage> messagesByPriorityLevel = getMessagesSortedByPriorityLevel(messages);
379+
380+
for (IterableInAppMessage message : messagesByPriorityLevel) {
357381
if (!message.isProcessed() && !message.isConsumed() && message.getTriggerType() == TriggerType.IMMEDIATE) {
358382
IterableLogger.d(TAG, "Calling onNewInApp on " + message.getMessageId());
359383
InAppResponse response = handler.onNewInApp(message);

iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppMessage.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class IterableInAppMessage {
2121
private final @NonNull Date createdAt;
2222
private final @NonNull Date expiresAt;
2323
private final @NonNull Trigger trigger;
24+
private final @NonNull double priorityLevel;
2425
private final @Nullable Boolean saveToInbox;
2526
private final @Nullable InboxMetadata inboxMetadata;
2627
private final @Nullable Long campaignId;
@@ -37,6 +38,7 @@ public class IterableInAppMessage {
3738
@NonNull Date createdAt,
3839
@NonNull Date expiresAt,
3940
@NonNull Trigger trigger,
41+
@NonNull Double priorityLevel,
4042
@Nullable Boolean saveToInbox,
4143
@Nullable InboxMetadata inboxMetadata,
4244
@Nullable Long campaignId) {
@@ -47,6 +49,7 @@ public class IterableInAppMessage {
4749
this.createdAt = createdAt;
4850
this.expiresAt = expiresAt;
4951
this.trigger = trigger;
52+
this.priorityLevel = priorityLevel;
5053
this.saveToInbox = saveToInbox;
5154
this.inboxMetadata = inboxMetadata;
5255
this.campaignId = campaignId;
@@ -276,6 +279,10 @@ Trigger.TriggerType getTriggerType() {
276279
return trigger.type;
277280
}
278281

282+
public double getPriorityLevel() {
283+
return priorityLevel;
284+
}
285+
279286
public boolean isInboxMessage() {
280287
return saveToInbox != null ? saveToInbox : false;
281288
}
@@ -358,6 +365,8 @@ static IterableInAppMessage fromJSONObject(@NonNull JSONObject messageJson, @Nul
358365
customPayload = new JSONObject();
359366
}
360367

368+
double priorityLevel = messageJson.optDouble(IterableConstants.ITERABLE_IN_APP_PRIORITY_LEVEL, IterableConstants.ITERABLE_IN_APP_PRIORITY_LEVEL_UNASSIGNED);
369+
361370
Boolean saveToInbox = messageJson.has(IterableConstants.ITERABLE_IN_APP_SAVE_TO_INBOX) ? messageJson.optBoolean(IterableConstants.ITERABLE_IN_APP_SAVE_TO_INBOX) : null;
362371
JSONObject inboxPayloadJson = messageJson.optJSONObject(IterableConstants.ITERABLE_IN_APP_INBOX_METADATA);
363372
InboxMetadata inboxMetadata = InboxMetadata.fromJSONObject(inboxPayloadJson);
@@ -370,6 +379,7 @@ static IterableInAppMessage fromJSONObject(@NonNull JSONObject messageJson, @Nul
370379
createdAt,
371380
expiresAt,
372381
trigger,
382+
priorityLevel,
373383
saveToInbox,
374384
inboxMetadata,
375385
campaignId);
@@ -400,7 +410,11 @@ JSONObject toJSONObject() {
400410
if (expiresAt != null) {
401411
messageJson.putOpt(IterableConstants.ITERABLE_IN_APP_EXPIRES_AT, expiresAt.getTime());
402412
}
413+
403414
messageJson.putOpt(IterableConstants.ITERABLE_IN_APP_TRIGGER, trigger.toJSONObject());
415+
416+
messageJson.putOpt(IterableConstants.ITERABLE_IN_APP_PRIORITY_LEVEL, priorityLevel);
417+
404418
inAppDisplaySettingsJson = encodePaddingRectToJson(content.padding);
405419

406420
inAppDisplaySettingsJson.put(IterableConstants.ITERABLE_IN_APP_SHOULD_ANIMATE, content.inAppDisplaySettings.shouldAnimate);

iterableapi/src/test/java/com/iterable/iterableapi/IterableApiAuthTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ public void testAuthTokenPresentInRequest() throws Exception {
397397
public void testAuthFailureReturns401() throws InterruptedException {
398398
doReturn(expiredJWT).when(authHandler).onAuthTokenRequested();
399399
dispatcher.enqueueResponse("/events/inAppConsume", new MockResponse().setResponseCode(401).setBody("{\"code\": \"InvalidJwtPayload\"}"));
400-
IterableApi.getInstance().inAppConsume(new IterableInAppMessage("asd", null, null, null, null, null, null, null, (long) 2), null, null);
400+
IterableApi.getInstance().inAppConsume(new IterableInAppMessage("asd", null, null, null, null, null, 0.0, null, null, (long) 2), null, null);
401401
Robolectric.flushForegroundThreadScheduler();
402402
assertEquals(IterableApi.getInstance().getAuthToken(), expiredJWT);
403403
}

iterableapi/src/test/resources/inapp_payload_multiple.json

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
"left": {
2323
"percentage": 0
2424
},
25-
"bgColor": {"hex": "#ABCDEF", "alpha": 0.5},
25+
"bgColor": {
26+
"hex": "#ABCDEF",
27+
"alpha": 0.5
28+
},
2629
"shouldAnimate": true
2730
}
2831
},
2932
"customPayload": {
3033
"intValue": 123
31-
}
32-
},{
34+
},
35+
"priorityLevel": 300.5
36+
},
37+
{
3338
"messageId": "7kx2MmoGdCpuZao9fDueuQoXVAZuDaVY",
3439
"content": {
3540
"html": "<html><head></head><body>Test2</body></html>",
@@ -46,14 +51,19 @@
4651
"left": {
4752
"percentage": 0
4853
},
49-
"bgColor": {"hex": "#ABCDEF", "alpha": 0.5},
54+
"bgColor": {
55+
"hex": "#ABCDEF",
56+
"alpha": 0.5
57+
},
5058
"shouldAnimate": true
5159
}
5260
},
5361
"customPayload": {
5462
"intValue": 123
55-
}
56-
},{
63+
},
64+
"priorityLevel": 100.0
65+
},
66+
{
5767
"messageId": "7kx2MmoGdCpuZao9fDueuQoXVAZuDaVZ",
5868
"expiresAt": 1549157312395,
5969
"trigger": {
@@ -78,14 +88,19 @@
7888
"left": {
7989
"percentage": 0
8090
},
81-
"bgColor": {"hex": "#ABCDEF", "alpha": 0.5},
91+
"bgColor": {
92+
"hex": "#ABCDEF",
93+
"alpha": 0.5
94+
},
8295
"shouldAnimate": true
8396
}
8497
},
8598
"customPayload": {
8699
"intValue": 123
87-
}
88-
},{
100+
},
101+
"priorityLevel": 200.5
102+
},
103+
{
89104
"messageId": "7kx2MmoGdCpuZao9fDueuQoXVAZuDaV0",
90105
"expiresAt": 1549157312395,
91106
"trigger": {
@@ -106,7 +121,10 @@
106121
"left": {
107122
"percentage": 0
108123
},
109-
"bgColor": {"hex": "#ABCDEF", "alpha": 0.5},
124+
"bgColor": {
125+
"hex": "#ABCDEF",
126+
"alpha": 0.5
127+
},
110128
"shouldAnimate": true
111129
}
112130
},
@@ -115,7 +133,8 @@
115133
"title": "Title",
116134
"subtitle": "Subtitle",
117135
"icon": "icon.png"
118-
}
136+
},
137+
"priorityLevel": 400.0
119138
}
120139
]
121140
}

0 commit comments

Comments
 (0)