繁体   English   中英

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

[英]iOS: How to expire a local notification with a region trigger?

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

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”授权,并希望保持这种方式。

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

请参阅此处Using Regions to Monitor Boundary Crossingshttps//developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html

您可以使用setminimumbackgroundfetchinterval设置后台提取,并在计划到期时取消已安排的通知。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM