2016-03-18 4 views
2

私は「テクスチャ・アプリ」と呼ぶことができます。クラウド・キットを使用しています。クラウド・キットで動作する通知を追加するためにどこからでも探しています...誰かが私にコードを教えてくれますか?私は非常に失われているので、クラウドキットのプッシュ通知を詳細に追加する...また、別の "テクスチャルーム"に行く通知をwan'tしないでください(クラウドキットではレコードタイプになります...) 「テキスト2」と呼ばれるもう1つのテキストとは、「テキスト2」を使用する人に「テキスト」からの通知を送信したくないということです。エルキャプテン& Xcodeのスイフト2.0を使用してスウィフト2とクラウドキットでの通知

+0

は[CKSubscription(https://developer.apple.com/library/prerelease/ios/documentation/CloudKit/Reference/CKSubscription_class/index.html)を見てください。 – tktsubota

+0

この回答をお読みください:http://stackoverflow.com/a/38171514/2725435 –

答えて

3

7.2.1

イーリア、あなたはあなたのアプリケーションのデリゲートにこれを追加する必要があります。これはデータのuserInfoパケットに到着します。データのパケットを解析して、送信されたデータベース/アプリを確認できます。あなたが始めるでしょう。この方法

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

}

より

UIApplicationDelegate to the class 
application.registerForRemoteNotifications() to the 

func application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

この方法を使用すると、開発中にダッシュボードを使用することができますが、プログラムによるサブスクリプションの確認ができます。

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


} 
+0

お手数をおかけしていただきありがとうございます。よろしくお願いします。Elia –

+0

私のアプリが決してアクセスしなかったすべての通知をフェッチすることは本当に可能ですか?本当に?私はそれをテストする必要があります。もしそうなら、アップルの「クラウドキット」は本当に素晴らしいです;) –

+0

はい、私はそう信じています。 – user3069232

関連する問題