Skip to content

Commit e86d62a

Browse files
committed
Fix messaging store max notification size
1 parent fae6843 commit e86d62a

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

packages/app/android/src/reactnative/java/io/invertase/firebase/common/ReactNativeFirebaseMeta.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public String getStringValue(String key, String defaultValue) {
7070
return metaData.getString(META_PREFIX + key, defaultValue);
7171
}
7272

73+
public int getIntValue(String key, int defaultValue) {
74+
Bundle metaData = getMetaData();
75+
if (metaData == null) return defaultValue;
76+
return metaData.getInt(META_PREFIX + key, defaultValue);
77+
}
78+
7379
public WritableMap getAll() {
7480
Bundle metaData = getMetaData();
7581
WritableMap map = Arguments.createMap();

packages/app/android/src/reactnative/java/io/invertase/firebase/common/ReactNativeFirebasePreferences.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public boolean getBooleanValue(String key, boolean defaultValue) {
4646
return getPreferences().getBoolean(key, defaultValue);
4747
}
4848

49+
public void setIntValue(String key, int value) {
50+
getPreferences().edit().putInt(key,value).apply();
51+
}
52+
53+
public int getIntValue(String key, int defaultValue) {
54+
return getPreferences().getInt(key,defaultValue);
55+
}
56+
4957
public void setLongValue(String key, long value) {
5058
getPreferences().edit().putLong(key, value).apply();
5159
}

packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStoreImpl.java

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
import static io.invertase.firebase.messaging.ReactNativeFirebaseMessagingSerializer.remoteMessageFromReadableMap;
66
import static io.invertase.firebase.messaging.ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap;
77

8-
import android.content.Context;
9-
import android.content.pm.ApplicationInfo;
10-
import android.content.pm.PackageManager;
118
import com.facebook.react.bridge.ReadableMap;
129
import com.facebook.react.bridge.WritableMap;
1310
import com.google.firebase.messaging.RemoteMessage;
14-
import io.invertase.firebase.common.UniversalFirebasePreferences;
11+
12+
import org.json.JSONException;
13+
import org.json.JSONObject;
14+
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
1717
import java.util.List;
18-
import org.json.JSONException;
19-
import org.json.JSONObject;
18+
19+
import io.invertase.firebase.common.ReactNativeFirebaseJSON;
20+
import io.invertase.firebase.common.ReactNativeFirebaseMeta;
21+
import io.invertase.firebase.common.ReactNativeFirebasePreferences;
22+
import io.invertase.firebase.common.UniversalFirebasePreferences;
2023

2124
public class ReactNativeFirebaseMessagingStoreImpl implements ReactNativeFirebaseMessagingStore {
2225

2326
private static final String KEY_MAX_STORED_NOTIFICATIONS = "rn_firebase_messaging_max_stored_notifications";
2427
private static final String S_KEY_ALL_NOTIFICATION_IDS = "all_notification_ids";
2528
private final String DELIMITER = ",";
2629
private static final int DEFAULT_MAX_SIZE_NOTIFICATIONS = 100;
27-
private static int MAX_SIZE_NOTIFICATIONS = DEFAULT_MAX_SIZE_NOTIFICATIONS;
28-
private static boolean isInitialized = false;
29-
30-
private int getMaxNotificationSize() {
31-
if (isInitialized) return MAX_SIZE_NOTIFICATIONS;
30+
private static final int maxNotificationSize = resolveMaxNotificationSize();
3231

32+
private static int resolveMaxNotificationSize() {
3333
int maxSize = DEFAULT_MAX_SIZE_NOTIFICATIONS;
3434
ReactNativeFirebaseJSON json = ReactNativeFirebaseJSON.getSharedInstance();
3535
ReactNativeFirebaseMeta meta = ReactNativeFirebaseMeta.getSharedInstance();
@@ -44,43 +44,29 @@ private int getMaxNotificationSize() {
4444
} else if (json.contains(KEY_MAX_STORED_NOTIFICATIONS)) {
4545
maxSize = json.getIntValue(KEY_MAX_STORED_NOTIFICATIONS, DEFAULT_MAX_SIZE_NOTIFICATIONS);
4646
} else if (meta.contains(KEY_MAX_STORED_NOTIFICATIONS)) {
47-
// ReactNativeFirebaseMeta uses rnfirebase_ prefix, but we want to read the raw key
48-
// So we read directly from AndroidManifest using the key as-is
49-
try {
50-
Context context = io.invertase.firebase.app.ReactNativeFirebaseApp.getApplicationContext();
51-
ApplicationInfo appInfo = context.getPackageManager()
52-
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
53-
if (appInfo != null && appInfo.metaData != null) {
54-
maxSize = appInfo.metaData.getInt(KEY_MAX_STORED_NOTIFICATIONS, DEFAULT_MAX_SIZE_NOTIFICATIONS);
55-
}
56-
} catch (Exception e) {
57-
// Ignore and use default
58-
}
47+
maxSize = meta.getIntValue(KEY_MAX_STORED_NOTIFICATIONS, DEFAULT_MAX_SIZE_NOTIFICATIONS);
5948
}
6049

6150
// Safety cap: prevent values > 100 to avoid re-introducing OOM risk
62-
MAX_SIZE_NOTIFICATIONS = Math.min(maxSize, DEFAULT_MAX_SIZE_NOTIFICATIONS);
51+
return Math.min(maxSize, DEFAULT_MAX_SIZE_NOTIFICATIONS);
6352
} catch (Exception e) {
6453
// Ignore and use default
65-
MAX_SIZE_NOTIFICATIONS = DEFAULT_MAX_SIZE_NOTIFICATIONS;
54+
return DEFAULT_MAX_SIZE_NOTIFICATIONS;
6655
}
67-
68-
isInitialized = true;
69-
return MAX_SIZE_NOTIFICATIONS;
7056
}
7157

7258
@Override
7359
public void storeFirebaseMessage(RemoteMessage remoteMessage) {
7460
try {
7561
String remoteMessageString =
76-
reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString();
62+
reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString();
7763
// Log.d("storeFirebaseMessage", remoteMessageString);
7864
UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance();
7965

8066
// remove old notifications message before store to free space as needed
8167
String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, "");
8268
List<String> allNotificationList = convertToArray(notificationIds);
83-
while (allNotificationList.size() > getMaxNotificationSize() - 1) {
69+
while (allNotificationList.size() > maxNotificationSize - 1) {
8470
clearFirebaseMessage(allNotificationList.get(0));
8571
allNotificationList.remove(0);
8672
}
@@ -109,7 +95,7 @@ public RemoteMessage getFirebaseMessage(String remoteMessageId) {
10995
@Override
11096
public WritableMap getFirebaseMessageMap(String remoteMessageId) {
11197
String remoteMessageString =
112-
UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null);
98+
UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null);
11399
if (remoteMessageString != null) {
114100
// Log.d("getFirebaseMessage", remoteMessageString);
115101
try {

0 commit comments

Comments
 (0)