CloudKit通知 [英] CloudKit notifications

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

问题描述

我可能会遗漏一些明显的东西。

I might be missing something obvious here.

如何创建一个CloudKit通知,其中包含有关您收到通知的CKRecord的详细信息? (即不仅仅是一个通用的New Item Created!,而是一个带有记录标题的通知,例如Jill和Jessica的生日派对!)

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"]

你有新闻:%1 $ @的地方Localizable.strings文件中的一个键

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天全站免登陆