首页 > 解决方案 > 如何在自定义 ArrayList 中保存 FCM 通知?

问题描述

我正在尝试将自定义 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();
}

标签: androidsharedpreference

解决方案


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


推荐阅读