[英]Remove fired Location-Based notification when user exits region
I've setup (default iOS8) location-based notifications for my app. 我为我的应用设置了(默认iOS8)基于位置的通知。
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.regionTriggersOnce = NO;
notification.userInfo = @{ @"notification_id" : @"someID" };
notification.region = region;
notification.alertBody = alertBody;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
When a user enters the specified region, the notification is shown in the NotificationCenter
correctly. 当用户进入指定区域时,通知将正确显示在NotificationCenter
。
However, I want to remove that notification message when the user exits that region, because it makes little sense for the user to go home and look at the notification center until they recieve a message which looks like so: 但是,我想在用户退出该区域时删除该通知消息,因为用户回家并查看通知中心直到他们收到如下所示的消息是没有意义的:
"You are at XXXXX!" “你在XXXXX!”
Has anyone tried something similar? 有没有人尝试类似的东西? The documentation is unclear on how this can be done. 文档不清楚如何做到这一点。
CLRegion
object has special properties for that: notifyOnEntry
and notifyOnExit
. CLRegion
对象具有以下特殊属性: notifyOnEntry
和notifyOnExit
。
Everything that you need is update code in this way: 您需要的一切都是以这种方式更新代码:
CLRegion *region = .... // Configure region here
region.notifyOnEntry = YES;
region.notifyOnExit = NO;
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.regionTriggersOnce = NO;
notification.userInfo = @{ @"notification_id" : @"someID" };
notification.region = region;
notification.alertBody = alertBody;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Here is Apple documentation that explains it: 以下是解释它的Apple 文档 :
@property(nonatomic, copy) CLRegion *region
Assigning a value to this property causes the local notification to be delivered when the user crosses the region's boundary. 为此属性分配值会导致在用户跨越区域边界时传递本地通知。
The region object itself defines whether the notification is triggered when the user enters or exits the region. 区域对象本身定义在用户进入或退出区域时是否触发通知。
I was really tired yesterday and couldn't complete my answer in time. 我昨天真的很累,无法及时完成答案。
Based on some answers here I have only a clue what you have to do. 基于这里的一些答案,我只知道你必须做什么。 I did not tried it myself (geofencing is a really a pain to test, I know that because I'm working on a geofencing project atm.), I also never had to delete a delivered notification from the Notification Center before. 我自己没有尝试过(地理围栏是一个非常痛苦的测试,我知道,因为我正在研究地理围栏项目。),我也从来没有必须删除通知中心发送的通知。
I assume your application will not terminate due the whole process. 我认为您的申请不会因整个过程而终止。 So we won't use startMonitoringForRegion
function here. 所以我们不会在这里使用startMonitoringForRegion
函数。
Answer written in Swift 2.0 (It's not that hard to translate it to ObjC). 用Swift 2.0编写的答案(将它翻译成ObjC并不难)。
func createAndRegisterSomeNotificationSomewhere() {
let region = CLCircularRegion(center: someCoordinates, radius: someRadius, identifier: someIdentifier)
region.notifyOnEntry = true
region.notifyOnExit = true
let locationNotification = UILocalNotification()
locationNotification.alertBody = "someAlertBody"
locationNotification.userInfo = ["notification_id" : "someID"]
locationNotification.regionTriggersOnce = false
locationNotification.region = region // remember 'presentLocalNotificationNow' will not work if this value is set
UIApplication.sharedApplication().scheduleLocalNotification(locationNotification)
}
/* CLLocationManagerDelegate provides two function */
// func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)
// func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion)
/* If I'm not mistaken they are only called for monitored regions and not location based local notifications */
/* I mean you will have to use something like: self.locationManager.startMonitoringForRegion(someCircularRegion) */
/* Correct me if I'm wrong. So consider to rebuild the following logic to ease everything if you want to monitor regions. */
/* Now when you receive your location notification */
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if let region = notification.region {
self.locationManager.requestStateForRegion(region)
/* based on other answers this will remove your noticaiton from NC and cancel from showing it anywhere */
application.cancelLocalNotification(notification)
/* but we need this notification still be scheduled because 'region.notifyOnExit = true' should fire it again later */
application.scheduleLocalNotification(notification)
}
}
func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
/* this is not the best solution, because it adds some latency to the dilivery code and CLRegionState can also be Unknown sometimes */
/* I'd go with the two functions above if I only had up to 20 regions to monitor (max region limit per App, read CLLocationManager docs) */
/* the mechanics would be more clear and save */
switch state {
case .Inside:
/* create a new noticiation with the same cpecs as the cancled notification but this time withot the region */
let sameNotificationAsAbove = UILocalNotification()
/* if you really need to know your IDs inside userInfo so create some good mechanics to pass these before canceling */
/* at least I would save the identifier of the region iside the noticiation */
/* save the notification somewhere to delete it later from NC */
self.someArrayToSaveDeliveredNotifications.append(sameNotificationAsAbove)
/* fire the notification */
UIApplication.sharedApplication().presentLocalNotificationNow(sameNotificationAsAbove)
case default:
/* if it is true that notication inside NC can be deleted just by calling 'cancelLocalNotification' function */
/* so find your notification inside someArrayToSaveDeliveredNotifications bases on the region.identier which you saved inside userInfo */
let notificationToCancel = self.getNotificationForIdentifier(region.identifier)
UIApplication.sharedApplication().cancelLocalNotification(notificationToCancel)
/* this should delete your notification from NC based on other answers */
}
}
This is some kind of pseudo mechanics I would build if I had to, so if anything is wrong or not correct I would appreciate to hear your feedback. 这是我必须建立的某种伪机制,所以如果有什么不对或不正确我会很高兴听到你的反馈。 :) :)
This will clear all of the app's notifications from Notification Center. 这将清除通知中心的所有应用程序通知。
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
It appears that you can clear a specific notification if you hold onto the UILocalNotification object. 如果您按住UILocalNotification对象,似乎可以清除特定通知。 Using your the notification object you created in your example above you can call 使用您在上面的示例中创建的通知对象,您可以调用
[[UIApplication sharedApplication] cancelLocalNotification:notification];
to clear the notification. 清除通知。
It looks like very simple solution. 它看起来非常简单。 follow the below steps it will help you. 按照以下步骤,它将帮助您。
You should trigger a background method which should only be called when the device is going out of the location. 您应该触发一个后台方法,该方法只应在设备离开该位置时调用。 This can be achieved by creating layer for region and trigger immediately when it is coming out of boundary. 这可以通过为区域创建图层并在其离开边界时立即触发来实现。
In the method, you may either clear all the notification of the respective app by 在该方法中,您可以清除相应应用程序的所有通知
[[UIApplication sharedApplication] cancelLocalNotification:notification];
or may clear only specific notification by calling 或者可以通过致电清除特定通知
UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
application.scheduledLocalNotifications = scheduledNotifications;
You will receive all the valid notification available for the particular app. 您将收到特定应用程序的所有有效通知。 Delete the specific notification for the specific region. 删除特定区域的特定通知。
CLLocationManagerDelegate delegate has a set of methods that get triggered base on the device location. CLLocationManagerDelegate委托具有一组基于设备位置触发的方法。 Such as; 如;
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
In your case what you have to do is when the following callback get triggered; 在您的情况下,您需要做的是触发以下回调时;
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
remove your notification from the NotificationCenter 从NotificationCenter中删除您的通知
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.