2016-07-02 58 views
0
  1. CloudKit管理我的通知(不是我的專用服務器
  2. 首先在CloudKit集裝箱設備的變化......而推通知。
  3. ...但在我的設備我的應用程序當前正在後臺模式下運行。所以...通知到達設備警報,但應用程序本身不知道它。

當應用程序回到前臺模式時,捕獲這一個錯過通知(甚至更多)的優雅而有效的方法是什麼?如何在應用處於後臺模式時錯過CloudKit通知?

假設更改與我的頂部可見控制器相關,並且我想在不提取任何內容的情況下應用該更改viewDidAppear:

回答

0

只要你能做到以下幾點,裏面UIApplicationDelegate方法來實現:

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) 
} 
相關問題