【问题标题】:How to show data from a CKRecord in alertBody of a Remote Notification?如何在远程通知的 alertBody 中显示来自 CKRecord 的数据?
【发布时间】:2017-10-28 16:29:27
【问题描述】:

在我的应用程序中,我有一个公共 CloudKit 数据库。我正在使用它进行远程订阅。

我在 CloudKit 仪表板中创建了一个 GlobalNotification 记录,为此记录创建了 String 类型的 content 字段,并在我的 AppDelegate 中实现了以下方法以使通知工作:

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let subscription = CKQuerySubscription(recordType: "GlobalNotification", predicate: NSPredicate(format: "TRUEPREDICATE"), options: .firesOnRecordCreation)

        let info = CKNotificationInfo()
        info.alertBody = "Some Text" // here i want to show content String data of a CKRecord which fired a notification
        info.shouldBadge = true
        info.soundName = "default"


        subscription.notificationInfo = info

        CKContainer.default().publicCloudDatabase.save(subscription, completionHandler: { subscription, error in
            if error == nil {
                // Subscription saved successfully
            } else {
                // An error occurred
            }
        })
    }

之后,我在 cloudKit 仪表板中创建一个新的 GlobalNotification 记录,并在 CloudKit 仪表板中的该记录的 content 字段中添加一些“CloudKit 内容文本”。 当我的设备上触发通知时,我在它的 alertBody 中看到“一些文本”文本,但我想看到“CloudKit 内容文本”。

我希望此通知显示取自 GlobalNotification Record 的 content 字段的字符串。我应该如何更改我的代码来实现这一点?

我已阅读有关 desiredKeys 并尝试添加 info.desiredKeys = ["content"] 但没有帮助。我也用谷歌搜索过,但没有找到解决方案。

【问题讨论】:

    标签: ios swift notifications cloudkit ckrecord


    【解决方案1】:

    您不能仅使用警报正文来做到这一点。

    您需要设置三个组件才能完成这项工作。您需要使用包含可替换参数的消息定义一个字符串。然后你必须告诉你的通知使用那个字符串。最后,您告诉通知将哪些记录字段替换为字符串。

    首先,您需要在 localizable.strings 文件中添加一个键/字符串对。在字符串中,您可以使用 %n$@ 指定要替换参数的位置,其中每个附加参数的“n”递增。

    其次,在您的通知中,您需要将 .alertLocalizationKey 设置为您在上面的字符串文件中指定的键名。

    第三,在您的通知中,将 .alertLocalizaionArgs 属性设置为记录中的字段名称数组。

    此 Apple 页面提供了如何使用可替换参数构造字符串的示例:https://developer.apple.com/documentation/cloudkit/cknotificationinfo/1515182-alertlocalizationargs

    这个问题还显示了一些示例代码:(示例 1 不再起作用。按照示例 2)CloudKit notifications

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 2018-04-15
      • 2017-11-06
      相关资源
      最近更新 更多