Skip to content

Commit

Permalink
Pull request #91: MM-6987 InAppChat does not handle notification taps…
Browse files Browse the repository at this point in the history
… with CALLBACK_ACTIVITY anymore

Merge in MML/infobip-mobile-messaging-huawei from jdzubak-MM-6987-notification-tap-intent-fix to master

Squashed commit of the following:

commit 49828442ddb2fea4a8eb4071eab698f90a6a045f
Author: Jakub Dzubak <[email protected]>
Date:   Tue Jan 14 10:19:32 2025 +0100

    MM-6987 Review fixes

commit 212cc8e1e90398ed842ae6ed881c0b555a88af11
Author: Jakub Dzubak <[email protected]>
Date:   Tue Jan 14 10:10:06 2025 +0100

    MM-6987 Review fixes

commit ae766779ac2bb932e36a18718ec474666a9ca349
Author: Jakub Dzubak <[email protected]>
Date:   Mon Jan 13 17:44:07 2025 +0100

    MM-6987 InAppChat does not handle notification taps with CALLBACK_ACTIVITY anymore
  • Loading branch information
jakubdzubak1 committed Jan 14, 2025
1 parent 3c0d744 commit 05529d1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import org.infobip.mobile.messaging.MobileMessaging;
import org.infobip.mobile.messaging.chat.core.MultithreadStrategy;
import org.infobip.mobile.messaging.chat.view.InAppChatEventsListener;
import org.infobip.mobile.messaging.chat.view.styles.InAppChatTheme;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;


/**
* Main interface for in-app chat communication
Expand Down Expand Up @@ -70,7 +70,7 @@ public synchronized static InAppChat getInstance(Context context) {
*
* @param activityClasses array of activities to put into task stack when message is tapped
*/
public abstract void setActivitiesToStartOnMessageTap(Class... activityClasses);
public abstract void setActivitiesToStartOnMessageTap(Class<?>... activityClasses);

/**
* Cleans up all InAppChat data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.TaskStackBuilder;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import org.infobip.mobile.messaging.Event;
import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MessageHandlerModule;
import org.infobip.mobile.messaging.MobileMessaging;
import org.infobip.mobile.messaging.MobileMessagingCore;
import org.infobip.mobile.messaging.MobileMessagingProperty;
import org.infobip.mobile.messaging.NotificationSettings;
import org.infobip.mobile.messaging.api.chat.WidgetInfo;
import org.infobip.mobile.messaging.app.ActivityLifecycleMonitor;
import org.infobip.mobile.messaging.chat.core.InAppChatBroadcasterImpl;
Expand Down Expand Up @@ -52,6 +43,14 @@

import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.TaskStackBuilder;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


public class InAppChatImpl extends InAppChat implements MessageHandlerModule {

Expand Down Expand Up @@ -154,32 +153,27 @@ public boolean messageTapped(Message message) {

private void doCoreTappedActions(Message chatMessage) {
TaskStackBuilder stackBuilder = stackBuilderForNotificationTap(chatMessage);
if (stackBuilder.getIntentCount() != 0) {
if (stackBuilder != null && stackBuilder.getIntentCount() > 0) {
stackBuilder.startActivities();
}
}

@NonNull
@Nullable
private TaskStackBuilder stackBuilderForNotificationTap(Message message) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
Bundle messageBundle = MessageBundleMapper.messageToBundle(message);
Class[] classes = propertyHelper().findClasses(MobileMessagingChatProperty.ON_MESSAGE_TAP_ACTIVITY_CLASSES);
Class<?>[] classes = propertyHelper().findClasses(MobileMessagingChatProperty.ON_MESSAGE_TAP_ACTIVITY_CLASSES);
if (classes != null) {
for (Class cls : classes) {
stackBuilder.addNextIntent(new Intent(context, cls)
.setAction(Event.NOTIFICATION_TAPPED.getKey())
.putExtras(messageBundle));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
Bundle messageBundle = MessageBundleMapper.messageToBundle(message);
for (Class<?> cls : classes) {
stackBuilder.addNextIntent(
new Intent(context, cls)
.setAction(Event.NOTIFICATION_TAPPED.getKey())
.putExtras(messageBundle)
);
}
return stackBuilder;
}

NotificationSettings notificationSettings = mobileMessagingCore().getNotificationSettings();
if (stackBuilder.getIntentCount() == 0 && notificationSettings != null && notificationSettings.getCallbackActivity() != null) {
stackBuilder.addNextIntent(new Intent(context, notificationSettings.getCallbackActivity())
.setAction(Event.NOTIFICATION_TAPPED.getKey())
.putExtras(messageBundle));
}

return stackBuilder;
return null;
}

@Override
Expand Down Expand Up @@ -250,7 +244,7 @@ public InAppChatScreenImpl inAppChatScreen() {
}

@Override
public void setActivitiesToStartOnMessageTap(Class... activityClasses) {
public void setActivitiesToStartOnMessageTap(Class<?>... activityClasses) {
propertyHelper().saveClasses(MobileMessagingChatProperty.ON_MESSAGE_TAP_ACTIVITY_CLASSES, activityClasses);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package org.infobip.mobile.messaging.chat.core;

import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatPause;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatResume;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendContextualData;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendDraft;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessage;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessageWithAttachment;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setLanguage;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setTheme;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.showThreadList;
import static org.infobip.mobile.messaging.util.StringUtils.isNotBlank;

import android.os.Handler;
import android.os.Looper;

Expand All @@ -22,10 +11,27 @@
import org.infobip.mobile.messaging.mobileapi.Result;
import org.infobip.mobile.messaging.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatPause;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatResume;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendContextualData;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendDraft;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessage;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessageWithAttachment;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setLanguage;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setTheme;
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.showThreadList;
import static org.infobip.mobile.messaging.util.StringUtils.isNotBlank;

public class InAppChatClientImpl implements InAppChatClient {

private static final int MAX_ALLOWED_SCRIPT_LENGTH = 200;
private static final int MAX_ALLOWED_ARGUMENT_LENGTH = 50;
private static final int ARGUMENT_VISIBLE_PART_LENGTH = 15;

private final InAppChatWebView webView;
private static final String TAG = InAppChatClient.class.getSimpleName();
private final Handler handler = new Handler(Looper.getMainLooper());
Expand Down Expand Up @@ -127,17 +133,18 @@ private void executeScript(String script, MobileMessaging.ResultListener<String>
try {
handler.post(() -> webView.evaluateJavascript(script, value -> {
String valueToLog = (value != null && !"null".equals(value)) ? " => " + value : "";
MobileMessagingLogger.d(TAG, "Called Widget API: " + script + valueToLog);
String scriptToLog = shortenScript(script);
MobileMessagingLogger.d(TAG, "Called Widget API: " + scriptToLog + valueToLog);
if (resultListener != null)
resultListener.onResult(new Result<>(valueToLog));
}));
} catch (Exception e) {
if (resultListener != null)
resultListener.onResult(new Result<>(MobileMessagingError.createFrom(e)));
MobileMessagingLogger.e(TAG,"Failed to execute webView JS script " + script + " " + e.getMessage());
MobileMessagingLogger.e(TAG, "Failed to execute webView JS script " + shortenScript(script) + " " + e.getMessage());
}
} else if (resultListener != null) {
resultListener.onResult(new Result<>(MobileMessagingError.createFrom(new IllegalStateException("Failed to execute webView JS script " + script + " InAppChatWebView is null."))));
resultListener.onResult(new Result<>(MobileMessagingError.createFrom(new IllegalStateException("Failed to execute webView JS script " + shortenScript(script) + " InAppChatWebView is null."))));
}
}

Expand All @@ -158,4 +165,34 @@ private String buildWidgetMethodInvocation(String methodName, String... params)

return builder.toString();
}

private String shortenScript(String script) {
if (script != null && script.length() > MAX_ALLOWED_SCRIPT_LENGTH) {
StringBuilder builder = new StringBuilder();
int methodNameEndIndex = script.indexOf("(");
if (methodNameEndIndex > 0) {
String methodName = script.substring(0, methodNameEndIndex);
builder.append(methodName);
String paramsSubstring = script.substring(methodNameEndIndex + 1, script.length() - 1);
String[] paramsArray = paramsSubstring.split(",");
if (paramsArray.length > 0) {
List<String> shortenedParams = new ArrayList<>();
for (String param : paramsArray) {
String value = param.replace("'", "");
if (value.length() > MAX_ALLOWED_ARGUMENT_LENGTH) {
value = value.substring(0, ARGUMENT_VISIBLE_PART_LENGTH) + "..." + value.substring(value.length() - ARGUMENT_VISIBLE_PART_LENGTH);
}
shortenedParams.add(value);
}
String params = StringUtils.join("','", "('", "')", shortenedParams.toArray(new String[0]));
builder.append(params);
}
else {
builder.append("()");
}
}
return builder.toString();
}
return script;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import android.os.Looper;
import android.webkit.JavascriptInterface;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import org.infobip.mobile.messaging.logging.MobileMessagingLogger;
import org.infobip.mobile.messaging.util.StringUtils;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

public class InAppChatMobileImpl implements InAppChatMobile {

private final InAppChatWebViewManager inAppChatWebViewManager;
Expand Down Expand Up @@ -98,11 +98,15 @@ public void onWidgetApiError(String method, String errorPayload) {
@JavascriptInterface
public void onWidgetApiSuccess(String method, String successPayload) {
Runnable myRunnable = () -> {
String result = StringUtils.isNotBlank(successPayload) ? " => " + successPayload : "";
String payload = successPayload;
if (StringUtils.isNotBlank(payload) && payload.startsWith("\"") && payload.endsWith("\"") && payload.length() > 2) {
payload = payload.substring(1, payload.length() - 1);
}
String result = StringUtils.isNotBlank(payload) ? " => " + payload : "";
MobileMessagingLogger.d(TAG,"Widget API call result: " + method + "()" + result);
if (inAppChatWebViewManager != null) {
try {
inAppChatWebViewManager.onWidgetApiSuccess(InAppChatWidgetApiMethod.valueOf(method), successPayload);
inAppChatWebViewManager.onWidgetApiSuccess(InAppChatWidgetApiMethod.valueOf(method), payload);
} catch (IllegalArgumentException exception) {
MobileMessagingLogger.e(TAG,"Could not parse InAppChatWidgetApiMethod from " + method, exception);
}
Expand Down

0 comments on commit 05529d1

Please sign in to comment.