2016-06-14 8 views
0

ユーザーが自分のアプリで新しいイベントを作成するたびに通知を送信しようとしています。私はクラウドキットのダッシュボードで見ることができるので、通知を正常に設定することができましたが、誰かがイベントを作成すると何も起こりません。CloudKitの定期購読が機能しない

事実:ViewController1では、ユーザーが名前と参加者を選択しますViewController2では、彼は他のものを選択し続けています。私が言いたいのは、私のアプリ(通知が届いたとき)に視覚的な詳細をアップロードする必要はないということです。通知が表示されたら、ユーザーは直接ViewController2に行きます。ここに私のコードは次のとおりです。

アプリの委任:

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

    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 

    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 

    return true 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 

    let cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject]) 

    if cloudKitNotification.notificationType == CKNotificationType.Query { 


     NSNotificationCenter.defaultCenter().postNotificationName("notinoti", object: nil) 

    } 
} 

ViewController1:

func setupCloudKitSubscription() { 

    let userDefaults = NSUserDefaults.standardUserDefaults() 

    if userDefaults.boolForKey("subscribed") == false { 

     let predicate = NSPredicate(format: "TRUEPREDICATE", argumentArray: nil) 
     let subscription = CKSubscription(recordType: "Event", predicate: predicate, options: CKSubscriptionOptions.FiresOnRecordCreation) 

     let notificationInfo = CKNotificationInfo() 
     notificationInfo.alertLocalizationKey = "New event" 
     notificationInfo.shouldBadge = true 
     notificationInfo.desiredKeys = ["name"] 

     subscription.notificationInfo = notificationInfo 

     let publicData = CKContainer.defaultContainer().publicCloudDatabase 
     publicData.saveSubscription(subscription, completionHandler: { (subscription, error) in 

      if error != nil { 
       print(error?.localizedDescription) 
      } else { 
       userDefaults.setBool(true, forKey: "subscribed") 
       userDefaults.synchronize() 
      } 
     }) 
    } 
} 

ViewController2:

var currentRecord: CKRecord? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
     NSNotificationCenter.defaultCenter().addObserver(self , selector: Selector("fetchRecord"), name: "notinoti", object: nil) 
    }) 
} 

func fetchRecord(recordID: CKRecordID) -> Void { 

    let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase 

    publicDatabase.fetchRecordWithID(recordID, completionHandler: ({record, error in 
     if let err = error { 
      dispatch_async(dispatch_get_main_queue()) { 
       print(err.localizedDescription) 
      } 
     } else { 
      dispatch_async(dispatch_get_main_queue()) { 
       self.currentRecord = record 
       //Now do something with the data 
      } 
     } 
    })) 
} 

答えて

0

Pedro、

重要なのは私の "働く"購読行が読んでいるかわかりません。自分のコードでsubscriptionIDを取得しましたか?

let subID = String(NSUUID().UUIDString) 
    let predicateY = NSPredicate(value: true) 
    let subscription = CKSubscription(recordType: "Blah", predicate: predicateY, subscriptionID: subID, options: [.FiresOnRecordUpdate, .FiresOnRecordDeletion]) 

私は、クラウドキットのダッシュボードとresubscribed上のDBをリセットし、それが再び働いていたその時点でそれとラウンドだますのしばらく後に動作していないことの漠然とした記憶を、得ました。

+0

こんにちは、お世話になりました!だから、私はダッシュボードをリセットするだけで、うまくいくはずです。 –

+0

はい、それはそれを修正するかもしれません。私のサブスクリプションラインにもサブスクリプションIDが含まれていることに注意してください。 – user3069232

+0

ダッシュボードをリセットしようとしましたが、まだ動作していません...現在、サブIDを使用してコードをやり直します。ありがとう! –

関連する問題