ios - 应用程序终止或终止时如何在 coredata 中存储推送通知?
问题描述
当应用程序终止或终止时,我试图将推送通知(FCM)存储在coredata中。它不工作。但是当应用程序处于后台状态时存储完美。当我单击通知时,它也完美存储。但我不能存储在coredata被杀状态。
我正在使用FCM通知
- 当应用程序被杀死状态时,iOS中可能的通知存储核心数据吗?
- 应用程序被杀死时存储核心数据的任何解决方案。
解决方案
您可以使用UNNotificationServiceExtension
. 这旨在用作拦截器,以在呈现给用户之前修改传入通知的内容。但是,您可以使用此拦截器将通知保存到 CoreData。请务必在通知数据有效负载上设置mutable-content
为。1
只有这些会被拦截。
FCM 支持这一点mutable-content
,我在我的一个项目中做了一些非常相似的事情,以确认我的后端用户收到通知并且它完美地工作。
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = request.content.mutableCopy()
// TODO
// Save the notification to core data
contentHandler(request.content)
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler,
let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
推荐阅读
- javascript - 使用键盘操作整数
- jenkins - Android10 平台:调用 Aidl 库 API 时出现 NoClassDefFoundError
- types - 尽管有模式匹配,但 Agda 并未消除目标中的子句
- python - 如何仅从时间戳中获取时间?
- javascript - 我该如何循环这个 JSON 数据
- javascript - 在 JavaScript 中为数组中的每个对象添加成功率
- android - 更改 TextView 中可绘制图标的位置
- javascript - 我正在尝试使用 jquery 设置表单变量的操作。它正确设置了动作,但在动作页面上没有任何内容
- ios - 归档“Command PhaseScriptExecution failed with a nonzero exit code”时出现颤振错误
- parsing - ANTLR4 解析器丢弃剩余的标记