gpt4 book ai didi

ios - CloudKit 不发送更新通知

转载 作者:可可西里 更新时间:2023-11-01 01:29:03 24 4
gpt4 key购买 nike

我正在尝试开发一个简单的消息传递应用程序来学习 CloudKit 的基础知识。

我几乎弄明白了,只是我无法接收记录更新事件的通知。

为了测试应用程序,我在设备和模拟器上同时运行它。

两个实例都登录到同一个 iCloud 帐户(还没有抽出时间创建专门的帐户进行测试...);然而,该应用使用 UUId 将本地用户与远程用户区分开来,因此这不是问题。

当应用程序的一个实例想要发送消息时,它会创建一条记录并将其保存到 CloudKit。

我知道模拟器不支持 APNs,但如果我从模拟器发送消息,我可以在设备上收到通知。

这有效。


接下来,我想将这些消息标记为“已读”:也就是说,当它们首次显示在不是创作它们的设备上时标记它们。

我通过获取相应的记录、修改它并保存它来完成此操作。因此,我从设备发送的消息显示在模拟器上,并在那里标记为“已读”。我将更改与 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!)")
})
})

但是,在另一端,永远不会收到“更新”通知。

是否因为两个实例都以同一用户身份登录到 iCloud?我觉得这很难相信,因为“创建”通知可以毫无问题地传送。

两个通知(“消息创建”和“消息更新”)的订阅已成功注册并显示在 CloudKit 仪表板上(触发 INSERTUPDATE)。


更新:经过很长时间思考我的“创建”订阅和我的“更新”订阅之间可能只有一个触发通知的不同之处,我意识到只有“创建”订阅有一个通知主体:

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

...而“更新”订阅没有:

    let notificationInfo = CKNotificationInfo()
subscription.notificationInfo = notificationInfo

添加警报正文后,“更新”通知开始到达。

但是,这只是一种变通方法:我的阅读更新需要静默通知。仅仅为了提醒用户他的消息已在另一端阅读而显示横幅是没有意义的。此外,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.

(强调我的)

我仍然看不出这个“不会导致系统提醒用户的低优先级”是如何等同于“application(:didReceiveRemoteNotification:fetchCompletionHandler:) not being called完全”。

最佳答案

解决方案似乎是使用 shouldSendContentAvailable = true 的信息对象,如下所示:

let info = CKNotificationInfo()
info.shouldSendContentAvailable = true
subscription.notificationInfo = info

这已经为我解决了。它会导致 didReceiveRemoteNotification: 触发,而不会出现任何用户可见的通知。所以这是一个无声的通知,如所期望的那样,并且它确实如所期望的那样到达。

如果我将 subscription.notificationInfo 设为 nil,则应用程序永远不会收到更改通知。但是有了一个 [有效沉默] 信息对象,我得到了想要的结果。

关于ios - CloudKit 不发送更新通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39912403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com