如何防止在屏幕上通知用户有关 CloudKit 的新更改?
How to prevent from notifying user on the screen about new change in CloudKit?
这是我准备订阅的方式:
class func saveSubscriptions() {
let options: CKSubscriptionOptions = [.firesOnRecordCreation, .firesOnRecordUpdate, .firesOnRecordDeletion]
let serviceSubscription = CKSubscription(recordType: "Service", predicate: NSPredicate(value: true), options: options)
let notificationInfo = CKNotificationInfo()
notificationInfo.alertLocalizationKey = "s"
notificationInfo.shouldBadge = false
notificationInfo.shouldSendContentAvailable = false
serviceSubscription.notificationInfo = notificationInfo
CloudContainer.publicCloudDatabase.save(serviceSubscription) { _, _ in }
}
通知会永久显示在屏幕上。
我真的不想在这里。有没有办法在应用程序处于活动状态时发送通知,但在应用程序未 运行 或处于前台模式时不显示任何内容?
您需要正确设置通知信息。不要设置 alertLocalizationKey
。 shouldSendContentAvailable
应设置为 true
。
试试这个代码,这不会显示通知横幅
let notificationInfo = CKNotificationInfo()
notificationInfo.shouldBadge = false
notificationInfo.shouldSendContentAvailable = true
subscription.notificationInfo = notificationInfo
这是我准备订阅的方式:
class func saveSubscriptions() {
let options: CKSubscriptionOptions = [.firesOnRecordCreation, .firesOnRecordUpdate, .firesOnRecordDeletion]
let serviceSubscription = CKSubscription(recordType: "Service", predicate: NSPredicate(value: true), options: options)
let notificationInfo = CKNotificationInfo()
notificationInfo.alertLocalizationKey = "s"
notificationInfo.shouldBadge = false
notificationInfo.shouldSendContentAvailable = false
serviceSubscription.notificationInfo = notificationInfo
CloudContainer.publicCloudDatabase.save(serviceSubscription) { _, _ in }
}
通知会永久显示在屏幕上。
我真的不想在这里。有没有办法在应用程序处于活动状态时发送通知,但在应用程序未 运行 或处于前台模式时不显示任何内容?
您需要正确设置通知信息。不要设置 alertLocalizationKey
。 shouldSendContentAvailable
应设置为 true
。
试试这个代码,这不会显示通知横幅
let notificationInfo = CKNotificationInfo()
notificationInfo.shouldBadge = false
notificationInfo.shouldSendContentAvailable = true
subscription.notificationInfo = notificationInfo