Android - 在共享首选项中保存通知数据
Android - Save Notifications data in Shared Preferences
我正在开发一个 Android 应用程序,我使用 firebase 推送通知,我希望 onMessageReceived() 应该将通知数据保存到共享首选项中,以便在将来我会显示一个列表视图,用户可以在其中看到所有通知。
问题:我的问题是有时 APP 将通知保存到 sharedPrefs,有时不保存,如果我在后台收到通知,它永远不会保存到共享 prefs。
我做了以下实现:
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null){
Log.d(TAG, "onMessageReceived: Printing Message details: ");
Log.d(TAG, "Title: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
if (remoteMessage.getData().size() > 0) {
Map<String, String> msgData = remoteMessage.getData();
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
Notification notification;
String title = remoteMessage.getNotification().getTitle();
String msg = remoteMessage.getNotification().getBody();
notification = new Notification(title, msg);
ArrayList<Notification> notificationsList = AppData.getInstance(getApplicationContext()).getNotifications();
notificationsList.add(notification);
AppData.getInstance(getApplicationContext()).setNotifications(notificationsList);
generateNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getImageUrl());
}
Notification.java
public class Notification {
String msgTitle;
String msgBody;
String imageURL;
String time;
Boolean read = false;
public Notification(String msgTitle, String msgBody) {
this.msgTitle = msgTitle;
this.msgBody = msgBody;
}
}
AppData 是一个单例 class,它在共享首选项中保存数据。
我正在使用的 AppData 方法:
public ArrayList<Notification> getNotifications() {
SharedPrefrences.init(context);
Gson gson = new Gson();
String json = SharedPrefrences.read(Constants.E_NOTIFICATIONS, "");
if(json == "") return null;
Notification[] notifications = gson.fromJson(json, Notification[].class);
ArrayList<Notification> notificationArrayList = new ArrayList<>();
notificationArrayList.addAll(Arrays.asList(notifications));
return notificationArrayList;
}
public void setNotifications(ArrayList<Notification> notifications) {
this.notifications = notifications;
SharedPrefrences.init(context);
Gson gson = new Gson();
String json = gson.toJson(notifications);
SharedPrefrences.write(Constants.E_NOTIFICATIONS, json);
}
是的,@NareshNK。您的评论帮助我找到了确切的解决方案。
问题是,
- 我正在使用 firebase 通知有效负载发送通知,我正在尝试阅读。
当我开始发送带有数据的消息时 json。我开始在数据负载中收到响应,我的问题已解决。
我正在开发一个 Android 应用程序,我使用 firebase 推送通知,我希望 onMessageReceived() 应该将通知数据保存到共享首选项中,以便在将来我会显示一个列表视图,用户可以在其中看到所有通知。
问题:我的问题是有时 APP 将通知保存到 sharedPrefs,有时不保存,如果我在后台收到通知,它永远不会保存到共享 prefs。
我做了以下实现:
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null){
Log.d(TAG, "onMessageReceived: Printing Message details: ");
Log.d(TAG, "Title: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
if (remoteMessage.getData().size() > 0) {
Map<String, String> msgData = remoteMessage.getData();
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
Notification notification;
String title = remoteMessage.getNotification().getTitle();
String msg = remoteMessage.getNotification().getBody();
notification = new Notification(title, msg);
ArrayList<Notification> notificationsList = AppData.getInstance(getApplicationContext()).getNotifications();
notificationsList.add(notification);
AppData.getInstance(getApplicationContext()).setNotifications(notificationsList);
generateNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getImageUrl());
}
Notification.java
public class Notification {
String msgTitle;
String msgBody;
String imageURL;
String time;
Boolean read = false;
public Notification(String msgTitle, String msgBody) {
this.msgTitle = msgTitle;
this.msgBody = msgBody;
}
}
AppData 是一个单例 class,它在共享首选项中保存数据。
我正在使用的 AppData 方法:
public ArrayList<Notification> getNotifications() {
SharedPrefrences.init(context);
Gson gson = new Gson();
String json = SharedPrefrences.read(Constants.E_NOTIFICATIONS, "");
if(json == "") return null;
Notification[] notifications = gson.fromJson(json, Notification[].class);
ArrayList<Notification> notificationArrayList = new ArrayList<>();
notificationArrayList.addAll(Arrays.asList(notifications));
return notificationArrayList;
}
public void setNotifications(ArrayList<Notification> notifications) {
this.notifications = notifications;
SharedPrefrences.init(context);
Gson gson = new Gson();
String json = gson.toJson(notifications);
SharedPrefrences.write(Constants.E_NOTIFICATIONS, json);
}
是的,@NareshNK。您的评论帮助我找到了确切的解决方案。 问题是,
- 我正在使用 firebase 通知有效负载发送通知,我正在尝试阅读。
当我开始发送带有数据的消息时 json。我开始在数据负载中收到响应,我的问题已解决。