我用自己的服务器,使用FCM推送通知到ios设备,推送成功,我也使用Realm数据库系统帮我存储fcm推送通知,但是存储只会在以下两个成功案例.案例1:应用运行时.
I use my own server, use FCM to push notifications to the ios device, push notifications are successful, I also use the Realm database system to help me store the fcm push notifications, but the storage will only succeed in the following two cases. Case1: When the app is running.
案例2:当您点击推送横幅通知时,应用程序被杀死或在后台运行.
Case2: The app is killed, or running in the background, when you click to push the banner notification.
但这不是我的主要意图.我知道当应用被杀死时,我无法处理推送通知,所以我希望能够在用户打开应用时存储推送通知.
But this is not my main intention. I know that when the app is killed, I can't handle the push notifications, so I want to be able to store the push notifications when the user opens the app.
对不起,我的英语不好.如果有不清楚的地方,请告诉我.
Sorry, my English is not good. If there is something unclear, please let me know.
领域类
class Order: Object {
@objc dynamic var id = UUID().uuidString
@objc dynamic var name = ""
@objc dynamic var amount = ""
@objc dynamic var createDate = Date()
override static func primaryKey() -> String? {
return "id"
}
let realm = try! Realm()
let order: Order = Order()
AppDelegate.swift(应用运行时存储 fcm 消息)
AppDelegate.swift (when app runing store fcm message)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo //
print("userInfo: (userInfo)")
guard
let aps = userInfo[AnyHashable("aps")] as? NSDictionary,
let alert = aps["alert"] as? NSDictionary,
let body = alert["body"] as? String,
let title = alert["title"] as? String
else {
// handle any error here
return
}
print("Title: (title)
Body:(body)")
order.name = title
order.amount = body
try! realm.write {
realm.add(order)
}
completionHandler([.badge, .sound, .alert])
}
点击推送横幅通知AppDelegate.swift(当应用被杀或在后台点击推送横幅通知时)
click to push the banner notification AppDelegate.swift (when app killed or on backgroung click to push the banner notification)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("userInfo: (userInfo)")
guard
let aps = userInfo[AnyHashable("aps")] as? NSDictionary,
let alert = aps["alert"] as? NSDictionary,
let body = alert["body"] as? String,
let title = alert["title"] as? String
else {
// handle any error here
return
}
print("Title: (title)
Body:(body)")
order.name = title
order.amount = body
try! realm.write {
realm.add(order)
}
completionHandler()
}
请有经验的人帮帮我,非常感谢.
Please have experienced people to help me, thank you very much.
您可以在应用被终止时将通知存储到数据库中.
You can store notifications into your database when your app is killed.
使用App Group"访问数据库,
To access database using "App Group" use,
var fileMgr : FileManager!
let databasePathURL = fileMgr!.containerURL(forSecurityApplicationGroupIdentifier: "<APP_GROUPS_ID>")
从上面的代码中,一旦你得到数据库的路径,你就可以对数据库执行插入操作.这将是您正在执行的正常插入操作.
From above code once you get the path for database, you can perform your insert operation into database. It would be your normal insert operation that you are performing.
在此文件和下图中突出显示的方法中插入用于保存数据的代码.
Insert code for saving data in this file and in the method highlighted in below image.
为了将您的文件访问到扩展,请按照下图进行操作.
For accessing your files into Extention follow the below image.
如果您需要任何其他帮助,请告诉我.
If you need any other help, Do let me know.
这篇关于使用swift杀死应用程序时如何存储ios推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!