Skip to content

Commit c85fec0

Browse files
committed
Pull request #475: MM-6987 InAppChat message handling updated, InfobipRtcUi caches livechat registrationId, InAppChat logging improvements
Merge in MML/infobip-mobile-messaging-android from jdzubak-MM-6987-notification-tap-intent-fix to master Squashed commit of the following: commit 580d26af8790fc4889b62c9e5b7cfd5bb8f502f7 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Tue Jan 14 10:41:07 2025 +0100 MM-6987 LcRegId is saved any time broadcast is triggered for possible later usage commit 4fee28e4578cf05205ab4749b984f85c7148b296 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Tue Jan 14 10:19:22 2025 +0100 MM-6987 Review fixes commit a9989fc9c6ee97c5d729bd0ec030d0048ef1df42 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Tue Jan 14 10:10:24 2025 +0100 MM-6987 Review fixes commit 8bbbf81fc4e1194b784904fb4935c0d6ad490cf0 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Jan 13 17:39:10 2025 +0100 MM-6987 Formatting commit 2c3dcb0da6f664b0a82fdd9a8226cc33cab36b92 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Jan 13 17:32:34 2025 +0100 MM-6987 Small update commit 05e5864f3c04fa9de423188e5c9256920a633608 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Jan 13 16:30:38 2025 +0100 MM-6987 Update commit de803a2adb4b3a29fb209813d93e7cb9fdd92f2e Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Jan 13 16:22:41 2025 +0100 MM-6987 InAppChat message handling updated, InfobipRtcUi caches livechat registrationId, InAppChat logging improvements
1 parent 6e56ea5 commit c85fec0

File tree

7 files changed

+143
-97
lines changed

7 files changed

+143
-97
lines changed

infobip-mobile-messaging-android-chat-sdk/src/main/java/org/infobip/mobile/messaging/chat/InAppChat.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import android.content.Context;
44

5-
import androidx.annotation.NonNull;
6-
import androidx.annotation.Nullable;
7-
import androidx.fragment.app.FragmentManager;
8-
95
import org.infobip.mobile.messaging.MobileMessaging;
106
import org.infobip.mobile.messaging.chat.core.MultithreadStrategy;
117
import org.infobip.mobile.messaging.chat.view.InAppChatEventsListener;
128
import org.infobip.mobile.messaging.chat.view.styles.InAppChatTheme;
139

10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
import androidx.fragment.app.FragmentManager;
13+
1414
/**
1515
* Main interface for in-app chat communication
1616
*/
@@ -69,7 +69,7 @@ public synchronized static InAppChat getInstance(Context context) {
6969
*
7070
* @param activityClasses array of activities to put into task stack when message is tapped
7171
*/
72-
public abstract void setActivitiesToStartOnMessageTap(Class... activityClasses);
72+
public abstract void setActivitiesToStartOnMessageTap(Class<?>... activityClasses);
7373

7474
/**
7575
* Cleans up all InAppChat data.

infobip-mobile-messaging-android-chat-sdk/src/main/java/org/infobip/mobile/messaging/chat/InAppChatImpl.java

+31-37
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,12 @@
1010
import android.view.View;
1111
import android.view.ViewGroup;
1212

13-
import androidx.annotation.NonNull;
14-
import androidx.annotation.Nullable;
15-
import androidx.appcompat.app.AppCompatActivity;
16-
import androidx.core.app.TaskStackBuilder;
17-
import androidx.fragment.app.Fragment;
18-
import androidx.fragment.app.FragmentManager;
19-
import androidx.fragment.app.FragmentTransaction;
20-
2113
import org.infobip.mobile.messaging.Event;
2214
import org.infobip.mobile.messaging.Message;
2315
import org.infobip.mobile.messaging.MessageHandlerModule;
2416
import org.infobip.mobile.messaging.MobileMessaging;
2517
import org.infobip.mobile.messaging.MobileMessagingCore;
2618
import org.infobip.mobile.messaging.MobileMessagingProperty;
27-
import org.infobip.mobile.messaging.NotificationSettings;
2819
import org.infobip.mobile.messaging.api.chat.WidgetInfo;
2920
import org.infobip.mobile.messaging.app.ActivityLifecycleMonitor;
3021
import org.infobip.mobile.messaging.chat.core.InAppChatBroadcasterImpl;
@@ -54,6 +45,14 @@
5445
import java.util.Locale;
5546
import java.util.concurrent.Executors;
5647

48+
import androidx.annotation.NonNull;
49+
import androidx.annotation.Nullable;
50+
import androidx.appcompat.app.AppCompatActivity;
51+
import androidx.core.app.TaskStackBuilder;
52+
import androidx.fragment.app.Fragment;
53+
import androidx.fragment.app.FragmentManager;
54+
import androidx.fragment.app.FragmentTransaction;
55+
5756

5857
public class InAppChatImpl extends InAppChat implements MessageHandlerModule {
5958

@@ -157,32 +156,27 @@ public boolean messageTapped(Message message) {
157156

158157
private void doCoreTappedActions(Message chatMessage) {
159158
TaskStackBuilder stackBuilder = stackBuilderForNotificationTap(chatMessage);
160-
if (stackBuilder.getIntentCount() != 0) {
159+
if (stackBuilder != null && stackBuilder.getIntentCount() > 0) {
161160
stackBuilder.startActivities();
162161
}
163162
}
164163

165-
@NonNull
164+
@Nullable
166165
private TaskStackBuilder stackBuilderForNotificationTap(Message message) {
167-
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
168-
Bundle messageBundle = MessageBundleMapper.messageToBundle(message);
169-
Class[] classes = propertyHelper().findClasses(MobileMessagingChatProperty.ON_MESSAGE_TAP_ACTIVITY_CLASSES);
166+
Class<?>[] classes = propertyHelper().findClasses(MobileMessagingChatProperty.ON_MESSAGE_TAP_ACTIVITY_CLASSES);
170167
if (classes != null) {
171-
for (Class cls : classes) {
172-
stackBuilder.addNextIntent(new Intent(context, cls)
173-
.setAction(Event.NOTIFICATION_TAPPED.getKey())
174-
.putExtras(messageBundle));
168+
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
169+
Bundle messageBundle = MessageBundleMapper.messageToBundle(message);
170+
for (Class<?> cls : classes) {
171+
stackBuilder.addNextIntent(
172+
new Intent(context, cls)
173+
.setAction(Event.NOTIFICATION_TAPPED.getKey())
174+
.putExtras(messageBundle)
175+
);
175176
}
177+
return stackBuilder;
176178
}
177-
178-
NotificationSettings notificationSettings = mobileMessagingCore().getNotificationSettings();
179-
if (stackBuilder.getIntentCount() == 0 && notificationSettings != null && notificationSettings.getCallbackActivity() != null) {
180-
stackBuilder.addNextIntent(new Intent(context, notificationSettings.getCallbackActivity())
181-
.setAction(Event.NOTIFICATION_TAPPED.getKey())
182-
.putExtras(messageBundle));
183-
}
184-
185-
return stackBuilder;
179+
return null;
186180
}
187181

188182
@Override
@@ -198,10 +192,10 @@ private void cleanupWidgetData() {
198192
handler.post(() -> {
199193
webView().clearHistory();
200194
webView().clearCache(true);
201-
MobileMessagingLogger.d(TAG,"Deleted local widget history");
195+
MobileMessagingLogger.d(TAG, "Deleted local widget history");
202196
});
203197
} catch (Exception e) {
204-
MobileMessagingLogger.w(TAG,"Failed to delete local widget history due to " + e.getMessage());
198+
MobileMessagingLogger.w(TAG, "Failed to delete local widget history due to " + e.getMessage());
205199
}
206200
}
207201

@@ -261,13 +255,13 @@ public InAppChatScreenImpl inAppChatScreen() {
261255
inAppChatScreen = new InAppChatScreenImpl(context);
262256
}
263257
if (!isActivated()) {
264-
MobileMessagingLogger.e(TAG,"In-app chat wasn't activated, call activate()");
258+
MobileMessagingLogger.e(TAG, "In-app chat wasn't activated, call activate()");
265259
}
266260
return inAppChatScreen;
267261
}
268262

269263
@Override
270-
public void setActivitiesToStartOnMessageTap(Class... activityClasses) {
264+
public void setActivitiesToStartOnMessageTap(Class<?>... activityClasses) {
271265
propertyHelper().saveClasses(MobileMessagingChatProperty.ON_MESSAGE_TAP_ACTIVITY_CLASSES, activityClasses);
272266
}
273267

@@ -336,7 +330,7 @@ private boolean areFragmentsEquals(Fragment f1, Fragment f2) {
336330

337331
@Override
338332
public void resetMessageCounter() {
339-
MobileMessagingLogger.d(TAG,"Resetting unread message counter to 0");
333+
MobileMessagingLogger.d(TAG, "Resetting unread message counter to 0");
340334
propertyHelper().remove(MobileMessagingChatProperty.UNREAD_CHAT_MESSAGES_COUNT);
341335
inAppChatBroadcaster().unreadMessagesCounterUpdated(0);
342336
}
@@ -352,7 +346,7 @@ public void setLanguage(String language) {
352346
@Override
353347
public void onResult(Result<String, MobileMessagingError> result) {
354348
if (!result.isSuccess()) {
355-
MobileMessagingLogger.e(TAG,"Set language error: " + result.getError().getMessage());
349+
MobileMessagingLogger.e(TAG, "Set language error: " + result.getError().getMessage());
356350
}
357351
}
358352
});
@@ -384,7 +378,7 @@ public void sendContextualData(@Nullable String data) {
384378
@Override
385379
public void onResult(Result<Void, MobileMessagingError> result) {
386380
if (!result.isSuccess()) {
387-
MobileMessagingLogger.e(TAG,"Send contextual data error: " + result.getError().getMessage());
381+
MobileMessagingLogger.e(TAG, "Send contextual data error: " + result.getError().getMessage());
388382
}
389383
}
390384
});
@@ -396,7 +390,7 @@ public void sendContextualData(@Nullable String data, @Nullable Boolean allMulti
396390
@Override
397391
public void onResult(Result<Void, MobileMessagingError> result) {
398392
if (!result.isSuccess()) {
399-
MobileMessagingLogger.e(TAG,"Send contextual data error: " + result.getError().getMessage());
393+
MobileMessagingLogger.e(TAG, "Send contextual data error: " + result.getError().getMessage());
400394
}
401395
}
402396
});
@@ -467,7 +461,7 @@ public void showThreadsList() {
467461
if (inAppChatWVFragment != null) {
468462
inAppChatWVFragment.showThreadList();
469463
} else {
470-
MobileMessagingLogger.e(TAG,"Function showThreadsList() skipped, InAppChatFragment has not been shown yet.");
464+
MobileMessagingLogger.e(TAG, "Function showThreadsList() skipped, InAppChatFragment has not been shown yet.");
471465
}
472466
}
473467

@@ -512,7 +506,7 @@ public void setChatPushTitle(@Nullable String title) {
512506
}
513507

514508
@Override
515-
public void setChatPushBody(@Nullable String body) {
509+
public void setChatPushBody(@Nullable String body) {
516510
PreferenceHelper.saveString(context, MobileMessagingProperty.DEFAULT_IN_APP_CHAT_PUSH_BODY, body);
517511
}
518512

infobip-mobile-messaging-android-chat-sdk/src/main/java/org/infobip/mobile/messaging/chat/core/InAppChatClientImpl.java

+51-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package org.infobip.mobile.messaging.chat.core;
22

3-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatPause;
4-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatResume;
5-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendContextualData;
6-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendDraft;
7-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessage;
8-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessageWithAttachment;
9-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setLanguage;
10-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setTheme;
11-
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.showThreadList;
12-
import static org.infobip.mobile.messaging.util.StringUtils.isNotBlank;
13-
143
import android.os.Handler;
154
import android.os.Looper;
165

@@ -22,10 +11,27 @@
2211
import org.infobip.mobile.messaging.mobileapi.Result;
2312
import org.infobip.mobile.messaging.util.StringUtils;
2413

14+
import java.util.ArrayList;
15+
import java.util.List;
2516
import java.util.Locale;
2617

18+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatPause;
19+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.mobileChatResume;
20+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendContextualData;
21+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendDraft;
22+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessage;
23+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.sendMessageWithAttachment;
24+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setLanguage;
25+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.setTheme;
26+
import static org.infobip.mobile.messaging.chat.core.InAppChatWidgetMethod.showThreadList;
27+
import static org.infobip.mobile.messaging.util.StringUtils.isNotBlank;
28+
2729
public class InAppChatClientImpl implements InAppChatClient {
2830

31+
private static final int MAX_ALLOWED_SCRIPT_LENGTH = 200;
32+
private static final int MAX_ALLOWED_ARGUMENT_LENGTH = 50;
33+
private static final int ARGUMENT_VISIBLE_PART_LENGTH = 15;
34+
2935
private final InAppChatWebView webView;
3036
private static final String TAG = InAppChatClient.class.getSimpleName();
3137
private final Handler handler = new Handler(Looper.getMainLooper());
@@ -127,17 +133,18 @@ private void executeScript(String script, MobileMessaging.ResultListener<String>
127133
try {
128134
handler.post(() -> webView.evaluateJavascript(script, value -> {
129135
String valueToLog = (value != null && !"null".equals(value)) ? " => " + value : "";
130-
MobileMessagingLogger.d(TAG, "Called Widget API: " + script + valueToLog);
136+
String scriptToLog = shortenScript(script);
137+
MobileMessagingLogger.d(TAG, "Called Widget API: " + scriptToLog + valueToLog);
131138
if (resultListener != null)
132139
resultListener.onResult(new Result<>(valueToLog));
133140
}));
134141
} catch (Exception e) {
135142
if (resultListener != null)
136143
resultListener.onResult(new Result<>(MobileMessagingError.createFrom(e)));
137-
MobileMessagingLogger.e(TAG,"Failed to execute webView JS script " + script + " " + e.getMessage());
144+
MobileMessagingLogger.e(TAG, "Failed to execute webView JS script " + shortenScript(script) + " " + e.getMessage());
138145
}
139146
} else if (resultListener != null) {
140-
resultListener.onResult(new Result<>(MobileMessagingError.createFrom(new IllegalStateException("Failed to execute webView JS script " + script + " InAppChatWebView is null."))));
147+
resultListener.onResult(new Result<>(MobileMessagingError.createFrom(new IllegalStateException("Failed to execute webView JS script " + shortenScript(script) + " InAppChatWebView is null."))));
141148
}
142149
}
143150

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

159166
return builder.toString();
160167
}
168+
169+
private String shortenScript(String script) {
170+
if (script != null && script.length() > MAX_ALLOWED_SCRIPT_LENGTH) {
171+
StringBuilder builder = new StringBuilder();
172+
int methodNameEndIndex = script.indexOf("(");
173+
if (methodNameEndIndex > 0) {
174+
String methodName = script.substring(0, methodNameEndIndex);
175+
builder.append(methodName);
176+
String paramsSubstring = script.substring(methodNameEndIndex + 1, script.length() - 1);
177+
String[] paramsArray = paramsSubstring.split(",");
178+
if (paramsArray.length > 0) {
179+
List<String> shortenedParams = new ArrayList<>();
180+
for (String param : paramsArray) {
181+
String value = param.replace("'", "");
182+
if (value.length() > MAX_ALLOWED_ARGUMENT_LENGTH) {
183+
value = value.substring(0, ARGUMENT_VISIBLE_PART_LENGTH) + "..." + value.substring(value.length() - ARGUMENT_VISIBLE_PART_LENGTH);
184+
}
185+
shortenedParams.add(value);
186+
}
187+
String params = StringUtils.join("','", "('", "')", shortenedParams.toArray(new String[0]));
188+
builder.append(params);
189+
}
190+
else {
191+
builder.append("()");
192+
}
193+
}
194+
return builder.toString();
195+
}
196+
return script;
197+
}
161198
}

infobip-mobile-messaging-android-chat-sdk/src/main/java/org/infobip/mobile/messaging/chat/core/InAppChatMobileImpl.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import android.os.Looper;
66
import android.webkit.JavascriptInterface;
77

8-
import androidx.fragment.app.Fragment;
9-
import androidx.fragment.app.FragmentActivity;
10-
118
import org.infobip.mobile.messaging.logging.MobileMessagingLogger;
129
import org.infobip.mobile.messaging.util.StringUtils;
1310

11+
import androidx.fragment.app.Fragment;
12+
import androidx.fragment.app.FragmentActivity;
13+
1414
public class InAppChatMobileImpl implements InAppChatMobile {
1515

1616
private final InAppChatWebViewManager inAppChatWebViewManager;
@@ -98,11 +98,15 @@ public void onWidgetApiError(String method, String errorPayload) {
9898
@JavascriptInterface
9999
public void onWidgetApiSuccess(String method, String successPayload) {
100100
Runnable myRunnable = () -> {
101-
String result = StringUtils.isNotBlank(successPayload) ? " => " + successPayload : "";
101+
String payload = successPayload;
102+
if (StringUtils.isNotBlank(payload) && payload.startsWith("\"") && payload.endsWith("\"") && payload.length() > 2) {
103+
payload = payload.substring(1, payload.length() - 1);
104+
}
105+
String result = StringUtils.isNotBlank(payload) ? " => " + payload : "";
102106
MobileMessagingLogger.d(TAG,"Widget API call result: " + method + "()" + result);
103107
if (inAppChatWebViewManager != null) {
104108
try {
105-
inAppChatWebViewManager.onWidgetApiSuccess(InAppChatWidgetApiMethod.valueOf(method), successPayload);
109+
inAppChatWebViewManager.onWidgetApiSuccess(InAppChatWidgetApiMethod.valueOf(method), payload);
106110
} catch (IllegalArgumentException exception) {
107111
MobileMessagingLogger.e(TAG,"Could not parse InAppChatWidgetApiMethod from " + method, exception);
108112
}

infobip-rtc-ui/src/main/java/com/infobip/webrtc/ui/internal/core/Cache.kt

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ internal interface SdkLifetimeCache {
7272
val inCallScreenStyle: InCallScreenStyle?
7373
var inCallButtons: List<InCallButton>
7474
var callErrorMapper: RtcUiCallErrorMapper?
75+
76+
/**
77+
* Last reported livechat registration id. It can be used to re-enable in-app chat calls multiple times.
78+
*/
79+
var livechatRegistrationId: String?
7580
}
7681

7782
internal class SdkLifetimeCacheImpl : SdkLifetimeCache {
@@ -103,6 +108,7 @@ internal class SdkLifetimeCacheImpl : SdkLifetimeCache {
103108
InCallButton.FlipCam(),
104109
)
105110
override var callErrorMapper: RtcUiCallErrorMapper? = null
111+
override var livechatRegistrationId: String? = null
106112
}
107113
//endregion
108114

0 commit comments

Comments
 (0)