我想在进入某个区域 20 分钟后向时间表发送本地推送通知。这是我当前的代码,不起作用。这是我的 View Controller 中的代码:
override func viewDidLoad() {
super.viewDidLoad()
NavViewController.locationManager.requestAlwaysAuthorization()
NavViewController.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
NavViewController.locationManager.delegate = self
NavViewController.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
NavViewController.locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
notificationSettings.setUpTimeIntervalNotification()
}
这是我的 notificationSettings 类中的代码:
class func setUpTimeIntervalNotification(){
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Are you ready to do your exercises?"
content.body = "Tap to start now."
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (20*60), repeats: false)
let request = UNNotificationRequest(identifier: Notification.Location.rawValue, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
}
有什么想法吗?
最佳答案
只需在 appdelegate 中添加此代码即可正常工作
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().delegate = self
return true
}
}
//符合UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler(.alert)
}
}
关于swift - 如何安排进入某个区域后的某个时间推送通知? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59494136/