首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS在通知服务扩展中获取用户位置

iOS在通知服务扩展中获取用户位置
EN

Stack Overflow用户
提问于 2020-05-14 19:18:15
回答 2查看 504关注 0票数 1

UNNotificationServiceExtension中可以使用CoreLocation框架吗?我尝试过的:

代码语言:javascript
复制
class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    private let locationManager = CLLocationManager()

    override func didReceive(_ request: UNNotificationRequest,
                             withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        os_log("%{public}@",
        log: OSLog(subsystem: "bundleIdentifier", category: "WakeUpExtension"),
        type: OSLogType.debug,
        "Push received")
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
    }

    override func serviceExtensionTimeWillExpire() {
        os_log("%{public}@",
        log: OSLog(subsystem: "bundleIdentifier", category: "WakeUpExtension"),
        type: OSLogType.debug,
        "Task expired")
        locationManager.stopUpdatingLocation()
        locationManager.delegate = nil

        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}
代码语言:javascript
复制
extension NotificationService: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locations.forEach {
            os_log("%{public}@",
            log: OSLog(subsystem: "bundleIdentifier", category: "WakeUpExtension"),
            type: OSLogType.debug,
            $0.horizontalAccuracy)
        }

        for location in locations {
            let locationAge = -location.timestamp.timeIntervalSinceNow
            if locationAge < 5 {
                os_log("%{public}@",
                log: OSLog(subsystem: "bundleIdentifier", category: "WakeUpExtension"),
                type: OSLogType.debug,
                "Send")
                locationManager.stopUpdatingLocation()
                locationManager.delegate = nil
                contentHandler?(UNNotificationContent())
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        os_log("%{public}@",
        log: OSLog(subsystem: "bundleIdentifier", category: "WakeUpExtension"),
        type: OSLogType.debug,
        error.localizedDescription)
    }
}

我在控制台里得到的只有:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-29 04:13:22

看起来在UNNotificationServiceExtension中永远不会调用CLLocationManagerDelegate方法。但是可以从CLLocationManager location属性中获取最后接收到的位置:

代码语言:javascript
复制
 let locationManager = CLLocationManager()
 let lastLocation = locationManager.location
票数 0
EN

Stack Overflow用户

发布于 2020-07-15 21:17:02

您需要实现一个后台url会话。它有助于延迟扩展到期,在这种情况下,直到位置更新被传递(以及任何其他编写的后期处理)。

代码语言:javascript
复制
private lazy var urlSession: URLSession = {
    let config = URLSessionConfiguration.background(withIdentifier: "MySession")
    config.sessionSendsLaunchEvents = true
    return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()

参考:https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61796055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档