如何在iOS 10消息扩展中使用CKSubscription?

San*_*aus 6 ios swift cloudkit ios10 ios-messages-extension

iOS 10引入了消息扩展,这是第一个(据我所知)扩展,不需要主机应用程序.我正在尝试在没有主机应用程序的邮件扩展中使用CloudKit.

据我所知,CKSubscription依赖于推送通知.但是,我无法UIApplication在应用扩展程序中以通常的方式(通过)注册推送通知:

let app = UIApplication.shared // Error: not available here blah blah blah
Run Code Online (Sandbox Code Playgroud)

这意味着CKSubscription在消息应用程序中似乎无法接收通知.我确实在新的中找到了希望UserNotifications.framework,但它没有提供任何注册远程通知的机制.我试过了:

override func willBecomeActive(with conversation: MSConversation) {

    // ...

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.badge, .alert]) { success, error in
        if error != nil { fatalError() }
    }
    center.delegate = self

    // ...
}

// MARK: UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    fatalError() // This never gets called :(
}
Run Code Online (Sandbox Code Playgroud)

但是,当我更新作为我的主题的记录时CKSubscription,不会向用户显示通知,也不会通知代理.

这是我的CKSubscription代码:

let sub = CKRecordZoneSubscription(zoneID: legitimateZone)

let notifInfo = CKNotificationInfo()
notifInfo.alertBody = "Wow it works! Amazing!"
sub.notificationInfo = notifInfo

privateDB.save(sub) { sub, error in
    if let e = error {
        fatalError("Error saving zone sub: \(e)")
    }
    print("Saved subscription")
}
Run Code Online (Sandbox Code Playgroud)

如何CKSubscription在邮件扩展中收到通知?


我甚至不需要向用户提供通知,也不需要在后台接收它们.我想要的只是知道在我的扩展程序运行时何时更新记录.

如果还有其他方式可以做到这一点,除了CKSubscription我也喜欢听到(只要它不会不断地轮询CloudKit,浪费我宝贵的40个请求/秒).

我已尝试过物理设备和模拟器.