存储从"Firebase Push Notification"接收到的所有消息.在移动数据库/本地 [英] Store all messages receiving from "Firebase Push Notification" in mobile db/locally

查看:166
本文介绍了存储从"Firebase Push Notification"接收到的所有消息.在移动数据库/本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UserDefaults 将来自Firebase推送通知的所有消息存储在移动数据库中.但它仅存储最新消息.但是我希望数据库存储所有接收消息.请帮我解决这个问题.我坚持这一点.

I am trying to store all the messages coming fro firebase push notification in mobile db using UserDefaults. But it store only latest message. But I want the db to store all the receiving messages. Please, help me to solve this. I am stuck with this.

 let notificationBody = userInfo[AnyHashable("text")]! as! String
 UserDefaults.standard.set(notificationBody, forKey: "notifications") 

----仅打印最后的通知-----

---- This prints only last notification-----

推荐答案

每次使用此代码存储通知时,它都会覆盖当前值.这就是为什么只有最新消息的原因.您可以创建一个通知数组,并将其存储在userDefaults中.那么每次收到新通知时,您都可以将新通知追加到UserDefaults中的当前通知数组中

Every time you store the notification using this code it overrides the current value.That is why you got the latest message only.You can create a array of notification and store it in userDefaults. then every time you got new notification you can append the new notification to current array of notifications in UserDefaults

首先,您创建一个用于通知的可编码类

first you create a codable class for notification

class FNotifications: Codable {
  let fnotifications:[FNotification]

    init(fnotifications:[FNotification]) {
        self.fnotifications = fnotifications
    }
}

class FNotification: Codable {

    let id:String
    let type:Int
    let header:String
    let description:String
    let date:String
    let badge:Int
    var status: UInt8

    init(id:String,type:Int,header:String,description:String,date:String,badge:Int,status:UInt8, recordID:String) {
        self.id = id
        self.type = type
        self.header = header
        self.description = description
        self.date = date
        self.badge = badge
        self.status = status
    }
}

然后您可以创建一个FNotification数组并将其存储到userDefaults中(只有第一次要创建数组,第二次可以获取保存的数组并向其添加新的通知)

then you can create a array of FNotification and store it to userDefaults (only first time you want to create array.second time you can get saved array and append new notification to it)

//define notification array when its first time
var notification: [FNotification] = []

//add new notification to array
notification.append(FNotification(id: "" ,type: "",header: "", description: "", date:"" , badge: 1, status: 1))

do{
//save to user defaults
UserDefaults.standard.set(try? PropertyListEncoder().encode(notification), forKey: "notifications")
UserDefaults.standard.synchronize()

}catch{
    print(error)
}

使用这种方式保存通知后,您可以像这样获取通知

After save notification using this way, you can get your notification like this

if let notiArray =  UserDefaults.standard.object(forKey: "notifications") as? Data {
        if notiArray.count != 0 {
            notification = try! PropertyListDecoder().decode(Array<FNotification>.self, from: notiArray)
            print("notification \(notification)")
        }
 }

这篇关于存储从"Firebase Push Notification"接收到的所有消息.在移动数据库/本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆