55import static io .invertase .firebase .messaging .ReactNativeFirebaseMessagingSerializer .remoteMessageFromReadableMap ;
66import 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 ;
118import com .facebook .react .bridge .ReadableMap ;
129import com .facebook .react .bridge .WritableMap ;
1310import com .google .firebase .messaging .RemoteMessage ;
14- import io .invertase .firebase .common .UniversalFirebasePreferences ;
11+
12+ import org .json .JSONException ;
13+ import org .json .JSONObject ;
14+
1515import java .util .ArrayList ;
1616import java .util .Arrays ;
1717import 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
2124public 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