iOS 7 上的区域监控故障 - 同时发送多个通知 [英] Region Monitoring Glitch on iOS 7 - Multiple Notifications at the same time

查看:17
本文介绍了iOS 7 上的区域监控故障 - 同时发送多个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 iOS 上开发区域监控大约 2 个月了.最近我们发现了一个小故障,某个半径(大约4.7KM或4700米)内的所有区域同时触发.该设备的当前位置甚至不靠近任何区域.我不确定是什么触发了该事件.我已经通过 StackOverFlow、Apple 开发者论坛等进行了搜索,但没有发现任何与我面临的问题类似的问题.

I have been developing region monitoring on iOS for about 2 months. Recently we have found a glitch where all the regions within a certain radius (about 4.7KM or 4700 meters) triggered at the same time. The current location of the device is not even close to any of the regions. I am not sure what could have triggered that event. I have searched through StackOverFlow, Apple Developer forums and etc, I haven't find any similar issue with what I am facing.

在我正在开发的应用程序中,我们正在监控城市(吉隆坡)内的 8 个区域.有一次,我的同事发现他的手机上同时触发了4个区域通知.下面的地图显示了他的位置、所有监控区域、触发 4 个区域通知的潜在半径.

In the app that I am developing, we are monitoring 8 regions within the City (Kuala Lumpur). On one instance, my colleague found out that there were 4 regions notification triggered on his phone at the same time. Below is the map showing his location, all the monitored regions, the potential radius that triggered the 4 region notifications.

  • 绿色标记是接收通知时设备的位置.
  • 蓝色圆圈是设备的潜在半径(约 4700 米),覆盖了向设备发送通知的 4 个区域.
  • 红色圆圈是每个区域的半径.
  • 地图上还有另外 2 个区域从不发送通知(永远不会覆盖在蓝色圆圈下)

触发通知的屏幕截图:

这是我的位置管理器代码:-

CLLocationManager *locationManager = [LocationTracker sharedLocationManager];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLDistanceFilterNone;

这是我的 didEnterRegion 代码:-

-(void)locationManager:(CLLocationManager *)manager 
        didEnterRegion:(CLRegion *)region{

NSString* message = [NSString stringWithFormat:@"Message"];        
UIApplicationState state = [[UIApplication sharedApplication] applicationState];

if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
   UILocalNotification *notification = [[UILocalNotification alloc] init];
   notification.fireDate = [NSDate date];
   NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
   notification.timeZone = timezone;
   notification.alertBody = message;
   notification.alertAction = @"Show";
   notification.soundName = UILocalNotificationDefaultSoundName;
   [[UIApplication sharedApplication] scheduleLocalNotification:notification];
 }

}

注意:此问题不会每次都发生,只是偶尔发生一次.4700米是我分析了4个触发区域的位置后得出的半径.我不确定这是我的代码、iOS 上的故障还是我所在国家/地区的本地电信公司出现问题.在最新版本的应用中,我将 distanceFiter 调整为 10,我们现在正在测试它是否能解决问题.

Note: This issue does not happen every time, it only happens once in a while. The 4700 meters is the radius that I came out with after analysing the location of the 4 triggered regions. I am not sure if this is a glitch on my code, on the iOS or there is a problem on the local telco in my country. In the latest version of the app, I am adjusting the distanceFiter to 10 and we are testing it right now to see if this will solve the issue.

//locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.distanceFilter = 10;

方法 didEnterRegion 永远不会返回用户的位置,我无法像上面显示的示例那样过滤掉半径大的潜在坏位置.我能做些什么来解决这个故障?

The method didEnterRegion never return the location of the user, I am not able to filter out the potential bad location with a big radius like the example I show above. What I can do to solve this glitch?

任何遇到类似问题的开发者,请站出来分享您解决此问题的经验和解决方案(如果有).谢谢.

Any developer who is facing the similar issue, please come forward and share your experience and solution to solve this issue (if there is any). Thanks.

推荐答案

我找到了解决这个奇怪错误的方法.我们已经测试了超过 1 周,到目前为止我们还没有再次看到相同的错误.这是解决方案:-

I have found a fix for this strange bug. We have tested for over 1 week and so far we haven't see the same bug again. Here is the solution:-

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{

NSLog(@"didEnterRegion");
CLLocation * lastLocation = [manager location];

BOOL doesItContainMyPoint;

if(lastLocation==nil)
    doesItContainMyPoint = NO;
else{
    CLLocationCoordinate2D theLocationCoordinate = lastLocation.coordinate;
    CLCircularRegion * theRegion = (CLCircularRegion*)region;
    doesItContainMyPoint = [theRegion containsCoordinate:theLocationCoordinate];
}

if(doesItContainMyPoint){
    NSString* message = [NSString stringWithFormat:@"You are now in this region:%@",region.identifier];
    UIApplicationState state = [[UIApplication sharedApplication] applicationState];

    if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
       {
         UILocalNotification *notification = [[UILocalNotification alloc] init];
         notification.fireDate = [NSDate date];
         NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
         notification.timeZone = timezone;
         notification.alertBody = message;
         notification.alertAction = @"Show";
         notification.soundName = UILocalNotificationDefaultSoundName;
         [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
  }
}

我使用 CLLocation * lastLocation = [manager location]; 从设备获取最新位置并使用此坐标查看它是否在带有 containsCoordinate 的触发区域内:方法.如果在里面,那么只会触发本地通知.

I use CLLocation * lastLocation = [manager location]; to get the latest location from the device and use this coordinate to see if it is inside the triggered region with containsCoordinate: method. If it is inside, then only the local notification will trigger.

关于这个bug的详细解释和修复方法,可以访问:iOS 区域监控和位置管理器

For the details explanation on this bug and the way to fix it, you may visit: iOS Region Monitoring and Location Manager

这篇关于iOS 7 上的区域监控故障 - 同时发送多个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆