【发布时间】:2016-12-29 15:02:45
【问题描述】:
我正在尝试在用户接近停靠点时获取一些公共汽车的到达时间,我已经测试以确保通过发送基本本地通知来正确触发这些区域,并且我还测试了我的 Web 服务调用以确保它工作正常。
但是我很难获取信息然后发送通知。
这是我的代码:
var bgTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier()
bgTask = UIApplication.shared().beginBackgroundTask {
self.webService?.getStopEstimates(routeStopIds: stopRouteIdSet, routeNameDict: routeNameDict, completion: { (result) in
if result == "error" {
return
}
let notification = UILocalNotification()
notification.fireDate = Date(timeIntervalSinceNow: 1)
notification.alertTitle = region.identifier + " Stop Details"
notification.alertBody = result
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared().scheduleLocalNotification(notification)
})
UIApplication.shared().endBackgroundTask(bgTask)
}
有人知道为什么它可能没有发送吗?我启用了后台获取和定位服务。这是里面didEnterRegion
【问题讨论】:
-
鉴于您使用的是 Swift 3,因此知道有一个新的 UserNotifications 框架。
-
会不会是您忘记在基金申请中注册通知(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)。注册码:let notificationSettings = UIUserNotificationSettings(types: .alert, categories: nil) application.registerUserNotificationSettings(notificationSettings)
-
你是如何测试的?在 Xcode 中?添加一些日志记录以检查请求是否已发送/接收。有什么错误吗?
标签: ios swift core-location uilocalnotification