简体   繁体   English

应用程序未运行时的iBeacon通知

[英]iBeacon Notification when the app is not running

I managed to make an iBeacon which triggers a local push notification on my iPhone when the beacon is in range. 我设法制作了一个iBeacon,当该信标在范围内时,它会在我的iPhone上触发本地推送通知。 It's perfectly working when the app is in background mode. 当应用程序处于后台模式时,它可以完美运行。

My question is: Can I trigger the notification even when the app is not running, not even in the background? 我的问题是:即使应用未运行,甚至不在后台,我都可以触发通知吗?

I thought this was possible but I'm not sure. 我以为这是可能的,但我不确定。 If so, how can I accomplish this? 如果是这样,我该怎么做?

Thanks! 谢谢!

Yes, it's possible and should be automatic. 是的,这是可能的,应该是自动的。

After you have created a CLBeaconRegion and started monitoring on it, Location Services will keep track of whether your phone is in or out of the region, even when your app isn't running. 创建CLBeaconRegion并开始对其进行监视之后,即使您的应用程序未运行,Location Services也会跟踪您的手机在该区域内还是不在该区域之外。 If you app isn't running during a transition, iOS will launch your app into the background for a few seconds to call the appropriate CLLocationManagerDelegate methods. 如果您的应用程序在过渡期间没有运行,iOS将在后台启动您的应用程序几秒钟,以调用适当的CLLocationManagerDelegate方法。

I found out the above behavior through experimentation with my own app, but have also witnessed it with Apple's AirLocate sample program. 我通过试验自己的应用程序发现了上述行为,但还通过Apple的AirLocate示例程序进行了见证。 With AirLocate, if you set up a monitoring region then reboot your phone, AirLocate will still deliver a local notification as soon as the phone enters the region. 使用AirLocate,如果您设置了监视区域,然后重新启动电话,则AirLocate仍会在电话进入该区域后立即发出本地通知。

Take care when testing this, because sometimes it takes up to 4 minutes after turning on/off an iBeacon before iOS recognizes the region state transition. 测试时要小心,因为有时在打开/关闭iBeacon之后最多需要4分钟才能使iOS识别区域状态转换。 EDIT : As of the iPhone 5, apps will typically use hardware acceleration to detect beacons within a few seconds, and if hardware acceleration is not available, it can take up to 15 minutes. 编辑 :从iPhone 5开始,应用程序通常会在几秒钟内使用硬件加速来检测信标,如果硬件加速不可用,则可能最多需要15分钟。

EDIT 3: AS of iOS 13, you must make sure the user actually grants background permission and not "only once" or "when in use" permission which are heavily pushed by the operating system in the dialogs they present to the user. 编辑3:在iOS 13上,您必须确保用户实际上授予了后台权限,而不是“仅一次”或“使用时”权限,这些权限由操作系统在向用户显示的对话框中大量推送。 See here for details. 有关详细信息,请参见此处

EDIT 2: As of iOS 8, you need to make sure you have called and successfully obtained locationManager.requestAlwaysAuthorization() as locationManager.requestWhenInUseAuthorization() only lets beacons be detected in the foreground. 编辑2:从iOS 8开始,您需要确保已调用并成功获取locationManager.requestAlwaysAuthorization()作为locationManager.requestWhenInUseAuthorization()仅允许在前台检测信标。

I have posted a detailed discussion on how this all works in this blog post. 我在此博客文章中发布了有关这一切如何工作的详细讨论

OK I've gotten this to work correctly and experimented around with it so here is the answer. 好的,我已经使它能够正常工作并对其进行了实验,因此这就是答案。 This is what you need to do to get your app to be invoked when crossing a beacon region boundary after the app has been terminated (assuming your app works properly when in the foreground): 这是在终止应用程序后越过信标区域边界时要使应用程序被调用所需的操作(假设您的应用程序在前景中运行正常):

  1. You must implement a CLLocation delegate inside your AppDelegate.m module . 您必须AppDelegate.m模块内实现CLLocation委托。 This delegate is what gets invoked by iOS so if you don't have the CLLocation delegate code in AppDelegate.m , you won't be able to respond to iOS when your app has been terminated. 此委托是由iOS调用的,因此,如果您在AppDelegate.m没有CLLocation委托代码,则您的应用终止后将无法响应iOS。 This is what Apple's AirLocate sample app does. 这就是Apple的AirLocate示例应用程序所做的。

So, inside AppDelegate.m you need the following (you also need to link in CoreLocation.h ): 因此,在AppDelegate.m您需要执行以下操作(还需要在CoreLocation.h进行链接):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

// This location manager will be used to notify the user of region state transitions when the app has been previously terminated.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
return YES;
}
  1. Inside AppDelegate.m , you need to implement the locationManager didDetermineState method, like this: AppDelegate.m内部,您需要实现locationManager didDetermineState方法,如下所示:

     -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{ UILocalNotification *notification = [[UILocalNotification alloc] init]; if(state == CLRegionStateInside) { notification.alertBody = [NSString stringWithFormat:@"You are inside region %@", region.identifier]; } else if(state == CLRegionStateOutside) { notification.alertBody = [NSString stringWithFormat:@"You are outside region %@", region.identifier]; } else { return; } [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; } 

--> So if your app has been terminated (it must be run at least ONCE), when the device transitions across a beacon boundary that you are monitoring, iOS will invoke your app and call the locationManager:didDetermineState method in your AppDelegate.m module. ->因此,如果您的应用已终止(必须至少运行一次),则当设备跨您监视的信标边界转换时,iOS将调用您的应用并在AppDelegate.m中调用locationManager:didDetermineState方法模块。 Inside this method you can then set up and call presentLocalNotificationNow. 然后,可以在此方法内设置并调用presentLocalNotificationNow。 If your app is NOT in the foreground when this happens, iOS will present the notification on the screen even if it is locked. 如果发生这种情况时您的应用程序不在前台,则iOS将在屏幕上显示通知,即使该通知已被锁定。 The user will then have to invoke the app for more information. 然后,用户将不得不调用该应用程序以获取更多信息。

I'm pretty sure that memory pressure has nothing to do with this. 我很确定内存压力与此无关。 Also, the setting notifyEntryStateOnDisplay has nothing to do this this issue either. 另外,设置notifyEntryStateOnDisplay也没有与此问题相关的内容。 Setting notifyEntryStateOnDisplay is only used when the user turns on the iOS device display (ie hits "home" or top left button). 设置notifyEntryStateOnDisplay仅在用户打开iOS设备显示屏(即,单击“主页”或左上角按钮)时使用。 If the user does this and notifyEntryStateOnDisplay is TRUE , AND the device is INSIDE the beacon region you are monitoring for, THEN you get a notification on the display at that time. 如果用户执行此操作,并且notifyEntryStateOnDisplayTRUE ,并且设备位于要监视的信标区域内,则此时您将在显示屏上收到通知。 If this property is set to FALSE , you don't. 如果将此属性设置为FALSE ,则不会。

Of course, you need to be running iOS 7.1 in order for this stuff to work correctly. 当然,您需要运行iOS 7.1才能正常工作。

For more details, visit Apple's documentation 有关更多详细信息,请访问Apple的文档。

You need to switch notifyEntryStateOnDisplay=YES for CLBeaconRegion for the system to wake your app for iBeacon entry/exit event. 您需要为CLBeaconRegion切换notifyEntryStateOnDisplay = YES,以使系统为iBeacon进入/退出事件唤醒您的应用程序。

But there is one tricky part. 但是有一个棘手的部分。 If your app is not running the system will only wake your app for beacon entry/exit handling if your app was terminated previously due to system memory pressure. 如果您的应用程序未在运行, 那么如果您的应用程序先前由于系统内存不足而终止,则系统只会唤醒您的应用程序以进行信标进入/退出处理 If the user kills the app by swiping it up in the task view, the system will not wake your app. 如果用户通过在任务视图中向上滑动来杀死应用程序,则系统将不会唤醒您的应用程序。 To verify this behaviour, launch you app, put it to background, then consecutively launch several memory consuming apps. 要验证此行为,请启动您的应用程序,将其置于后台,然后连续启动多个消耗内存的应用程序。 I launched several 3D games before my app gets terminated by the system due to memory pressure. 在我的应用由于内存压力而被系统终止之前,我启动了一些3D游戏。

Just upgrade your iOS version to 7.1 and set "notifyEntryStateOnDisplay=YES" and it should work like a charm even when your app is not running. 只需将您的iOS版本升级到7.1并设置“ notifyEntryStateOnDisplay = YES”,即使您的应用未运行,它也应该像超级按钮一样工作。 I was having this problem earlier but it got fixed once I did this upgrade! 我之前有这个问题,但是一旦完成升级,它就可以解决! Enjoy.. 请享用..

The only way I have been able to make this work is by monitoring for major location changes which seem to do the trick. 我能够进行这项工作的唯一方法是监视似乎可以解决问题的主要位置更改。 Be warned I have not tested this for all of the device or use case scenarios. 请注意,我尚未针对所有设备或用例场景对此进行测试。

Yes, we can present the local notification in kill state or in the background state, just follow the steps, 是的,我们可以按照杀死状态或后台状态显示本地通知,只需按照以下步骤操作,

1) Start location manager using CLLocationManager class. 1)使用CLLocationManager类启动位置管理器。

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;

2) Create CLBeaconRegion like, 2)创建CLBeaconRegion,

CLBeaconRegion *beacon_Region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:mjorVa minor:minorVa identifier:identifier];
beacon_Region.notifyEntryStateOnDisplay = YES;
beacon_Region.notifyOnEntry=YES;
beacon_Region.notifyOnExit=YES;

3) Implement two location manager delegate method like, 3)实现两个位置管理器委托方法,

-didEnterRegion
-didExitRegion

The above two location manager method will work even your app is kill or in background. 即使您的应用被杀死或在后台运行,上述两个位置管理器方法也可以使用。 The system will keep track of your beacon and when it goes out of the range the system will fire the didExitRegion method and when comes in the system will fire the didEnterRegion method. 系统将跟踪您的信标,当其超出范围时,系统将触发didExitRegion方法,而当其进入系统时将触发didEnterRegion方法。

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

相关问题 iBeacon:在运行应用程序时,没有调用didRangeBeacons - iBeacon: didRangeBeacons is not getting called when running app 有时,当应用程序被ios中的用户杀死时,应用程序不提供iBeacon通知 - Sometimes App is not giving notification of iBeacon when app is kill by user in ios iBeacon和通知 - iBeacon and Notification 当app在后台时,使用iBeacon进行广告宣传 - Advertising with iBeacon when app is in background 用户终止应用程序时发出iBeacon通知(通过在任务视图中向上滑动它) - iBeacon Notification when the app has been terminated by user (by swiping it up in the task view) 在没有iBeacon的情况下实现地理围栏时,当应用未运行时,didEnterRegion和didDetermineState不被调用 - didEnterRegion & didDetermineState not being called when the app is not running while implementing Geofencing without iBeacon 不运行且iBeacon在范围内时调用应用程序 - invoking application when not running and iBeacon in range 当应用未运行时,不会在iOS上触发`on('notification')` - `on('notification')` is not triggered on iOS when app is not running 在应用未运行时推送通知数据? - Push notification data when app is not running? 应用未运行时基于位置的推送通知? - location based push notification when app is not running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM