ある座標の半径500mに入った時点でローカル通知を出したいのですが上手く動きません。
Notification, Location(Always usage)はどちらも許可されている前提です。
またCapablitiesの設定は画像の通りです
Swift4
1import UIKit 2import CoreLocation 3import UserNotifications 4 5class a: UIViewController { 6 7 @objc func didSwitched(_ sender: UISwitch) { 8 switch sender.isOn { 9 case true: 10 setupNotification(coordinate: CLLocationCoordinate2D(任意の値)) 11 case false: 12 deleteNotification() 13 } 14 } 15 16 17 func setupNotification(coordinate: CLLocationCoordinate2D) { 18 let trigger: UNNotificationTrigger 19 let region = CLCircularRegion(center: coordinate, radius: 500, identifier: "destination") 20 region.notifyOnExit = false 21 trigger = UNLocationNotificationTrigger(region: region, repeats: false) 22 let content = UNMutableNotificationContent() 23 content.title = "title" 24 content.body = "body" 25 content.sound = UNNotificationSound.default 26 let request = UNNotificationRequest(identifier: "destination", content: content, trigger: trigger) 27 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 28 } 29 30 func deleteNotification() { 31 UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in 32 for request in requests{ 33 if request.content.categoryIdentifier == "destination"{ 34 UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["destination"]) 35 } 36 } 37 } 38 } 39} 40
あなたの回答
tips
プレビュー