当应用程序处于后台模式时,如何丢失CloudKit通知? [英] How can I get missed CloudKit notification while app is in the background mode?

查看:81
本文介绍了当应用程序处于后台模式时,如何丢失CloudKit通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. CloudKit 管理我的通知(不是我的专用服务器

  2. 我的第一设备更改了 CloudKit容器并推送了通知。

  3. ...但是在我的第二设备上,我的应用是当前在后台模式下运行。所以...通知随 Alert 到达设备,但应用程序本身并不知道。

  1. CloudKit manages my notifications (not my dedicated server)
  2. My first device changes sth in CloudKit Container and pushes notification.
  3. ... but on my second device my app is currently running in background mode. So... the notification arrives to device with Alert, but the app itself doesn't know about it.

当应用程序返回到前台模式时,一种巧妙而又有效的方法来捕获此一条错过的通知(甚至更多)是什么?

假设更改与我的顶部可见控制器有关,我想应用此更改而无需在 viewDidAppear:上获取任何内容。

Suppose the change is related to my top visible controller, and I would like to apply that change without fetching anything on viewDidAppear:.

推荐答案

只需执行以下操作,即可在 UIApplicationDelegate 方法内实现:

Simply you can do the following, implemented inside UIApplicationDelegate method:

func applicationWillEnterForeground(application: UIApplication) {

    var queryNotifications = [CKQueryNotification]()
    let operation = CKFetchNotificationChangesOperation(previousServerChangeToken: nil)

    operation.notificationChangedBlock = { notification in

        if let queryNotification = notification as? CKQueryNotification {
            queryNotifications.append(queryNotification)
        }
    }

    operation.fetchNotificationChangesCompletionBlock = { token, error in

        var notificationIdentifiers = [CKNotificationID]()
        for queryNotification in queryNotifications {

            let recordID = queryNotification.recordID!

            //here you can do enything you need with your recordID
            container.publicCloudDatabase.fetchRecordWithID(recordID, completionHandler: { object, error in

                notificationIdentifiers.append(queryNotification.notificationID!)

                if queryNotifications.count == notificationIdentifiers.count {

                    let operationQueue = NSOperationQueue()
                    operationQueue.addOperation(CKMarkNotificationsReadOperation(notificationIDsToMarkRead: notificationIdentifiers))
                }
            })
        }
    }

    let operationQueue = NSOperationQueue()
    operationQueue.addOperation(operation)
}

这篇关于当应用程序处于后台模式时,如何丢失CloudKit通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆