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

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

问题描述

我一直在iOS上开发区域监控约2个月。最近我们发现了一个小故障,同时在一定半径范围内(约4.7KM或4700米)触发了所有区域。设备的当前位置甚至不靠近任何区域。我不确定是什么原因引发了这一事件。我搜索了StackOverFlow,Apple Developer论坛等,我没有发现任何类似的问题。

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个其他区域从不发送通知(从不覆盖在蓝色圆圈下)

  • The green marker is the location of the device when receiving the notification.
  • The blue circle is the potential radius of the device (about 4700 meters) which is covering 4 regions that send the notification to the device.
  • The red circle is the radius for each of the region.
  • There are 2 other regions on the map which never send notifications (never cover under blue circle)

触发通知的屏幕截图

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

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.

推荐答案

我找到了修复这个奇怪的bug。我们测试了超过1周,到目前为止我们还没有再看到相同的bug。以下是解决方案: -

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.

有关此错误及其解决方法的详细说明,您可以访问: 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天全站免登陆