swift - 如何安排进入某个区域后的某个时间推送通知? swift

标签 swift push-notification location apple-push-notifications

我想在进入某个区域 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/

相关文章:

ios - 快速更新了每个 API 的 almofire timeoutIntervalForRequest 吗?

android - android中聊天应用程序的通知

push-notification - Web Push vapid key 生成逻辑

iphone - iOS 推送通知问题

android - 在 GooglePlayServices API 中是否有检测 Android 位置设置的新方法?

android - 如何编写依赖于位置更新的 Android 单元测试?

javascript - 我如何等待 location.href 在 JavaScript 中完成?

json - 如何从swift调用rest web服务

ios - tableView单元格不显示数据,有什么问题? ( swift 3)

swift - View 不会被复制