半年前在幫朋友阿強研究iBeacon,發現iOS在這方面比Android限制還多!首先來看iBeacon背景偵測流程,是跟前景偵測有何不一樣?
App在前景時,會陸續觸發三支API,didEnterRegion、didRangeBeacons、didExitRegion,如上圖從時間點A到時間點B。
接下來看App在背景時,跟前景不一樣在哪?
其中一支API不會觸發,就是前景時會多次觸發didRangeBeacons,在背景時卻無動於衷!?
於是僅能由進入與離開偵測範圍來進行我們的事件。
後來我發現有個變數設定:
/*
* notifyEntryStateOnDisplay
*
* Discussion:
* App will be launched and the delegate will be notified via locationManager:didDetermineState:forRegion:
* when the device’s screen is turned on and the user is in the region. By default, this is NO.
*/
@property (nonatomic, assign) BOOL notifyEntryStateOnDisplay;
它定義為當iOS設備螢幕亮起狀態下(背景或前景皆可),又偵測到iBeacon,就會觸發didRangeBeacons。
以上兩張流程圖可以說明概念。
這裡聊一下秘訣,我在didEnterRegion使用Local Notification,在背景時就會收到推播,此時螢幕亮起來,接著會不斷觸發didRangeBeacons,若是在didRangeBeacons繼續使用Local Notification,App又在背景之下,就會不斷收到推播。於是乎,你知道的,永不停歇的推播,直到你離開偵測範圍觸發didExitRegion。哈哈~
參考:iBeacon detection rate on iOS、Is it possible to use beacon ranging in the background?。
隨意留個言吧:)~