问题描述
在我的应用程序中,当收到新的推送通知时,我想向Realm数据库中插入一些数据,但是问题是如果应用程序位于后台数据中而不是写入数据库中,但是如果用户在通知栏中触摸通知,则一切正常!
以下是我用于将数据插入Realm DB的代码:
let realm = try! Realm()
try! realm.write() {
let notif = NotificationDBModel()
notif.id = id
if let title = data["title"] {
notif.title = title as! String
}
if let content = data["content"] {
notif.content = content as! String
}
if let date = data["date"] {
notif.date = date as! String
}
realm.add(notif)
}
解决方法
Realm插入代码看起来不错,请检查是否在Background modes
中启用了signing & Capabilities