CloudKit 不发送更新通知
CloudKit Not Sending Update Notifications
我正在尝试开发一个简单的消息传递应用程序来学习 CloudKit 的基础知识。
我差不多明白了,只是我无法接收记录更新事件的通知。
为了测试该应用程序,我 运行 它同时在设备和模拟器上进行。
两个实例都登录到同一个 iCloud 帐户(还没有抽出时间创建专用帐户进行测试...);但是应用程序使用 UUIds 区分本地用户和远程用户,所以这不是问题。
当应用程序的一个实例想要发送一条消息时,它会创建一条记录并将其保存到 CloudKit。
我知道模拟器不支持 APNs,但如果我从模拟器发送消息,我可以在设备上收到通知。
有效。
下一个,我想将这些消息标记为 "read":也就是说,当它们第一次显示在非作者设备上时标记它们他们。
我通过获取相应的记录、修改它并保存它来完成此操作。因此,我从设备发送的消息显示在模拟器上,并在那里标记为 'read'。我将更改与 cloudKit 同步并且 希望设备收到通知:
publicDatabase.perform(query, inZoneWith: nil, completionHandler: {(records, error) in
guard let records = records, records.count > 0 else {
return print("Error - Message: \(message.text) by \(message.userName) NOT found on the cloud!")
}
let record = records[0]
// HERE THE RECORD IS MODIFIED LOCALLY:
record["readTimestamp"] = (message.readTimestamp! as NSDate)
// Now, save it:
self.publicDatabase.save(record, completionHandler: {record, error in
guard error == nil else {
return print("Error - Message: \(message.text) by \(message.userName) could NOT be saved as read!")
}
print("Message: \(message.text) by \(message.userName) SAVED as read at \(message.readTimestamp!)")
})
})
然而,在另一端,从未收到'Update'通知。
是否因为两个实例都以同一用户身份登录到 iCloud?我觉得这很难相信,因为 "Create" 通知传送没有问题。
两个通知("Message Create" 和 "Message Update")的订阅已成功注册并显示在 CloudKit 仪表板上(触发 INSERT
和 UPDATE
) .
更新: 经过很长一段时间思考我的 "create" 订阅和我的 "update" 订阅之间可能有什么不同,这可能只会导致其中之一触发通知,我意识到只有 "create" 订阅有一个通知主体:
let subscription = CKQuerySubscription(recordType: "Message",
predicate: NSPredicate(value: true),
subscriptionID: Bundle.main.bundleIdentifier! + ".subscription.message.create",
options: .firesOnRecordCreation
)
let notificationInfo = CKNotificationInfo()
// THIS LINE MAKES ALL THE DIFFERENCE:
notificationInfo.alertBody = "You've Got Mail!"
subscription.notificationInfo = notificationInfo
publicDatabase.save(subscription, completionHandler: {savedSubscription, error in
...而 "update" 订阅没有:
let notificationInfo = CKNotificationInfo()
subscription.notificationInfo = notificationInfo
添加警报正文后,"update" 通知开始到达。
但是,这只是一种解决方法:我需要 静默 通知我的阅读更新。仅仅为了提醒用户他的消息已在另一端阅读而显示横幅是没有意义的。
另外,CloudKit 编程指南没有提到这个限制。
有没有办法订阅静默(即空的)推送通知?
更新 2: 实际上,我在 API Reference 上找到了 CKNotificationInfo
:
Note
If you don’t set any of the alertBody
, soundName
, or shouldBadge
properties, the push notification is sent at a lower priority that
doesn’t cause the system to alert the user.
(强调我的)
我仍然看不出这个 "lower priority that doesn’t cause the system to alert the user" 是如何等同于“application(:didReceiveRemoteNotification:fetchCompletionHandler:)
根本没有被调用”。
解决方案似乎是将信息对象与 shouldSendContentAvailable = true
一起使用,如下所示:
let info = CKNotificationInfo()
info.shouldSendContentAvailable = true
subscription.notificationInfo = info
这已经为我解决了。它会导致 didReceiveRemoteNotification:
触发,而不会出现任何用户可见的通知。所以这是一个无声的通知,如所期望的那样,并且它确实如所期望的那样到达。
如果我离开 subscription.notificationInfo
nil,应用程序永远不会收到更改通知。但是有了一个 [有效沉默] 信息对象,我得到了想要的结果。
我正在尝试开发一个简单的消息传递应用程序来学习 CloudKit 的基础知识。
我差不多明白了,只是我无法接收记录更新事件的通知。
为了测试该应用程序,我 运行 它同时在设备和模拟器上进行。
两个实例都登录到同一个 iCloud 帐户(还没有抽出时间创建专用帐户进行测试...);但是应用程序使用 UUIds 区分本地用户和远程用户,所以这不是问题。
当应用程序的一个实例想要发送一条消息时,它会创建一条记录并将其保存到 CloudKit。
我知道模拟器不支持 APNs,但如果我从模拟器发送消息,我可以在设备上收到通知。
有效。
下一个,我想将这些消息标记为 "read":也就是说,当它们第一次显示在非作者设备上时标记它们他们。
我通过获取相应的记录、修改它并保存它来完成此操作。因此,我从设备发送的消息显示在模拟器上,并在那里标记为 'read'。我将更改与 cloudKit 同步并且 希望设备收到通知:
publicDatabase.perform(query, inZoneWith: nil, completionHandler: {(records, error) in
guard let records = records, records.count > 0 else {
return print("Error - Message: \(message.text) by \(message.userName) NOT found on the cloud!")
}
let record = records[0]
// HERE THE RECORD IS MODIFIED LOCALLY:
record["readTimestamp"] = (message.readTimestamp! as NSDate)
// Now, save it:
self.publicDatabase.save(record, completionHandler: {record, error in
guard error == nil else {
return print("Error - Message: \(message.text) by \(message.userName) could NOT be saved as read!")
}
print("Message: \(message.text) by \(message.userName) SAVED as read at \(message.readTimestamp!)")
})
})
然而,在另一端,从未收到'Update'通知。
是否因为两个实例都以同一用户身份登录到 iCloud?我觉得这很难相信,因为 "Create" 通知传送没有问题。
两个通知("Message Create" 和 "Message Update")的订阅已成功注册并显示在 CloudKit 仪表板上(触发 INSERT
和 UPDATE
) .
更新: 经过很长一段时间思考我的 "create" 订阅和我的 "update" 订阅之间可能有什么不同,这可能只会导致其中之一触发通知,我意识到只有 "create" 订阅有一个通知主体:
let subscription = CKQuerySubscription(recordType: "Message",
predicate: NSPredicate(value: true),
subscriptionID: Bundle.main.bundleIdentifier! + ".subscription.message.create",
options: .firesOnRecordCreation
)
let notificationInfo = CKNotificationInfo()
// THIS LINE MAKES ALL THE DIFFERENCE:
notificationInfo.alertBody = "You've Got Mail!"
subscription.notificationInfo = notificationInfo
publicDatabase.save(subscription, completionHandler: {savedSubscription, error in
...而 "update" 订阅没有:
let notificationInfo = CKNotificationInfo()
subscription.notificationInfo = notificationInfo
添加警报正文后,"update" 通知开始到达。
但是,这只是一种解决方法:我需要 静默 通知我的阅读更新。仅仅为了提醒用户他的消息已在另一端阅读而显示横幅是没有意义的。 另外,CloudKit 编程指南没有提到这个限制。
有没有办法订阅静默(即空的)推送通知?
更新 2: 实际上,我在 API Reference 上找到了 CKNotificationInfo
:
Note
If you don’t set any of the
alertBody
,soundName
, orshouldBadge
properties, the push notification is sent at a lower priority that doesn’t cause the system to alert the user.
(强调我的)
我仍然看不出这个 "lower priority that doesn’t cause the system to alert the user" 是如何等同于“application(:didReceiveRemoteNotification:fetchCompletionHandler:)
根本没有被调用”。
解决方案似乎是将信息对象与 shouldSendContentAvailable = true
一起使用,如下所示:
let info = CKNotificationInfo()
info.shouldSendContentAvailable = true
subscription.notificationInfo = info
这已经为我解决了。它会导致 didReceiveRemoteNotification:
触发,而不会出现任何用户可见的通知。所以这是一个无声的通知,如所期望的那样,并且它确实如所期望的那样到达。
如果我离开 subscription.notificationInfo
nil,应用程序永远不会收到更改通知。但是有了一个 [有效沉默] 信息对象,我得到了想要的结果。