2013-02-24 34 views
4

我正在研究一个应用程序,当他靠近使用区域监视的某些地标时提醒用户。一切正常,但当应用程序在后台我没有得到警报。当我打开应用程序时,我会弹出所有警报。我想要的是在应用程序处于后台时获取它们。我想知道是否有可能或应用程序需要运行才能获取警报?任何帮助将不胜感激。IOS Geofencing后台警报

更新: 问题似乎是我用警报而不是本地通知。以下是我使用的代码:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    NSLog(@"Entered Region - %@", region.identifier); 
    [self showRegionAlert:@"You are near: " forRegion:region.identifier]; 
} 

如何将此更改为本地通知?

+2

在你的'Info.plist',你声明'location'为你的'UIBackgroundModes'? – yeesterbunny 2013-02-24 22:14:36

+0

其实我这样做,这是为什么我认为它仍然可以跟踪位置变化,以便当我运行应用程序时,我得到所有警报弹出一个接一个后,我只是不会弹出时,应用程序运行在后台 – XpApp 2013-02-24 22:34:48

+1

时你说你'没有得到警报',你的意思是你在地标到达时产生UIAlertViews?这将无法正常工作,因为您无法在后台显示警报。你应该使用UILocalNotifications如果你还没有 – 2013-02-25 00:15:48

回答

7

入住

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

如果您来回切换前景色和背景的阈值条件之间的“测试您的应用区域监视”一节可能无法得到满足,触发你把应用推到前再次前景。

此外,当后台应用获取通知时,只有一个小窗口用于处理消息。试图做网络请求可能会超时...

检查你的plist设置 - 声明位置为UIBackgroundModes只需要如果您需要高精度定位。即使没有定义位置,重要的位置变化也能起作用

检查locationManager:didUpdateLocations:和locationManager:didFailWithError:正在被调用,并且没有发布错误。

检查您是否在您的plist中将ApplicationRunsInBackground设置为NO。

尝试实施AppDelegates applicationDidEnterBackground :,应用程序:didFinishLaunchingWithOptions:和朋友来确定您在给定时间的应用生命周期中的哪个位置。

+1

感谢您的回答。其实我读了这个,这里是我的问题,当在后台应用程序监视区域,我甚至可以在控制台中看到它,但问题是,我没有得到任何警报,而应用程序是在后台。当我开始时,我得到了在后台发生的所有事件的警报 – XpApp 2013-02-24 23:01:39

+0

我用更多的指针更新了答案。 – 2013-02-25 10:02:12

+0

其实问题似乎是,我用UIAlert,而不是本地通知,我更新我的问题与我使用的代码,你知道我可以切换到localnotifications而不是警报? – XpApp 2013-02-25 15:14:34

0
  1. 对于地理围栏解决方案,您可以参考@Niels Castle给出的答案。
  2. 对于本地通知,你可以参考下面的代码:
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 

localNotification.alertBody = @"I'M IN THE REGION"; 

localNotification.userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:@"nearBy",@"type", nil]; 

localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

localNotification.soundName = UILocalNotificationDefaultSoundName; 

[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; 
0
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    NSLog(@"Yes! Welcome to %@",region.identifier); 
    UILocalNotification* notify = [UILocalNotification new]; 
    notify.alertBody = [NSString stringWithFormat:@"Welcome to %@",region.identifier]; 
    notify.soundName = UILocalNotificationDefaultSoundName; 
    if (notify.applicationIconBadgeNumber == 0) { 
     notify.applicationIconBadgeNumber = 1; 
    } 
    [[UIApplication sharedApplication] presentLocalNotificationNow:notify]; 
}