ios - 使用 Swift 2 和 Cloudkit 进行通知

标签 ios xcode swift notifications cloudkit

我正在制作一个“短信应用程序”,你可以称之为它,它使用 cloudkit,我一直在到处寻找添加与 cloudkit 一起使用的通知...有人能告诉我为 cloudkit 添加推送通知的代码吗详细地说,因为我很迷失...而且我不希望通知发送到不同的“短信室”(在cloudkit中它将是记录类型...)例如,我有一种称为“文本”的记录类型,另一种是一个名为“text 2”的通知我不希望来自“text”的通知发送给使用“text2”的人,反之亦然。

最佳答案

将 Swift 2.0 与 El Captain 和 Xcode 7.2.1 结合使用

Elia,您需要将其添加到您的应用程序委托(delegate)中。它将到达 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)")
        }
    })
}

最后当你得到它时;您可以执行此例程,以确保您捕获应用程序 sleep 时错过的任何订阅,并确保订阅不会发送到您的所有设备(一旦您也处理过这些设备)。

 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)
}


}

关于ios - 使用 Swift 2 和 Cloudkit 进行通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36096114/

相关文章:

ios - 在 iOS 中与服务器保持持续连接

ios - Xcode 导出本地化抛出错误 "Argument list too long"

xcode - PromiseKit 分支 promise

swift ARKit : Get face anchor transform relative to camera

ios - 如何检测 iOS 应用程序是新安装还是之前已安装并删除

ios - 如何让 xcode 为 iPhone 6 构建和运行 armv7 版本

ios - 强,弱或无主引用周期与定时器

ios - 我需要禁用用户与键盘或整个应用程序的交互

ios - SwiftUI:如何动态设置自定义 View 大小

objective-c - 如果 Firebase 已集成推送通知,则在 iOS 应用程序中实现 Google Analytics