Skip to content

Commit 41ca8ef

Browse files
committed
fix(messaging): honor custom max stored notifications
1 parent 47ea774 commit 41ca8ef

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
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: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,65 @@
88
import com.facebook.react.bridge.ReadableMap;
99
import com.facebook.react.bridge.WritableMap;
1010
import com.google.firebase.messaging.RemoteMessage;
11-
import io.invertase.firebase.common.UniversalFirebasePreferences;
11+
12+
import org.json.JSONException;
13+
import org.json.JSONObject;
14+
1215
import java.util.ArrayList;
1316
import java.util.Arrays;
1417
import java.util.List;
15-
import org.json.JSONException;
16-
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;
1723

1824
public class ReactNativeFirebaseMessagingStoreImpl implements ReactNativeFirebaseMessagingStore {
1925

26+
private static final String KEY_MAX_STORED_NOTIFICATIONS = "rn_firebase_messaging_max_stored_notifications";
2027
private static final String S_KEY_ALL_NOTIFICATION_IDS = "all_notification_ids";
21-
private static final int MAX_SIZE_NOTIFICATIONS = 100;
2228
private final String DELIMITER = ",";
29+
private static final int DEFAULT_MAX_SIZE_NOTIFICATIONS = 100;
30+
private static final int maxNotificationSize = resolveMaxNotificationSize();
31+
32+
private static int resolveMaxNotificationSize() {
33+
int maxSize = DEFAULT_MAX_SIZE_NOTIFICATIONS;
34+
ReactNativeFirebaseJSON json = ReactNativeFirebaseJSON.getSharedInstance();
35+
ReactNativeFirebaseMeta meta = ReactNativeFirebaseMeta.getSharedInstance();
36+
ReactNativeFirebasePreferences prefs = ReactNativeFirebasePreferences.getSharedInstance();
37+
38+
try {
39+
// Priority: SharedPreferences -> firebase.json -> AndroidManifest
40+
// Note: ReactNativeFirebaseMeta doesn't have getIntValue, so we check meta.contains
41+
// and read from AndroidManifest directly if needed
42+
if (prefs.contains(KEY_MAX_STORED_NOTIFICATIONS)) {
43+
maxSize = prefs.getIntValue(KEY_MAX_STORED_NOTIFICATIONS, DEFAULT_MAX_SIZE_NOTIFICATIONS);
44+
} else if (json.contains(KEY_MAX_STORED_NOTIFICATIONS)) {
45+
maxSize = json.getIntValue(KEY_MAX_STORED_NOTIFICATIONS, DEFAULT_MAX_SIZE_NOTIFICATIONS);
46+
} else if (meta.contains(KEY_MAX_STORED_NOTIFICATIONS)) {
47+
maxSize = meta.getIntValue(KEY_MAX_STORED_NOTIFICATIONS, DEFAULT_MAX_SIZE_NOTIFICATIONS);
48+
}
49+
50+
// Safety cap: prevent values > 100 to avoid re-introducing OOM risk
51+
return Math.min(maxSize, DEFAULT_MAX_SIZE_NOTIFICATIONS);
52+
} catch (Exception e) {
53+
// Ignore and use default
54+
return DEFAULT_MAX_SIZE_NOTIFICATIONS;
55+
}
56+
}
2357

2458
@Override
2559
public void storeFirebaseMessage(RemoteMessage remoteMessage) {
2660
try {
2761
String remoteMessageString =
28-
reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString();
62+
reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString();
2963
// Log.d("storeFirebaseMessage", remoteMessageString);
3064
UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance();
3165

3266
// remove old notifications message before store to free space as needed
3367
String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, "");
3468
List<String> allNotificationList = convertToArray(notificationIds);
35-
while (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS - 1) {
69+
while (allNotificationList.size() > maxNotificationSize - 1) {
3670
clearFirebaseMessage(allNotificationList.get(0));
3771
allNotificationList.remove(0);
3872
}
@@ -61,7 +95,7 @@ public RemoteMessage getFirebaseMessage(String remoteMessageId) {
6195
@Override
6296
public WritableMap getFirebaseMessageMap(String remoteMessageId) {
6397
String remoteMessageString =
64-
UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null);
98+
UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null);
6599
if (remoteMessageString != null) {
66100
// Log.d("getFirebaseMessage", remoteMessageString);
67101
try {

0 commit comments

Comments
 (0)