diff --git a/packages/app/android/src/main/java/io/invertase/firebase/common/UniversalFirebasePreferences.java b/packages/app/android/src/main/java/io/invertase/firebase/common/UniversalFirebasePreferences.java index 2c7d0cad84..4fd60b88be 100644 --- a/packages/app/android/src/main/java/io/invertase/firebase/common/UniversalFirebasePreferences.java +++ b/packages/app/android/src/main/java/io/invertase/firebase/common/UniversalFirebasePreferences.java @@ -74,6 +74,8 @@ public void clearAll() { getPreferences().edit().clear().apply(); } + // Note the caller is responsible for calling apply() or commit() on the + // returned SharedPreferences.Editor object for the remove to take affect public SharedPreferences.Editor remove(String key) { return getPreferences().edit().remove(key); } diff --git a/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStoreImpl.java b/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStoreImpl.java index c7d2c5fb76..03cb8f2eaf 100644 --- a/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStoreImpl.java +++ b/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStoreImpl.java @@ -28,19 +28,21 @@ public void storeFirebaseMessage(RemoteMessage remoteMessage) { reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString(); // Log.d("storeFirebaseMessage", remoteMessageString); UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance(); - preferences.setStringValue(remoteMessage.getMessageId(), remoteMessageString); - // save new notification id - String notifications = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); - notifications += remoteMessage.getMessageId() + DELIMITER; // append to last - // check and remove old notifications message - List allNotificationList = convertToArray(notifications); - if (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS) { - String firstRemoteMessageId = allNotificationList.get(0); - preferences.remove(firstRemoteMessageId); - notifications = removeRemoteMessage(firstRemoteMessageId, notifications); + // remove old notifications message before store to free space as needed + String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); + List allNotificationList = convertToArray(notificationIds); + while (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS - 1) { + clearFirebaseMessage(allNotificationList.get(0)); + allNotificationList.remove(0); } - preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notifications); + + // now refetch the ids after possible removals, and store the new message + notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); + preferences.setStringValue(remoteMessage.getMessageId(), remoteMessageString); + // save new notification id + notificationIds += remoteMessage.getMessageId() + DELIMITER; // append to last + preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notificationIds); } catch (JSONException e) { e.printStackTrace(); } @@ -76,17 +78,17 @@ public WritableMap getFirebaseMessageMap(String remoteMessageId) { @Override public void clearFirebaseMessage(String remoteMessageId) { UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance(); - preferences.remove(remoteMessageId); + preferences.remove(remoteMessageId).apply(); // check and remove old notifications message - String notifications = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); - if (!notifications.isEmpty()) { - notifications = removeRemoteMessage(remoteMessageId, notifications); // remove from list - preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notifications); + String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); + if (!notificationIds.isEmpty()) { + notificationIds = removeRemoteMessageId(remoteMessageId, notificationIds); // remove from list + preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notificationIds); } } - private String removeRemoteMessage(String remoteMessageId, String notifications) { - return notifications.replace(remoteMessageId + DELIMITER, ""); + private String removeRemoteMessageId(String remoteMessageId, String notificationIds) { + return notificationIds.replace(remoteMessageId + DELIMITER, ""); } private List convertToArray(String string) {