如何将FCM通知保存在自定义ArrayList中?

时间:2019-01-21 16:21:24

标签: android shared preference

我试图以共享首选项保存自定义Arraylist,但是当我重新启动或重新运行并发送新的推送时,它会覆盖其他的

 @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();
}

1 个答案:

答案 0 :(得分:1)

您不应将Notification中的数据保存在自定义ArrayList中,而应将数据保存到SQLite数据库中