2016-11-16 10 views
7

iOS 10で利用可能なユーザ通知フレームワークを使用しています。ユーザがUNLocationNotificationTriggerを使用して特定のジオロケーションを入力するたびに通知をトリガしようとしました。 Geoの位置をシミュレートしてシミュレータを使ってテストしようとしたとき、通知はトリガされませんが、ロケーションマネージャは更新されたジオロケーションを返します。これをシミュレータで実行するのではなく、実際のデバイスでテストする必要がありますか? Apple DocumentationによるとUNLocationNotificationTrigger-シミュレータで動作しません

+0

実際のデバイスでも通知を受け取ることができません。 :/ – Rihards

+0

シミュレータとデバイスの両方でUser Notification Frameworkを使用して、日付と時刻に基づいて通知をトリガすることができます。私はまだデバイス内の位置に基づく通知をチェックしています。 – Ashok

+0

あなたはそれを修正することができましたか?そうでなければ私はあなたに手を差し伸べることができます。 –

答えて

2

アプリが位置情報サービスへのアクセスを要求しなければなりませんし、このクラスを使用することで使用中のアクセス権を持っている必要があります。ロケーションサービスの使用を許可するには、ロケーションベースのトリガーをスケジュールする前にCLLocationManagerのrequestWhenInUseAuthorization()メソッドを呼び出します。

私のエミュレータ/デバイスでは、「使用時」のアクセス許可では不十分な場合は、アクセス許可を「常に」に設定する必要があります。

したがって、

<key>NSLocationAlwaysUsageDescription</key> 
<string>We use your location to warn you when there are adorable cats nearby</string> 

はその後場所を活性化させるあなたのpinfo.listにこのキーを追加します。あなたが常に承認されていることを確認してから、トリガーを定義してください。たとえば、didChangeAuthorizationStatusでここで行っています。

class myClass : CLLocationManagerDelegate { 

var locationManager: CLLocationManager() 

func init() { 
    // Note: defining the location manager locally in this function won't work 
    // var locationManager: CLLocationManager() 
    // as it gets gargabe collected too early. 

    locationManager.delegate = self 
    locationManager.requestAlwaysAuthorization() 
    UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in 
    if !accepted { 
     logger.info("Notification access denied.") 
    } 

} 

// MARK CLLocationManagerDelegate: 
func locationManager(manager: CLLocationManager, 
        didChangeAuthorizationStatus status: CLAuthorizationStatus) 
{ 
    if status == .AuthorizedAlways { 

     let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 61.446812, longitude: 23.859914), 
        radius: 1000, identifier: "test") 
     logger.info("Notification will trigger at \(region)") 
     region.notifyOnEntry = true 
     region.notifyOnExit = false 

     let trigger = UNLocationNotificationTrigger(region: region, repeats:true) 

     let content = UNMutableNotificationContent() 
     content.title = "Oh Dear !" 
     content.body = "It's working!" 
     content.sound = UNNotificationSound.default() 

     let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger) 

     UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 
     UNUserNotificationCenter.current().add(request) {(error) in 
      if let error = error { 
       print("Uh oh! We had an error: \(error)") 
      } 
     } 

    } 
} 
} 
関連する問題