【问题标题】:How to save FCM notification in a custom ArrayList?如何在自定义 ArrayList 中保存 FCM 通知?
【发布时间】:2019-01-21 16:21:23
【问题描述】:

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

【问题讨论】:

  • 它正在覆盖值,因为键是常量。并且解决方案是正确的,您可以使用 ROOM 保存您的对象。

标签: android shared preference


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2018-11-08
    • 1970-01-01
    • 2018-09-04
    • 2018-12-28
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多