iOS:如何使用区域触发器使本地通知失效?

时间:2015-12-31 09:34:03

标签: ios swift location uilocalnotification geofencing

我有一个本地地理围栏通知,使用以下代码进行设置:

func startMonitoring(annotation:MKAnnotation) {

    let region = CLCircularRegion(center: annotation.coordinate, radius: 10.0, identifier: "randomID")
    region.notifyOnExit = false

    let notification = UILocalNotification()
    notification.region = region
    notification.alertBody = "You got it!"
    notification.category = self.notificationCategory
    notification.soundName = "my_sound.wav"
    notification.userInfo = ["ID": "randomID"]

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

这很有效,但是用户必须在一定时限内到达该区域才能使通知的行动可用。如果时间限制已经过去,我如何才能通知?

我目前只有" WhenInUse"授权用户的位置,并希望保持这种方式。

2 个答案:

答案 0 :(得分:1)

最好使用CLLocationManager手动监控CLCircularRegion,当您的应用被通知用户已进入该区域时,请检查它是否在时间限制内并手动发布UILocalNotification。

请参阅此处的Using Regions to Monitor Boundary Crossings部分:https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html

答案 1 :(得分:1)

您可以使用setminimumbackgroundfetchinterval设置后台提取,如果预定通知将过期,则取消已安排的通知。

它不是100%可靠,因为你无法控制确切的时间,但它总比没有好。

相关问题