Geofence Region複数設定したい。(20箇所)
CLCircularRegion.identifier別に通知を配信したい。```class ViewController: UIViewController, CLLocationManagerDelegate {
var audioPlayerInstance : AVAudioPlayer! = nil
@IBOutlet weak var myimage: UIImageView!
let locationManager: CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.myimage.isHidden = true
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.distanceFilter = 10
let geoFenceRegion:CLCircularRegion = CLCircularRegion(center: CLLocationCoordinate2DMake(33.654237, 130.693872),radius: 500.0,identifier: "TokyoTower")
locationManager.startMonitoring(for: geoFenceRegion)
let soundFilePath = Bundle.main.path(forResource: "new", ofType: "mp3")!
let sound:URL = URL(fileURLWithPath: soundFilePath)
// AVAudioPlayerのインスタンスを作成
do {
audioPlayerInstance = try AVAudioPlayer(contentsOf: sound, fileTypeHint:nil)
} catch {
print("AVAudioPlayerインスタンス作成失敗")
}
// バッファに保持していつでも再生できるようにする
audioPlayerInstance.prepareToPlay()
audioPlayerInstance.volume = 2
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound], completionHandler: {(success,error) in
if error != nil{
print("失敗しました")
}
else{
print("成功しました")
}
})
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("設定したジオフェンスに入りました。")
self.myimage.isHidden = false
audioPlayerInstance.play()
timedNotifications(inSeconds: 3) {(success) in
if success {
print("Successfully Notified")
}
}
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("設定したジオフェンスから出ました。")
self.myimage.isHidden = true
}
func timedNotifications(inSeconds: TimeInterval, completion: @escaping (_ _Success: Bool) -> ()){
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: inSeconds, repeats: false)
let content = UNMutableNotificationContent()
content.title = "浦上亭お知らせ"
content.subtitle = "本日のサービス"
content.body = "高級手羽先30%OFFでの提供!19:00まで"
let request = UNNotificationRequest(identifier: "customNotification", content: content,trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error != nil {
completion(false)
}else{
completion(true)
}
}
}
}
コード
あなたの回答
tips
プレビュー