|
8 | 8 | import com.facebook.react.bridge.ReadableMap; |
9 | 9 | import com.facebook.react.bridge.WritableMap; |
10 | 10 | 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 | + |
12 | 15 | import java.util.ArrayList; |
13 | 16 | import java.util.Arrays; |
14 | 17 | 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; |
17 | 23 |
|
18 | 24 | public class ReactNativeFirebaseMessagingStoreImpl implements ReactNativeFirebaseMessagingStore { |
19 | 25 |
|
| 26 | + private static final String KEY_MAX_STORED_NOTIFICATIONS = "rn_firebase_messaging_max_stored_notifications"; |
20 | 27 | private static final String S_KEY_ALL_NOTIFICATION_IDS = "all_notification_ids"; |
21 | | - private static final int MAX_SIZE_NOTIFICATIONS = 100; |
22 | 28 | 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 | + } |
23 | 57 |
|
24 | 58 | @Override |
25 | 59 | public void storeFirebaseMessage(RemoteMessage remoteMessage) { |
26 | 60 | try { |
27 | 61 | String remoteMessageString = |
28 | | - reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString(); |
| 62 | + reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString(); |
29 | 63 | // Log.d("storeFirebaseMessage", remoteMessageString); |
30 | 64 | UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance(); |
31 | 65 |
|
32 | 66 | // remove old notifications message before store to free space as needed |
33 | 67 | String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); |
34 | 68 | List<String> allNotificationList = convertToArray(notificationIds); |
35 | | - while (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS - 1) { |
| 69 | + while (allNotificationList.size() > maxNotificationSize - 1) { |
36 | 70 | clearFirebaseMessage(allNotificationList.get(0)); |
37 | 71 | allNotificationList.remove(0); |
38 | 72 | } |
@@ -61,7 +95,7 @@ public RemoteMessage getFirebaseMessage(String remoteMessageId) { |
61 | 95 | @Override |
62 | 96 | public WritableMap getFirebaseMessageMap(String remoteMessageId) { |
63 | 97 | String remoteMessageString = |
64 | | - UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null); |
| 98 | + UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null); |
65 | 99 | if (remoteMessageString != null) { |
66 | 100 | // Log.d("getFirebaseMessage", remoteMessageString); |
67 | 101 | try { |
|
0 commit comments