Skip to content

Commit 47ea774

Browse files
committed
fix(messaging, android): purge message store to limit first, add second
previously we added before purging, so if close to an OOM we would trigger it, if an app update is pushed with this change, it will ideally recover completely from this situation by purging the store down before any new data is added
1 parent 53fb8c8 commit 47ea774

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ public void storeFirebaseMessage(RemoteMessage remoteMessage) {
2828
reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString();
2929
// Log.d("storeFirebaseMessage", remoteMessageString);
3030
UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance();
31-
preferences.setStringValue(remoteMessage.getMessageId(), remoteMessageString);
32-
// save new notification id
33-
String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, "");
34-
notificationIds += remoteMessage.getMessageId() + DELIMITER; // append to last
35-
preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notificationIds);
3631

37-
// check and remove old notifications message
32+
// remove old notifications message before store to free space as needed
33+
String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, "");
3834
List<String> allNotificationList = convertToArray(notificationIds);
39-
while (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS) {
35+
while (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS - 1) {
4036
clearFirebaseMessage(allNotificationList.get(0));
4137
allNotificationList.remove(0);
4238
}
39+
40+
// now refetch the ids after possible removals, and store the new message
41+
notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, "");
42+
preferences.setStringValue(remoteMessage.getMessageId(), remoteMessageString);
43+
// save new notification id
44+
notificationIds += remoteMessage.getMessageId() + DELIMITER; // append to last
45+
preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notificationIds);
4346
} catch (JSONException e) {
4447
e.printStackTrace();
4548
}

0 commit comments

Comments
 (0)