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가 플레이어 레코드를 생성하거나 업데이트 할 때 사용자 A가 푸시를 통해 알림을 받고 10KM 내에 있어야합니다.

나는 내 앱에서 밀어 넣기 권한 설정을 올바르게했다고 믿는다. (예를 들어 사용자는 자신의 하위가 생성되기 전에이를 확인하라는 메시지가 표시된다.)

푸시가 발생하지 않습니다. 어떤 아이디어? 근본적인 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가 현재 위치에서 10KM 이내에있는 위치로 플레이어 B가 자신의 Player 개체를 업데이트했다는 것을 알려주는 이러한 유형의 가입 작업입니까? 내 대시 보드에서 두 개의 테스트 장치에 두 개의 계정이 있지만 여전히 구독 유형이 하나만 표시됩니다. – Genericrich

+0

미터 대 km에 대한 그의 관찰을 위해 didgog에게 현상금을 수여하십시오. – Genericrich