Skip to content

Commit 2c29fbf

Browse files
authored
Merge pull request #53 from Iterable/bugfix/ITBL-5899-inapp-display-fix
[ITBL-5899] Only show one in-app message at a time
2 parents dda5702 + d07c26b commit 2c29fbf

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,13 +897,13 @@ public void execute(String payload) {
897897
Rect padding = IterableInAppManager.getPaddingFromPayload(paddingOptions);
898898

899899
double backgroundAlpha = message.optDouble("backgroundAlpha", 0);
900-
IterableInAppManager.showIterableNotificationHTML(context, html, messageId, clickCallback, backgroundAlpha, padding);
900+
if (IterableInAppManager.showIterableNotificationHTML(context, html, messageId, clickCallback, backgroundAlpha, padding)) {
901+
IterableApi.sharedInstance.inAppConsume(messageId);
902+
}
901903
} else {
902904
IterableLogger.w(TAG, "No href tag in found in the in-app html payload: "+ html);
905+
IterableApi.sharedInstance.inAppConsume(messageId);
903906
}
904-
905-
IterableApi.sharedInstance.inAppConsume(messageId);
906-
907907
}
908908
}
909909
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public static IterableInAppHTMLNotification createInstance(Context context, Stri
5252
return notification;
5353
}
5454

55+
/**
56+
* Returns the notification instance currently being shown
57+
* @return notification instance
58+
*/
59+
public static IterableInAppHTMLNotification getInstance() {
60+
return notification;
61+
}
62+
5563
/**
5664
* HTML In-App Notification
5765
* @param context
@@ -161,6 +169,7 @@ public void run() {
161169
@Override
162170
protected void onStop() {
163171
orientationListener.disable();
172+
notification = null;
164173
}
165174

166175
/**

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,28 @@ public class IterableInAppManager {
3939
* @param backgroundAlpha
4040
* @param padding
4141
*/
42-
public static void showIterableNotificationHTML(Context context, String htmlString, String messageId, IterableHelper.IterableActionHandler clickCallback, double backgroundAlpha, Rect padding) {
42+
public static boolean showIterableNotificationHTML(Context context, String htmlString, String messageId, IterableHelper.IterableActionHandler clickCallback, double backgroundAlpha, Rect padding) {
4343
if (context instanceof Activity) {
4444
Activity currentActivity = (Activity) context;
4545
if (htmlString != null) {
46+
if (IterableInAppHTMLNotification.getInstance() != null) {
47+
IterableLogger.w(TAG, "Skipping the in-app notification: another notification is already being displayed");
48+
return false;
49+
}
50+
4651
IterableInAppHTMLNotification notification = IterableInAppHTMLNotification.createInstance(context, htmlString);
4752
notification.setTrackParams(messageId);
4853
notification.setCallback(clickCallback);
4954
notification.setBackgroundAlpha(backgroundAlpha);
5055
notification.setPadding(padding);
5156
notification.setOwnerActivity(currentActivity);
5257
notification.show();
58+
return true;
5359
}
5460
} else {
5561
IterableLogger.w(TAG, "To display in-app notifications, the context must be of an instance of: Activity");
5662
}
63+
return false;
5764
}
5865

5966
/**
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.iterable.iterableapi;
2+
3+
import android.app.Activity;
4+
import android.graphics.Rect;
5+
6+
import com.iterable.iterableapi.unit.BaseTest;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.powermock.core.classloader.annotations.PrepareForTest;
12+
import org.robolectric.Robolectric;
13+
import org.robolectric.RuntimeEnvironment;
14+
import org.robolectric.android.controller.ActivityController;
15+
16+
import java.io.IOException;
17+
18+
import okhttp3.mockwebserver.MockWebServer;
19+
20+
import static junit.framework.Assert.assertFalse;
21+
import static junit.framework.Assert.assertTrue;
22+
23+
@PrepareForTest(IterableUtil.class)
24+
public class IterableInAppManagerTest extends BaseTest {
25+
26+
private MockWebServer server;
27+
28+
@Before
29+
public void setUp() {
30+
server = new MockWebServer();
31+
IterableApi.overrideURLEndpointPath(server.url("").toString());
32+
IterableApi.sharedInstance = new IterableApi();
33+
}
34+
35+
@After
36+
public void tearDown() throws IOException {
37+
server.shutdown();
38+
server = null;
39+
}
40+
41+
@Test
42+
public void testDoNotShowMultipleTimes() throws Exception {
43+
ActivityController<Activity> controller = Robolectric.buildActivity(Activity.class).create().start();
44+
Activity activity = controller.get();
45+
46+
boolean shownFirstTime = IterableInAppManager.showIterableNotificationHTML(activity, "", "", null, 0.0, new Rect());
47+
boolean shownSecondTime = IterableInAppManager.showIterableNotificationHTML(activity, "", "", null, 0.0, new Rect());
48+
assertTrue(shownFirstTime);
49+
assertFalse(shownSecondTime);
50+
}
51+
52+
}

iterableapi/src/test/java/com/iterable/iterableapi/unit/BaseTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"android.*",
1818
"okhttp3.*",
1919
"javax.net.ssl.*",
20+
"javax.xml.parsers.*",
21+
"com.sun.org.apache.xerces.internal.jaxp.*",
2022
"com.squareup.*",
2123
"okio.*"
2224
})

0 commit comments

Comments
 (0)