提问人:Monarch 提问时间:11/16/2023 更新时间:11/16/2023 访问量:22
无法及时获取基于位置的本地通知
Can't get local notification based on location in time
问:
当前的目标是在用户到达确切位置(我添加到数组的预期位置)时获得适当的本地通知。不幸的是,我总是不时收到通知。例如,我在iPhone上运行应用程序并模拟预期位置 - 什么也没发生,然后我切换到另一个适当的位置 - 仍然没有发生任何事情,当我第三次选择适当的位置时 - 我收到与第一个位置相关的通知,其他一两个通知处于待处理状态。代码示例如下。
import UIKit
import Foundation
import CoreLocation
import MapKit
import Foundation
import NotificationCenter
class ViewController: UIViewController, CLLocationManagerDelegate, UNUserNotificationCenterDelegate {
var positionAlert:UIAlertController? = nil
var ULocation:UserLocations?
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let identifier = UUID().uuidString
var usrAlrt:AlertNotification?
let locList = locationList()
let firstLocation = CLLocationCoordinate2D(latitude: 40.0, longitude: 120.0)
let secondLocation = CLLocationCoordinate2D(latitude: 4.0, longitude: 12.0)
@IBAction func btnAlrt(_ sender: Any) {
print(ULocation!.locManager.location!)
}
//MARK: - Send changed location to local variable
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNNotificationPresentationOptions.list)
}
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
locList.locationAdd(location: firstLocation)
locList.locationAdd(location: secondLocation)
ULocation = UserLocations(locationManager: CLLocationManager(),Delegate: self)
DispatchQueue.main.async {
_ = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) {
[self] timer in
if(locList.locatinFinder(location: ULocation!.locManager.location!.coordinate)){
let long = ULocation!.locManager.location!.coordinate.longitude
let lat = ULocation!.locManager.location!.coordinate.latitude
print("MATCHED")
AlertNotification(region: CLCircularRegion(center: CLLocationCoordinate2D(latitude: lat, longitude: long), radius: 30, identifier: identifier), delegate: self, identifier: identifier).scheduleNotification(notificationType: "\(ULocation!.locManager.location!)")
usrAlrt?.notificationCenter.getPendingNotificationRequests(completionHandler: { notif in
for notification in notif {
print(notification)
}
})
}
}
}
}
}
我已经阅读了所有可能的文档并尝试实现我找到的所有示例,但仍然有同样的问题。
答: 暂无答案
评论