Search code examples
androidsharedpreference

How to save FCM notification in a custom ArrayList?


I'm trying to save a custom Arraylist in shared preference, but when i restart or re-run and send a new push ,it overwritte the others

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    NotificationData data = new NotificationData(remoteMessage.getData());
    notificationDataArray.add(data);

   SharedPreferences sharedPreferences = getSharedPreferences(NotificationShared.SHARED_PREFERENCES, Context.MODE_PRIVATE);
   SharedPreferences.Editor editor = sharedPreferences.edit();

    Gson gson = new Gson();
    String json = gson.toJson(notificationDataArray);
    editor.putString(NotificationShared.DATA_ARRAY, json);
    editor.apply();
}

Solution

  • You shouldn't save the data from the Notification in a custom ArrayList, but save the data into SQLite database