Skip to content

Commit c5caa92

Browse files
committed
fix(messaging, android): prevent OOM by limiting stored notifications
1 parent b388022 commit c5caa92

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,31 @@
1818
public class ReactNativeFirebaseMessagingStoreImpl implements ReactNativeFirebaseMessagingStore {
1919

2020
private static final String S_KEY_ALL_NOTIFICATION_IDS = "all_notification_ids";
21-
private static final int MAX_SIZE_NOTIFICATIONS = 100;
2221
private final String DELIMITER = ",";
22+
private static int MAX_SIZE_NOTIFICATIONS = 20;
23+
private static boolean isInitialized = false;
24+
25+
private int getMaxNotificationSize() {
26+
if (isInitialized) return MAX_SIZE_NOTIFICATIONS;
27+
try {
28+
android.content.Context context =
29+
io.invertase.firebase.app.ReactNativeFirebaseApp.getApplicationContext();
30+
android.content.pm.ApplicationInfo appInfo =
31+
context
32+
.getPackageManager()
33+
.getApplicationInfo(
34+
context.getPackageName(), android.content.pm.PackageManager.GET_META_DATA);
35+
if (appInfo.metaData != null) {
36+
int configValue =
37+
appInfo.metaData.getInt("rn_firebase_messaging_max_stored_notifications", 20);
38+
MAX_SIZE_NOTIFICATIONS = Math.min(configValue, 100);
39+
}
40+
} catch (Exception e) {
41+
// Ignore and use default
42+
}
43+
isInitialized = true;
44+
return MAX_SIZE_NOTIFICATIONS;
45+
}
2346

2447
@Override
2548
public void storeFirebaseMessage(RemoteMessage remoteMessage) {
@@ -35,10 +58,11 @@ public void storeFirebaseMessage(RemoteMessage remoteMessage) {
3558

3659
// check and remove old notifications message
3760
List<String> allNotificationList = convertToArray(notifications);
38-
if (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS) {
61+
while (allNotificationList.size() > getMaxNotificationSize()) {
3962
String firstRemoteMessageId = allNotificationList.get(0);
4063
preferences.remove(firstRemoteMessageId);
4164
notifications = removeRemoteMessage(firstRemoteMessageId, notifications);
65+
allNotificationList.remove(0);
4266
}
4367
preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notifications);
4468
} catch (JSONException e) {

0 commit comments

Comments
 (0)