2016-06-12 3 views
0

私は、次のサブスクリプションに苦しんでいます:新しいプレーヤーレコードが作成または更新されたときにCloudKit購読苦悩は

let predicate = NSPredicate(format: "gc_alias != %@ AND distanceToLocation:fromLocation:(%K,%@) < %f", 
           self.localPlayer!.alias!, 
           "location", 
           self.currentLocation!, 
           10) 


    let subscription = CKSubscription(recordType: "Player", predicate: predicate, options: .FiresOnRecordCreation) 
    subscription.zoneID = nil 

    let notification = CKNotificationInfo() 
    notification.alertBody = "Nearby Player within Range!" 
    notification.soundName = UILocalNotificationDefaultSoundName 

    subscription.notificationInfo = notification 

    let container = CKContainer.defaultContainer() 
    let publicDb = container.publicCloudDatabase 

    publicDb.saveSubscription(subscription) { (result, error) -> Void in 
     if error != nil { 
      print(error!.localizedDescription) 
     } else { 
      print("SUBSCRIBED SUCCESS") 
      print(result) 
      NSUserDefaults.standardUserDefaults().setBool(true, forKey: "subscribed") 
     } 
    } 

は基本的に、私は、ユーザの位置を記録します。

ユーザBがPlayerレコードを作成または更新して10KM以内にいる場合、プッシュ経由でユーザAに通知するようにします。

私はプッシュパーミッションが正しく設定されていると信じています(たとえば、サブが作成される前にこれを確認するように求められます)。

プッシュは到着しません。何か案は?基本的なCKの誤解に苦しんでいますか?

+0

はあなたがCKレコードを作成し、通知の登録場所のコードを追加することができます。このような何かはトリックを行うべき? – diegog

+0

distanceToLocation:fromLocation :()は、キロメートルではなくメートルを返します。 10の代わりに10000を渡す必要があります。 – diegog

+0

Diegog FTW!メーターズはそれを揺るがすようだ!驚くべきことに、これに関するウェブ上の誤った情報があります。 :| – Genericrich

答えて

0

あなたは、プッシュ通知のために登録していないようです:

iOS Developer Library: Subscribing to Record Changes

たときに自動的にサブスクリプション火災通知を受け取るためにあなたのアプリを設定していないデータベースへのサブスクリプションを保存します。 CloudKitは、Appleプッシュ通知サービス(APN)を使用してアプリにサブスクリプション通知を送信するので、プッシュ通知を受け取るには登録する必要があります。 Hacking with Swift: Delivering notifications with CloudKit push messages: CKSubscription and saveSubscriptionによると

あなたがする必要があります

AppDelegate.swiftに移動し、didFinishLaunchingWithOptionsメソッドにこのコードを置くための:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil) 
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
UIApplication.sharedApplication().registerForRemoteNotifications() 

そして

完了のために、あなたはオプションとすることができますまた、アプリデリゲートに送信されたdidReceiveRemoteNotificationメッセージをキャッチします。これは、アプリの実行中にプッシュメッセージが届いた場合に呼び出されます。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    if let pushInfo = userInfo as? [String: NSObject] { 
     let notification = CKNotification(fromRemoteNotificationDictionary: pushInfo) 

     let ac = UIAlertController(title: "What's that Whistle?", message: notification.alertBody, preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 

     if let nc = window?.rootViewController as? UINavigationController { 
      if let vc = nc.visibleViewController { 
       vc.presentViewController(ac, animated: true, completion: nil) 
      } 
     } 
    } 
} 
+0

私は他の場所で通知を登録しています。私が知りたいのは、このタイプのサブスクリプションがプレーヤーAに通知するためのサブスクリプションで、プレーヤーBが現在の場所の10KM以内の場所でCKのプレーヤーオブジェクトを更新したということです。私のダッシュボードでは、2つのテストデバイス上に2つのアカウントがありますが、サブスクリプションタイプは1つしか表示されません。 – Genericrich

+0

メーター対km以上の彼の観察のためにdidgogに賞金を与えました。 – Genericrich