[英]Silent iCloud changed notifications not received in background
My app uses a public iCloud database that is synchronised using push notifications. 我的应用程序使用公共iCloud数据库,该数据库使用推送通知进行同步。
The subscription to iCloud notifications uses the following notificationInfo
: iCloud通知的预订使用以下
notificationInfo
:
let notificationInfo = CKNotificationInfo()
notificationInfo.alertBody = nil
notificationInfo.shouldSendContentAvailable = true
The test setup uses 2 iOS devices: 测试设置使用2个iOS设备:
1st test: 第一次测试:
2nd device: Executes my app in foreground. 第二台设备:在前台执行我的应用程序。
When the 1st device modifies the database, a notification is received in 当第一台设备修改数据库时,将在
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
This is as expected. 这是预期的。
2nd test: 第二次测试:
2nd device: As above, but now the screen is turned off, ie my app is in background. 第二台设备:如上所述,但是现在屏幕已关闭,即我的应用程序处于后台。
When the 1st device modifies the database, a notification is received. 当第一台设备修改数据库时,将收到通知。
Expected behaviour: 预期的行为:
Since shouldSendContentAvailable
is set to true
in the notification info, the system should wake my app (see docs ). 由于通知信息中的
shouldSendContentAvailable
设置为true
,因此系统应唤醒我的应用程序(请参阅docs )。 The app should then be given background execution time to download any data related to the push notification, such as the set of records that changed. 然后应该给应用程序后台执行时间,以下载与推送通知相关的任何数据,例如已更改的记录集。 This should be done by calling
这应该通过调用
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
Actual behaviour: 实际行为:
An alert is displayed in the lock screen, saying „iCloud changed, slide to open“. 锁定屏幕中会显示一条警报,提示“ iCloud已更改,请滑动以打开”。 If I swipe this alert and unlock the device, only then is this function actually called.
如果我滑动此警报并解锁设备,则只有该函数真正被调用。
My problem: 我的问题:
I want to use silence pushes from iCloud to update the local data of my app. 我想使用来自iCloud的静默推送来更新我的应用程序的本地数据。 So why is an alert with the default message body „iCloud changed“ shown on the lock screen, although I set
shouldSendContentAvailable = true
? 那么,尽管我设置了
shouldSendContentAvailable = true
为什么在锁定屏幕上仍显示带有默认消息正文“ iCloud shouldSendContentAvailable = true
?
My fault, although not obvious: 我的错,虽然不明显:
My CloudKit Dashboard shows that there is my subscription type. 我的CloudKit仪表板显示有我的订阅类型。 But for some reason, the dashboard does not show any subscription.
但是由于某种原因,仪表板不显示任何订阅。 I thus fetched all existing subscriptions using
因此,我使用获取了所有现有的订阅
private func getAllSubscriptionIDs(completion: @escaping (_ subscriptionIDs:[String]?, _ error: Error?) -> Void) {
let database = CKContainer.default().publicCloudDatabase
database.fetchAllSubscriptions { subscriptions, error in
guard error == nil else {
completion(nil, error)
return
}
guard let subscriptions = subscriptions else {
completion([], nil)
return
}
let subscriptionIDs = subscriptions.map{ $0.subscriptionID }
completion(subscriptionIDs, nil)
} // fetchAllSubscriptions
}
I got 5 existing subscriptions back, and one of them had an alert body " iCloud changed
" with shouldSendContentAvailable = false
. 我收回了5个现有的订阅,其中一个订阅主体具有
shouldSendContentAvailable = false
“ iCloud changed
”。
Obviously, I did use these subscriptions earlier during the development, and I did not delete them since they were not shown in the dashboard. 显然,我在开发过程中的早期就使用了这些订阅,并且由于它们没有显示在仪表板上,所以我没有删除它们。
Deleting them fixed the problem. 删除它们可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.