如何更新 CloudKit 订阅
How to update CloudKit Subscriptions
我正在尝试使用 iOS、Swift 和 CloudKit 制作消息传递应用程序。我已经成功做到了,每当有人添加另一条消息时,我都会发送推送通知。但是,我希望推送通知的文本能够说明消息中的内容。这是我尝试过的:
发送通知的默认代码:
let subscription = CKSubscription(recordType: "Responses", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil), options: .FiresOnRecordCreation)
let notification = CKNotificationInfo()
notification.alertBody = "New Message Sent"
notification.soundName = UILocalNotificationDefaultSoundName
subscription.notificationInfo = notification
CKContainer.defaultContainer().publicCloudDatabase.saveSubscription(subscription) { (result, error) -> Void in
if error != nil {
print(error!.localizedDescription)
}
}
这让我可以为每条显示 "New Message Sent" 的消息发送通知。这是我为指定通知尝试过的方法:
//publicDB is the CKContainer.defaultContainer.publicDatabase
publicDB.fetchAllSubscriptionsWithCompletionHandler({ (subscriptions, error) in
if error != nil {
self.displayError(self.getStringFromError(error!))
} else {
for subscription in subscriptions! {
subscription.notificationInfo?.alertBody = self.responseTextField.text
}
}
})
let record = CKRecord(recordType: "Responses")
record.setObject(currentUsername, forKey: "respondedBy")
record.setObject(responseTextField.text, forKey: "response")
publicDB.saveRecord(record) { (record, error) in
//code for updating tables, etc.
//Notification on other device sent
}
设置订阅的 alertBody
(我在上面尝试做的)不起作用。是否有任何其他解决方案来更新订阅的 alertBody?谢谢!
无法更新订阅。您必须先删除它,然后重新创建它。
但是,为什么要更改 alertBody?您最初可以将其设置为显示记录的响应字段。为此你应该使用这样的东西:
notificationInfo?.alertLocalizationKey = "Response: %1$@"
notificationInfo?.alertLocalizationArgs = ["response"]
那么您的 Localization.Strings 文件中也需要这个。
"Response: %1$@" = "Response: %1$@";
在您的情况下,您希望在每次创建新消息时更改订阅。您正在将通知消息设置为与记录的字段之一相同。相反,您可以让 CloudKit 自动发送该字段。您只需创建订阅一次。在
上方的 2 行代码中
alertLocalizationKey 是在 Localization.Strings 文件中查找的键,而 alertLocalizationArgs 是将用于替换参数的 CloudKit 字段。
有关本地化的更多信息,请参阅 http://nshipster.com/nslocalizedstring/
我正在尝试使用 iOS、Swift 和 CloudKit 制作消息传递应用程序。我已经成功做到了,每当有人添加另一条消息时,我都会发送推送通知。但是,我希望推送通知的文本能够说明消息中的内容。这是我尝试过的:
发送通知的默认代码:
let subscription = CKSubscription(recordType: "Responses", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil), options: .FiresOnRecordCreation)
let notification = CKNotificationInfo()
notification.alertBody = "New Message Sent"
notification.soundName = UILocalNotificationDefaultSoundName
subscription.notificationInfo = notification
CKContainer.defaultContainer().publicCloudDatabase.saveSubscription(subscription) { (result, error) -> Void in
if error != nil {
print(error!.localizedDescription)
}
}
这让我可以为每条显示 "New Message Sent" 的消息发送通知。这是我为指定通知尝试过的方法:
//publicDB is the CKContainer.defaultContainer.publicDatabase
publicDB.fetchAllSubscriptionsWithCompletionHandler({ (subscriptions, error) in
if error != nil {
self.displayError(self.getStringFromError(error!))
} else {
for subscription in subscriptions! {
subscription.notificationInfo?.alertBody = self.responseTextField.text
}
}
})
let record = CKRecord(recordType: "Responses")
record.setObject(currentUsername, forKey: "respondedBy")
record.setObject(responseTextField.text, forKey: "response")
publicDB.saveRecord(record) { (record, error) in
//code for updating tables, etc.
//Notification on other device sent
}
设置订阅的 alertBody
(我在上面尝试做的)不起作用。是否有任何其他解决方案来更新订阅的 alertBody?谢谢!
无法更新订阅。您必须先删除它,然后重新创建它。
但是,为什么要更改 alertBody?您最初可以将其设置为显示记录的响应字段。为此你应该使用这样的东西:
notificationInfo?.alertLocalizationKey = "Response: %1$@"
notificationInfo?.alertLocalizationArgs = ["response"]
那么您的 Localization.Strings 文件中也需要这个。
"Response: %1$@" = "Response: %1$@";
在您的情况下,您希望在每次创建新消息时更改订阅。您正在将通知消息设置为与记录的字段之一相同。相反,您可以让 CloudKit 自动发送该字段。您只需创建订阅一次。在
上方的 2 行代码中alertLocalizationKey 是在 Localization.Strings 文件中查找的键,而 alertLocalizationArgs 是将用于替换参数的 CloudKit 字段。
有关本地化的更多信息,请参阅 http://nshipster.com/nslocalizedstring/