Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff for the 5th commit is easier to understand in split view, it's mostly code motion swapping the two chunks of logic, not code change

String notificationIds = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, "");
List<String> 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();
}
Expand Down Expand Up @@ -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<String> convertToArray(String string) {
Expand Down
Loading