Skip to content

Commit

Permalink
Pull request #90: Provided in app click reporting
Browse files Browse the repository at this point in the history
Merge in MML/infobip-mobile-messaging-huawei from ikresic-MM-6997-huawei-fix-inapp-click-reports to master

Squashed commit of the following:

commit 4e802542bd3442d84c39161443da2f57ed089026
Author: ikresic <[email protected]>
Date:   Wed Jan 8 14:10:49 2025 +0100

    - provided in app click reporting
  • Loading branch information
ikresicc committed Jan 9, 2025
1 parent 0790c44 commit 9bc929d
Show file tree
Hide file tree
Showing 19 changed files with 350 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.infobip.mobile.messaging;

import static org.mockito.Mockito.mock;

import org.infobip.mobile.messaging.interactive.inapp.InAppWebViewMessage;
import org.infobip.mobile.messaging.mobileapi.BatchReporter;
import org.infobip.mobile.messaging.mobileapi.common.MRetryPolicy;
import org.infobip.mobile.messaging.mobileapi.common.RetryPolicyProvider;
import org.infobip.mobile.messaging.mobileapi.inapp.InAppClickReporter;
import org.infobip.mobile.messaging.stats.MobileMessagingStats;
import org.infobip.mobile.messaging.tools.MobileMessagingTestCase;
import org.infobip.mobile.messaging.util.StringUtils;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.concurrent.Executor;

public class ClickUrlReporterTest extends MobileMessagingTestCase {

private InAppClickReporter inAppClickReporter;
private Executor executor;
private BatchReporter batchReporter;
private MRetryPolicy retryPolicy;

@Override
public void setUp() throws Exception {
super.setUp();

batchReporter = new BatchReporter(300L);

RetryPolicyProvider retryPolicyProvider = new RetryPolicyProvider(context);
retryPolicy = retryPolicyProvider.DEFAULT();

MobileMessagingStats stats = mobileMessagingCore.getStats();
executor = mock(Executor.class);
inAppClickReporter = new InAppClickReporter(mobileMessagingCore, context, stats, executor, broadcaster, batchReporter, retryPolicy);
}

@Test
public void test_clickUrlReportBatch() throws Exception {

InAppWebViewMessage message = new InAppWebViewMessage();
message.clickUrl = "https://www.infobip.com";
message.url = "https://www.infobip.com";
message.position = InAppWebViewMessage.InAppWebViewPosition.TOP;
message.type = InAppWebViewMessage.InAppWebViewType.BANNER;

String temp = StringUtils.concat(message.clickUrl, "banner", StringUtils.COMMA_WITH_SPACE);
String clickReport = StringUtils.concat(temp, null, StringUtils.COMMA_WITH_SPACE);

mobileMessagingCore.reportInAppClick(clickReport);
inAppClickReporter.sync();

Mockito.verify(executor, Mockito.times(1)).execute(Mockito.any(Runnable.class));
Mockito.verify(executor, Mockito.after(500).atMost(2)).execute(Mockito.any(Runnable.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.infobip.mobile.messaging.interactive.platform.InteractiveBroadcaster;
import org.junit.Before;
import org.junit.Test;

import org.mockito.Mockito;

/**
Expand Down Expand Up @@ -115,6 +114,7 @@ public void shouldShowInAppNativeDialogWhenInForeground() {
when(inAppRules.areModalInAppNotificationsEnabled()).thenReturn(true);
when(inAppViewFactory.create(eq(activity), any(InAppView.Callback.class), eq(message))).thenReturn(inAppNativeView);


inAppNotificationHandler.handleMessage(message);

verify(dialogStack, times(1)).add(any(InAppNativeCtx.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package org.infobip.mobile.messaging.interactive.inapp.rules;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

import android.app.Activity;

import org.infobip.mobile.messaging.Message;
Expand All @@ -27,6 +18,15 @@

import java.util.concurrent.TimeUnit;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

/**
* @author sslavin
* @since 18/04/2018.
Expand Down Expand Up @@ -108,6 +108,7 @@ public void shouldDisplayIfSilent() {
assertEquals(false, showOrNot.shouldShowWhenInForeground());
}


@Test
public void shouldDisplayWithDefaultActionsIfNoCategoryAndForeground() {
when(foregroundStateMonitor.isInForeground()).thenReturn(ForegroundState.foreground(activity));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
package org.infobip.mobile.messaging.interactive.inapp.view;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.R;
import org.infobip.mobile.messaging.interactive.NotificationAction;
import org.infobip.mobile.messaging.interactive.NotificationCategory;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Random;
import java.util.concurrent.Executor;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author sslavin
* @since 24/04/2018.
*/
@Ignore("mock issues")
@Ignore("mock issues") //fix within MM-5769
public class InAppViewDialogTest {

private InAppViewDialog inAppViewDialog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public enum Event {
*/
CUSTOM_EVENTS_SENT("org.infobip.mobile.messaging.CUSTOM_EVENTS_SENT"),

/**
* It is triggered when in-app click events are sent to the server.
*/
IN_APP_CLICKS_REPORTED("org.infobip.mobile.messaging.IN_APP_CLICKS_REPORTED"),

/**
* It is triggered when Google Play Services are not available.
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.infobip.mobile.messaging.mobileapi.common.RetryPolicyProvider;
import org.infobip.mobile.messaging.mobileapi.events.UserEventsRequestMapper;
import org.infobip.mobile.messaging.mobileapi.events.UserEventsSynchronizer;
import org.infobip.mobile.messaging.mobileapi.inapp.InAppClickReporter;
import org.infobip.mobile.messaging.mobileapi.messages.MessagesSynchronizer;
import org.infobip.mobile.messaging.mobileapi.messages.MoMessageSender;
import org.infobip.mobile.messaging.mobileapi.seen.SeenStatusReporter;
Expand Down Expand Up @@ -154,6 +155,7 @@ public class MobileMessagingCore
private volatile Long lastSyncTimeMillis;
private volatile Long lastForegroundSyncMillis;
private PostNotificationsPermissionRequester postNotificationsPermissionRequester;
private InAppClickReporter inAppClickReporter;

protected MobileMessagingCore(Context context) {
this(context, new AndroidBroadcaster(context), Executors.newSingleThreadExecutor(), new ModuleLoader(context));
Expand Down Expand Up @@ -291,7 +293,7 @@ private void initCustomChannels() {

CharSequence appName = SoftwareInformation.getAppName(context);
if (channelName != null)
appName = appName +" "+ channelName;
appName = appName + " " + channelName;


NotificationChannelCompat.Builder notificationChannelBuilder = new NotificationChannelCompat.Builder(channelId, NotificationManagerCompat.IMPORTANCE_DEFAULT)
Expand Down Expand Up @@ -457,6 +459,7 @@ private void performSyncActions() {
messagesSynchronizer().sync();
moMessageSender().sync();
seenStatusReporter().sync();
inAppClickReporter().sync();
}

protected void syncInstallation() {
Expand Down Expand Up @@ -979,6 +982,44 @@ private void updateStoredMessagesWithSeenStatus(String[] messageIds) {
}
}

public void reportInAppClick(String... clickUrls) {
if (clickUrls != null) {
addUnreportedInAppClicks(clickUrls);
}
lazySync();
}

public void addUnreportedInAppClicks(final String... clickUrls) {
PreferenceHelper.appendToStringArray(context, MobileMessagingProperty.INFOBIP_UNREPORTED_IN_APP_CLICK_URLS, clickUrls);
}

public String[] getUnreportedInAppClickActions() {
return PreferenceHelper.findStringArray(context, MobileMessagingProperty.INFOBIP_UNREPORTED_IN_APP_CLICK_URLS);
}

public void removeReportedInAppClickActions(final String... clickUrls) {
PreferenceHelper.deleteFromStringArray(context, MobileMessagingProperty.INFOBIP_UNREPORTED_IN_APP_CLICK_URLS, clickUrls);
}

/**
* Returns list of clickUrls from action click buttons
*
* @param reports concatenated click urls
* @return reports
*/
public List<String> getInAppClickUrlsFromReports(String[] reports) {
List<String> ids = new ArrayList<>();
for (String report : reports) {
ids.add(getInAppClickActionUrlFromReport(report));
}
return ids;
}

private String getInAppClickActionUrlFromReport(String report) {
String[] reportContents = report.split(StringUtils.COMMA_WITH_SPACE);
return reportContents.length > 0 ? reportContents[0] : null;
}

@Nullable
public NotificationSettings getNotificationSettings() {
if (!isDisplayNotificationEnabled()) {
Expand Down Expand Up @@ -1401,6 +1442,7 @@ private static void cleanup(Context context) {
mmCore.seenStatusReporter = null;
mmCore.versionChecker = null;
mmCore.baseUrlChecker = null;
mmCore.inAppClickReporter = null;
resetApiUri(context);

mmCore.didSyncAtLeastOnce = false;
Expand Down Expand Up @@ -1985,6 +2027,14 @@ private SeenStatusReporter seenStatusReporter() {
return seenStatusReporter;
}

@NonNull
private InAppClickReporter inAppClickReporter() {
if (inAppClickReporter == null) {
inAppClickReporter = new InAppClickReporter(this, context, stats, registrationAlignedExecutor, broadcaster, new BatchReporter(PreferenceHelper.findLong(context, MobileMessagingProperty.BATCH_REPORTING_DELAY)), retryPolicyProvider.DEFAULT());
}
return inAppClickReporter;
}

@NonNull
private VersionChecker versionChecker() {
if (versionChecker == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum MobileMessagingProperty {
// START: MO/MT messages related prefs
INFOBIP_UNREPORTED_MESSAGE_IDS("org.infobip.mobile.messaging.infobip.INFOBIP_UNREPORTED_MESSAGE_IDS", new String[0]),
INFOBIP_UNREPORTED_SEEN_MESSAGE_IDS("org.infobip.mobile.messaging.infobip.INFOBIP_UNREPORTED_SEEN_MESSAGE_IDS", new String[0]),
INFOBIP_UNREPORTED_IN_APP_CLICK_URLS("org.infobip.mobile.messaging.infobip.INFOBIP_UNREPORTED_IN_APP_CLICK_URLS", new String[0]),
INFOBIP_GENERATED_MESSAGE_IDS("org.infobip.mobile.messaging.infobip.INFOBIP_GENERATED_MESSAGE_IDS", new String[0]),
INFOBIP_SYNC_MESSAGES_IDS("org.infobip.mobile.messaging.infobip.INFOBIP_SYNC_MESSAGES_IDS", new String[0]),
MESSAGE_STORE_CLASS("org.infobip.mobile.messaging.infobip.MESSAGE_STORE_CLASS"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import android.text.TextUtils;

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MobileMessagingCore;
Expand Down Expand Up @@ -38,7 +39,6 @@
*/

public class InAppNotificationHandlerImpl implements InAppNotificationHandler, InAppView.Callback {

private final MobileInteractive mobileInteractive;
private final InAppViewFactory inAppViewFactory;
private final InAppRules inAppRules;
Expand Down Expand Up @@ -80,7 +80,6 @@ public InAppNotificationHandlerImpl(Context context) {
public void handleMessage(Message message) {
ShowOrNot showOrNot = inAppRules.shouldDisplayDialogFor(message);


if (!showOrNot.shouldShowNow() && showOrNot.shouldShowWhenInForeground()) {
oneMessageCache.save(message);
return;
Expand All @@ -91,6 +90,7 @@ public void handleMessage(Message message) {
@Override
public void handleMessage(InAppWebViewMessage message) {
ShowOrNot showOrNot = inAppRules.shouldDisplayDialogFor(message);

if (!showOrNot.shouldShowNow() && showOrNot.shouldShowWhenInForeground()) {
oneMessageCache.save(message);
return;
Expand Down Expand Up @@ -126,6 +126,7 @@ public void displayDialogFor(Message message) {

private void displayDialogFor(Message message, Boolean displayingEnabled) {
ShowOrNot showOrNot = inAppRules.shouldDisplayDialogFor(message);

if (!showOrNot.shouldShowNow()) {
return;
}
Expand Down Expand Up @@ -177,7 +178,6 @@ public void userTappedNotificationForMessage(@NonNull Message message) {
public void buttonPressedFor(@NonNull InAppNativeView inAppView, @NonNull Message message, NotificationCategory category, @NonNull NotificationAction action) {
mobileInteractive.triggerSdkActionsFor(action, message);
Intent callbackIntent = interactiveBroadcaster.notificationActionTapped(message, category, action);

handleButtonPress(message, action, callbackIntent);
}

Expand All @@ -201,7 +201,6 @@ public void notificationPressedFor(@NonNull InAppWebView inAppView, @NonNull Mes
}

private void handleButtonPress(@NonNull Message message, @NonNull NotificationAction action, Intent callbackIntent) {

if (PredefinedActionsProvider.isOpenAction(action.getId()) || action.bringsAppToForeground()) {
if (StringUtils.isNotBlank(message.getWebViewUrl())) {
activityStarterWrapper.startWebViewActivity(callbackIntent, message.getWebViewUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

public class InAppWebViewMessage extends Message {

@Nullable
public String clickUrl;
@NonNull
public String url;
@Nullable
Expand Down Expand Up @@ -94,4 +96,4 @@ public static InAppWebViewMessage createInAppWebViewMessage(Message message) {
inAppWebViewMessage.setWebViewUrl(message.getWebViewUrl());
return inAppWebViewMessage;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.infobip.mobile.messaging.interactive.inapp.rules;


import androidx.annotation.NonNull;

import org.infobip.mobile.messaging.Message;
Expand Down Expand Up @@ -51,9 +50,9 @@ public ShowOrNot shouldDisplayDialogFor(InAppWebViewMessage message) {
@NonNull
private ShowOrNot checkDialogConditions(Message message) {
long inAppExpiryTimestamp = message.getInAppExpiryTimestamp();
if ((inAppExpiryTimestamp != 0 && inAppExpiryTimestamp < Time.now())) {
return ShowOrNot.not();
}

if ((inAppExpiryTimestamp != 0 && inAppExpiryTimestamp < Time.now()))
return ShowOrNot.not();

ForegroundState state = foregroundStateMonitor.isInForeground();
if (state.isForeground() && state.getForegroundActivity() != null && !state.getForegroundActivity().isFinishing()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* @since 13/04/2018.
*/
public class InAppViewDialog implements InAppNativeView {

private final Callback callback;
private final View dialogView;
private final TextView tvMessageText;
Expand Down
Loading

0 comments on commit 9bc929d

Please sign in to comment.