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

4

我试图将自定义的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
它正在覆盖值,因为键是一个常量。解决方案是正确的,您可以使用ROOM保存对象。 - cutiko
1个回答

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接