2017-02-21 7 views
0

ローカルのプッシュ通知を送信しようとしています。現在はUNLocationNotificationTriggerと呼ばれていますが、プッシュ通知がトリガーされていません。通知が表示されないのはなぜですか?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in 
      // Enable or disable features based on authorization. 
     } 

     return true 
    } 

ビューコントローラ

var locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager.delegate = self 
    locationManager.requestAlwaysAuthorization() 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    locationManager.startUpdatingLocation() 
    localLocations() 

} 

func localLocations() { 
    //My current location is equal to the below coordinates, so I am in the radius 
    let center = CLLocationCoordinate2D(latitude: xx.xxxxx, longitude: -xx.xxxx) 
    let region = CLCircularRegion(center: center, radius: 20000, identifier: "Store") 
    region.notifyOnEntry = true 
    region.notifyOnExit = false 
    let trigger = UNLocationNotificationTrigger(region: region, repeats: false) 

    let content = UNMutableNotificationContent() 
    content.body = "Grocery Store" 

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) 
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
} 
+0

_authorizing_ローカル通知のコードを示してください。 – matt

+0

コード – joethemow

答えて

0
//My current location is equal to the below coordinates, so I am in the radius 
// ... 
region.notifyOnEntry = true 

あなたの問題があります。あなたはすでに地域にいる。したがって、あなたはそれを入力することはできません。したがって、通知は発生しません。

+0

で更新された質問 – joethemow

+0

いいえ、それをコメントアウトすることは役に立ちません。私はあなたの現在の位置が「半径内」であるというあなたのコメントについて話しています。したがって通知は発生しません。通知が発生するためには、領域を積極的に( 'entry'のために)入れ替えるか(exit'のために)しなければなりません。あなたはまだ座っている間はテストすることはできません。あなたが「半径内にいる」ために発射されません。 – matt

+0

そして別のポイントがあります。リージョン通知を使用するために 'startUpdatingLocation'を行う必要はありません。あなたが 'startUpdatingLocation'を実行しているなら、あなたはすでにどこにいるのか、あなたはいつも_know_です。地域通知を使用する必要はありません。地域の通知を_do_すると、startUpdatingLocation'を実行しないでください。必要に応じてシステムにユーザーに通知させます。 – matt

関連する問題