【问题标题】:Android - Save Notifications data in Shared PreferencesAndroid - 在共享首选项中保存通知数据
【发布时间】:2022-01-06 00:54:59
【问题描述】:

我正在开发一个 Android 应用程序,我使用 firebase 推送通知,我希望 onMessageReceived() 应该将通知数据保存到共享首选项中,以便将来我将显示一个列表视图,其中用户将能够看到所有通知。

问题:我的问题是有时APP将通知保存到sharedPrefs,有时不保存,如果我在后台收到通知,那么它永远不会保存到共享首选项。

我做了以下实现:

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 是一个单例类,它将数据保存在共享首选项中。

我正在使用的 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);
}

【问题讨论】:

  • 您正在使用通知负载,这就是为什么在应用程序处于后台时不会调用 onMessageReceived 的原因。仅使用数据负载,请参阅此答案:stackoverflow.com/a/37845174/9854554
  • stackoverflow.com/a/56987273/3522570 有一些选项(或)可以获取收到的消息的数据.. 尝试在应用程序处于后台时使用它.. 确保这会节省您的时间
  • @NareshNK 数据负载总是为空。
  • @GaneshPokale 没用!
  • 如果您从 firebase 控制台发送通知,请尝试使用邮递员:stackoverflow.com/a/55795189/9854554

标签: android push-notification


【解决方案1】:

是的,@NareshNK。您的 cmets 帮助我找到了确切的解决方案。 问题是,

  • 我正在使用 firebase 通知有效负载发送通知,我尝试读取的内容也是如此。

当我开始发送带有数据 json 的消息时。我开始在数据负载中收到响应,并且我的问题已解决。

Official FCM Documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2012-03-29
    • 2013-08-03
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多