Skip to content

Commit 68f33d9

Browse files
jakubdzubak1Matuš Tokar
authored andcommitted
Pull request #180: MCA-4999 Fixed events caching issue.
Merge in MML/infobip-mobile-messaging-react-native-plugin from jdzubak-MCA-4999-caching-issue-fix to master Squashed commit of the following: commit 9049af412448d190ab4213ef9bff40d6c0d1f113 Author: Jakub Dzubak <[email protected]> Date: Tue Sep 30 19:38:36 2025 +0200 MCA-4999 Fixed events caching issue.
1 parent ccc5c7a commit 68f33d9

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

Example/android/build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ buildscript {
1010
repositories {
1111
google()
1212
mavenCentral()
13+
mavenLocal()
1314
}
1415
dependencies {
1516
classpath("com.android.tools.build:gradle")
1617
classpath("com.facebook.react:react-native-gradle-plugin")
1718
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
18-
classpath 'com.google.gms:google-services:4.4.2'
19+
classpath("com.google.gms:google-services:4.4.2")
1920
}
2021
}
2122

22-
apply plugin: "com.facebook.react.rootproject"
23+
allprojects {
24+
repositories {
25+
google()
26+
mavenCentral()
27+
mavenLocal()
28+
}
29+
}
30+
31+
apply plugin: "com.facebook.react.rootproject"

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ repositories {
8787
}
8888

8989
dependencies {
90-
def mmVersion = '14.6.0'
90+
def mmVersion = '14.6.2'
9191
//react and mm dependencies clash
9292
if (!overrideKotlinVersion.empty) {
9393
constraints {

android/src/main/java/org/infobip/reactlibrary/mobilemessaging/CacheManager.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.content.SharedPreferences;
55
import android.preference.PreferenceManager;
66

7+
import android.util.Log;
8+
79
import org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer;
810
import org.infobip.mobile.messaging.dal.json.JSONArrayAdapter;
911
import org.infobip.mobile.messaging.dal.json.JSONObjectAdapter;
@@ -39,17 +41,29 @@ public String toString() {
3941
}
4042

4143
static void saveEvent(Context context, String event, JSONObject object, String actionId, String actionInputText) {
44+
if (context == null) {
45+
Log.e(Utils.TAG, "context is null, can't cache event " + event);
46+
return;
47+
}
4248
String serialized = serializer.serialize(new Event(event, object, actionId, actionInputText));
4349
saveStringsToSet(context, EVENTS_KEY, serialized);
4450
}
4551

4652
static void saveEvent(Context context, String event, int unreadMessagesCounter) {
53+
if (context == null) {
54+
Log.e(Utils.TAG, "context is null, can't cache event " + event);
55+
return;
56+
}
4757
//int `unreadMessagesCounter` isn't a JSONObject, so it'll go as a second argument
4858
String serialized = serializer.serialize(new Event(event, null, unreadMessagesCounter));
4959
saveStringsToSet(context, EVENTS_KEY, serialized);
5060
}
5161

5262
static Event[] loadEvents(Context context, String eventType) {
63+
if (context == null) {
64+
Log.e(Utils.TAG, "context is null, can't load cached events " + eventType);
65+
return new Event[0];
66+
}
5367
Set<String> serialized = getStringSet(context, EVENTS_KEY);
5468
if (serialized == null || serialized.isEmpty()) {
5569
return new Event[0];

android/src/main/java/org/infobip/reactlibrary/mobilemessaging/ReactNativeMobileMessagingModule.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public void onReceive(Context context, Intent intent) {
261261
if (!pluginInitialized) {
262262
CacheManager.saveEvent(context, event, message, actionId, actionInputText);
263263
} else {
264-
emitOrCache(event, getReactContext(context), message, actionId, actionInputText);
264+
emitOrCache(event, context, message, actionId, actionInputText);
265265
}
266266
}
267267

@@ -270,7 +270,7 @@ private void handleUnreadMessageCounterIntent(Context context, Intent intent, St
270270
if (!pluginInitialized) {
271271
CacheManager.saveEvent(context, event, unreadChatMessagesCounter);
272272
} else {
273-
emitOrCache(event, getReactContext(context), unreadChatMessagesCounter);
273+
emitOrCache(event, context, unreadChatMessagesCounter);
274274
}
275275
}
276276

@@ -282,19 +282,29 @@ private ReactContext getReactContext(Context context) {
282282
return reactApplication.getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
283283
}
284284

285-
private void emitOrCache(String eventType, ReactContext reactContext, JSONObject message, String actionId, String actionInputText) {
285+
private void emitOrCache(String eventType, Context context, JSONObject message, String actionId, String actionInputText) {
286+
ReactContext reactContext = getReactContext(context);
286287
if (jsHasListeners && reactContext != null) {
287288
ReactNativeEvent.send(eventType, reactContext, message, actionId, actionInputText);
288-
} else {
289+
} else if (reactContext != null) {
289290
CacheManager.saveEvent(reactContext, eventType, message, actionId, actionInputText);
291+
} else if (context != null) {
292+
CacheManager.saveEvent(context, eventType, message, actionId, actionInputText);
293+
} else {
294+
Log.e(Utils.TAG, "Both reactContext and androidContext are null, can't emit or cache event " + eventType);
290295
}
291296
}
292297

293-
private void emitOrCache(String eventType, ReactContext reactContext, int unreadMessagesCounter) {
298+
private void emitOrCache(String eventType, Context context, int unreadMessagesCounter) {
299+
ReactContext reactContext = getReactContext(context);
294300
if (jsHasListeners && reactContext != null) {
295301
ReactNativeEvent.send(eventType, reactContext, unreadMessagesCounter);
296-
} else {
302+
} else if (reactContext != null) {
297303
CacheManager.saveEvent(reactContext, eventType, unreadMessagesCounter);
304+
} else if (context != null) {
305+
CacheManager.saveEvent(context, eventType, unreadMessagesCounter);
306+
} else {
307+
Log.e(Utils.TAG, "Both reactContext and androidContext are null, can't emit or cache event " + eventType);
298308
}
299309
}
300310
}

0 commit comments

Comments
 (0)