Swift 2 和 Cloudkit 的通知
Posted
技术标签:
【中文标题】Swift 2 和 Cloudkit 的通知【英文标题】:Notifications with Swift 2 and Cloudkit 【发布时间】:2016-03-18 23:49:10 【问题描述】:我正在制作一个“短信应用”,你可以调用它,它使用 cloudkit,我一直在到处寻找添加与 cloudkit 一起使用的通知...有人能告诉我为 cloudkit 添加推送通知的代码吗详细地说,因为我很迷茫......而且我不想让通知去不同的“短信室”(在cloudkit中它会是记录类型......)例如,我有一个名为“文本”的记录类型和另一个一个名为“text 2”的通知我不希望来自“text”的通知发送给使用“text2”的人,反之亦然。
【问题讨论】:
看CKSubscription。 请阅读这个答案:***.com/a/38171514/2725435 【参考方案1】:将 Swift 2.0 与 El Captain 和 Xcode 7.2.1 一起使用
Elia,您需要将此添加到您的应用委托中。它将以 userInfo 数据包的形式到达,然后您可以对其进行解析以查看哪个数据库/应用程序发送了它。
UIApplicationDelegate to the class
application.registerForRemoteNotifications() to the
func application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
比这个方法
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
let notification = CKQueryNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
let container = CKContainer(identifier: "iCloud.com")
let publicDB = container.publicCloudDatabase
if notification.notificationType == .Query
let queryNotification = notification as! CKQueryNotification
if queryNotification.queryNotificationReason == .RecordUpdated
print("queryNotification.recordID \(queryNotification.recordID)")
// Your notification
print("userInfo \(userInfo["ck"])")
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: self, userInfo:dataDict)
这会让你开始。
您可以使用此方法以编程方式检查您的订阅,当然,在您开发时,您可以使用仪表板。
func fetchSubsInPlace()
let container = CKContainer(identifier: "iCloud.com")
let publicDB = container.publicCloudDatabase
publicDB.fetchAllSubscriptionsWithCompletionHandler(subscriptions, error in
for subscriptionObject in subscriptions!
let subscription: CKSubscription = subscriptionObject as CKSubscription
print("subscription \(subscription)")
)
最后当你得到它时;您可以执行此例程,以确保在应用处于休眠状态时捕获您错过的任何订阅,并确保订阅不会发送到您的所有设备,一旦您也处理它们。
func fetchNotificationChanges()
let operation = CKFetchNotificationChangesOperation(previousServerChangeToken: nil)
var notificationIDsToMarkRead = [CKNotificationID]()
operation.notificationChangedBlock = (notification: CKNotification) -> Void in
// Process each notification received
if notification.notificationType == .Query
let queryNotification = notification as! CKQueryNotification
let reason = queryNotification.queryNotificationReason
let recordID = queryNotification.recordID
print("reason \(reason)")
print("recordID \(recordID)")
// Do your process here depending on the reason of the change
// Add the notification id to the array of processed notifications to mark them as read
notificationIDsToMarkRead.append(queryNotification.notificationID!)
operation.fetchNotificationChangesCompletionBlock = (serverChangeToken: CKServerChangeToken?, operationError: NSError?) -> Void in
guard operationError == nil else
// Handle the error here
return
// Mark the notifications as read to avoid processing them again
let markOperation = CKMarkNotificationsReadOperation(notificationIDsToMarkRead: notificationIDsToMarkRead)
markOperation.markNotificationsReadCompletionBlock = (notificationIDsMarkedRead: [CKNotificationID]?, operationError: NSError?) -> Void in
guard operationError == nil else
// Handle the error here
return
let operationQueue = NSOperationQueue()
operationQueue.addOperation(markOperation)
let operationQueue = NSOperationQueue()
operationQueue.addOperation(operation)
【讨论】:
非常感谢您的帮助我非常感谢它帮助了很多。最好的问候,伊利亚 真的可以获取我的应用从未访问过的所有通知吗?真的吗?我需要测试它。如果是这样,Apple 的Cloud Kit
真的很棒;)
是的,我相信是的。以上是关于Swift 2 和 Cloudkit 的通知的主要内容,如果未能解决你的问题,请参考以下文章
Swift CloudKit CoreData 远程更改通知也会触发本地更改
swift Swift - CloudKit - 为特定用户设置订阅通知
swift Swift - CloudKit - 为远程通知配置应用程序
iOS 10 - Swift 仍然通知 CloudKit:应用程序未启动,在 didFinishLaunchingWithOptions 之后读取通知