CloudKit 通知 [英] CloudKit notifications

查看:23
本文介绍了CloudKit 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能在这里遗漏了一些明显的东西.

I might be missing something obvious here.

如何创建 CloudKit 通知,其中包含有关您收到通知的 CKRecord 的详细信息?(即不仅仅是一个通用的新项目创建!"而是带有记录标题的通知,例如吉尔和杰西卡的生日派对!")

How would one create a CloudKit notification with detail about the CKRecord that you are being notified about? (i.e. not just a generic "New Item Created!" but a notification with the title of the record for instance, like, "Jill and Jessica's Birthday Party!")

为每个新记录更改创建一个带有新notification.alertBody 的新订阅是正确的方法吗?这没有任何意义.

Is the proper way to do this to create a new subscription with a new notification.alertBody for every new record change? That doesn't make any sense.

剩下的选项 queryNotification.recordID 可以从 CloudKit(或我的本地存储)中提取数据并将其添加到通知中?添加或更改已通知通知的值的方法是什么?我错过了什么?

That leaves the option queryNotification.recordID to pull the data out of CloudKit (or my local store) and add it to the notification somehow? What's the method for adding or changing the values of an already notified notification? What am I missing?

更新:根据 Edwin 的建议,这里有一个代码片段,但它不起作用.

Updated: Per Edwin's suggestion here's a code snippet, but it doesn't work.

let notification = CKNotificationInfo()
notification.alertBody = "Added: %@" as NSString
notification.alertLocalizationArgs = ["name"]

推荐答案

只需填写 CKNotificationInfo 对象的 .alertLocalizationArgs

Just fill the .alertLocalizationArgs of the CKNotificationInfo object

.alertLocalizationArgs 的文档说:

The documentation for the .alertLocalizationArgs says:

此属性的使用是可选的.该属性包含一个数组NSString 对象,每个对象对应记录的一个字段触发了推送通知.这些名称用于检索记录中的相应值.然后将这些值用于替换 alertBody 或alertLocalizationKey 字符串.值本身必须是 NSString,NSNumber 或 NSDate 对象.不要指定具有其他值的键.长度超过 100 个字符的字符串值可能是添加到推送通知时被截断.

Use of this property is optional. This property contains an array of NSString objects, each of which corresponds to a field of the record that triggered the push notification. Those names are used to retrieve the corresponding values from the record. The values are then used to replace any substitution variables in either the alertBody or alertLocalizationKey strings. The values themselves must be NSString, NSNumber, or NSDate objects. Do not specify keys with other values. String values that are greater than 100 characters in length may be truncated when added to the push notification.

如果您使用 %@ 作为替换变量,则这些变量是替换为按顺序遍历数组.如果使用变量%n$@形式,其中n为整数,n表示索引(从1) 要使用的数组中的项.因此,数组中的第一项替换变量 %1$@,第二项替换变量%2$@,等等.您可以使用索引替换变量来更改结果字符串中项目的顺序,这可能是必要的当您本地化应用的消息时.

If you use %@ for your substitution variables, those variables are replaced by walking the array in order. If you use variables of the form %n$@, where n is an integer, n represents the index (starting at 1) of the item in the array to use. Thus, the first item in the array replaces the variable %1$@, the second item replaces the variable %2$@, and so on. You can use indexed substitution variables to change the order of items in the resulting string, which might be necessary when you localize your app’s messages.

以下是我如何使用它的 2 个示例:

Here are 2 samples of how I use it:

            notificationInfo.alertBody = "%1$@ : %2$@"
            notificationInfo.alertLocalizationArgs = ["FromName", "Text"]

第二个样本:

            notificationInfo.alertLocalizationKey = "News: %1$@"
            notificationInfo.alertLocalizationArgs = ["Subject"]

您在 Localizable.strings 文件中将新闻:%1$@"作为键的位置

Where you have the "News: %1$@" as a key in the Localizable.strings file

更新:看起来您现在需要使用 .alertLocalizationKey.所以第一个样本不再起作用.

Update: It looks like you now require to use the .alertLocalizationKey. So the first sample does not work anymore.

这篇关于CloudKit 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆