2016-02-17 4 views
0

Я пытаюсь подписаться на push-уведомления с помощью CloudKit с Swift. Вот мой код:подписка на CloudKit не работает

App делегат:

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

    //Push 
    let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil) 
    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 
    if let options: NSDictionary = launchOptions { 
     let remoteNotification = options.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as? NSDictionary 
     if let notification = remoteNotification { 
      self.application(application, didReceiveRemoteNotification: notification as! [NSObject : AnyObject]) 
     } 
    } 

    return true 

} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    let ckNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject]) 
    if ckNotification.notificationType == .Query { 
     let queryNotification = ckNotification as! CKQueryNotification 
     let recordID = queryNotification.recordID 
     let container = CKContainer.defaultContainer() 
     let privateDatabase = container.privateCloudDatabase 
     privateDatabase.fetchRecordWithID(recordID!) {newRecord, error in 
      if error != nil { 
       print(error) 
      } else { 
       NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in 
        print(newRecord) 
       }) 
      } 
     } 
    } 
} 

Создание:

func addNewRecordsSub() { 
    let subscription = CKSubscription(recordType: "UserRecords", predicate: predicate, options: .FiresOnRecordCreation) 
    let notificationInfo = CKNotificationInfo() 
    notificationInfo.alertBody = "OK!" 
    notificationInfo.shouldBadge = true 
    subscription.notificationInfo = notificationInfo 
    let privateDatabase = container.privateCloudDatabase 
    privateDatabase.saveSubscription(subscription) { subscription, error in 
     if error != nil { 
      print(error) 
     } 

    } 
} 

После подписки запуск был появиться в приборной панели CloudKit в:

enter image description here

Но ничего не происходит, когда я добавляю новую запись ... Просто ничего. Я что-то пропустил?

+0

ли вы попробовать реализовать desiredKeys & SoundName и посмотреть, если есть какая-то разница? '' notificationInfo.desiredKeys = ["yourKey1", "yourKey2"] '' и '' notificationInfo.soundName = UILocalNotificationDefaultSoundName'' – Allen

+0

@Allen все еще не работает. – dsk1306

+0

Я также пытался сбросить среду, но все равно ничего. – dsk1306

ответ

1

Я попытался сбросить окружающую среду еще раз, и все работы теперь ...