react-native-beacons-manager
react-native-beacons-manager copied to clipboard
iOS montioring when the app is killed/in background mode does not work
Version
1.0.7
Platform
iOS
OS version
iOS 11.0.2
Steps to reproduce
listener = DeviceEventEmitter.addListener(
'beaconsDidRange',
(data) => {
PushNotification.localNotificationSchedule({
message: 'Did Detect Beacon', // (required)
date: new Date(Date.now())
})
}
)
Beacons.requestAlwaysAuthorization()
Beacons.startMonitoringForRegion(region)
Beacons.startRangingBeaconsInRegion(region)
Beacons.startUpdatingLocation()
Expected behavior
Beacon should be detected also when the app is killed/in background mode
Actual behavior
The listener callback does not get called (it does get called on foreground)
I did try to add:
Beacons.setBackgroundBetweenScanPeriod(1)
which using it I sometimes get push on background (I get inconsistent behaviour – sometimes I do get, sometimes I don’t), but still - not at all when the app is killed.
Also, I now get the following warning:
TypeError:_reactNativeBeaconsManager2.default.setBckgroundBetweenScanPeriod is not a function`
Thank you @oferRounds for this detailed issue.
Master branch contains a merged PR to try to fix these inconsistencies in iOS (drop of DeviceEventEmitter
in favor of Beacons.BeaconsEventEmitter
: see CHANGELOG).
Would you give a try (hoping it solves it)?
Get master branch version:
npm install git+https://github.com/MacKentoch/react-native-beacons-manager.git
I think the issue here is a typo. Check the spelling of background in the method you are calling!
On 16 Oct 2017, at 00:43, Erwan DATIN [email protected] wrote:
Thank you @oferRounds for this details issue.
Master branch contains a merged PR to try to fix these inconsistencies in iOS (drop of DeviceEventEmitter in favor of Beacons.BeaconsEventEmitter: see CHANGELOG).
Would you give a try (hoping it solves it)?
Get master branch version:
npm install git+https://github.com/MacKentoch/react-native-beacons-manager.git — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
@MacKentoch - thanks! Do you mean it suppose to suppose to solve the background/app killed issue?
@eurobob - oh, you’re right! Sorry for this :(
@oferRounds I was having issues with detection when the app is killed but have been having much more consistent behaviour with a small addition to the library. Try the code from my pull request and see how it works:
https://github.com/MacKentoch/react-native-beacons-manager/pull/58
Be aware that you should be able to detect when monitoring and ranging is working, but if you are in range of the beacon when starting the app you will not trigger the entry event from monitoring. I'm trying to find a workaround for this now.
Replied directly on issue #58.
So it now does works great for me in the background/foreground, but not when the app is killed. The problems are the app either only wakes up once, when it enters the region (i.e., I would like to have the control to detect the proximity and only do something on an immediate
event) or it doesn’t wake up at all.
@eurobob I saw some posts that mention that when the app wakes up, in the didFinishLaunchingWithOptions
we should reactivate the location manager. Is that true from what you aware of? If so, how can get the location manager’s instance?
@MacKentoch - I did the update, but I get the error: Property 'BeaconsEventEmitter' does not exist on type 'typeof 'react-native-beacons-manager'.
Any idea?
Can you share more code from the file you're working in?
Thanks! Sure:
import { DeviceEventEmitter, EmitterSubscription, Alert, Platform, AppState } from 'react-native'
import Beacons from 'react-native-beacons-manager'
import { toggleClock } from '../actions'
const PushNotification = require('react-native-push-notification')
const region = {
identifier: ***,
uuid: ***,
major: ***,
minor: ***
}
let didEnterRegionListener: EmitterSubscription = null
export default class TimeLogService {
static trackUserLocation(dispatch: any) {
if (didEnterRegionListener === null) {
didEnterRegionListener = Beacons.BeaconsEventEmitter.addListener(
'regionDidEnter',
() => {
if (AppState.currentState === 'active') {
Alert.alert(
'Toggle clock sent.',
'',
[
{text: 'OK', onPress: () => console.log('Cancel Pressed'), style: 'cancel'}
],
{ cancelable: false }
)
} else {
PushNotification.localNotification({
message: 'Toggle clock sent.'
})
}
dispatch(toggleClock())
}
)
if (Platform.OS === 'ios') {
Beacons.requestAlwaysAuthorization()
Beacons.startMonitoringForRegion(region)
Beacons.startRangingBeaconsInRegion(region)
Beacons.startUpdatingLocation()
} else {
Beacons.detectIBeacons()
try {
Beacons.startRangingBeaconsInRegion(region)
console.log(`Beacons ranging started succesfully!`)
} catch (error) {
console.log(`Beacons ranging not started, error: ${error}`)
}
}
}
}
}
It sounds like you might still be using v1.0.7? Or are you installing the package directly from the git repository?
I followed your suggestion and installed it by:
npm install git+https://github.com/MacKentoch/react-native-beacons-manager.git
I'm not sure what could be the problem then. Just to be thorough, maybe unlink it and uninstall the npm package, then try again?
You can also use my fork to include this pull request:
npm install git+https://github.com/eurobob/react-native-beacons-manager.git
That’s an idea! I’ll do that.
PR #58 is merged in master CHANGELOG
@oferRounds going back to the initial problem, i've found that in debugging mode sometimes the app can take a long time to load (bad wifi or whatever), which eats into the time window allocated to take action on the region entry from the monitoring. Perhaps you could try a production build to see if it works that way?
We had the same problem.
Adding
self.locationManager.allowsBackgroundLocationUpdates = YES;
to the init method of RNIBeacon.m solved the problem.
https://developer.apple.com/documentation/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates
maybe some function like
RCT_EXPORT_METHOD(allowsBackgroundLocationUpdates:(BOOL)allow)
{
self.locationManager.allowsBackgroundLocationUpdates = allow;
}
would be helpful. This would allow the developer to control, when ranging in background really needed.
Thank you @sign2k for sharing.
This is definitely a good fix to add.
As suggested by @sign2k, I added:
/**
* set background location updates to ensure monitoring when app is killed or in background mode
*
* @param {boolean} [allow=false] allow or disallow background modes
*/
function allowsBackgroundLocationUpdates(allow: boolean = false): void {
BeaconsManager.allowsBackgroundLocationUpdates(allow);
}
Please note this requires iOS9+ (it should not be a problem, since iPhone owners should be at iOS10 for more than 90%)
@MacKentoch, @sign2k
I’m facing the issue also in Android. I.e, on both platform, when the app is killed, the beacon is sometimes not detected and the app is not woken up. Otherwise — when in foreground or background - it works great.
Two questions please:
- Is using
allowsBackgroundLocationUpdates
should affect monitoring also? Anyhow, I’ll test it now and follow up with results. - Any solution to Android?
@eurobob any findings perhaps from your tests?
On iOS - I just added the code to AppDelegate.m according to Estimote guide, and now it works! Even when the app was killed by the user.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"])
{
self.beaconManager = [ESTBeaconManager new];
self.beaconManager.delegate = self;
// don't forget the NSLocationAlwaysUsageDescription in your Info.plist
[self.beaconManager requestAlwaysAuthorization];
[self.beaconManager startMonitoringForRegion:[[ESTBeaconRegion alloc]
initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
identifier:@"AppRegion"]];
}
return YES;
}
-(void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Enter region";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
-(void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Exit region";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
Do we want to address this in the library?
As for Android, I see in Estimote docs that the following scenario is indeed not handled
Keep in mind that if the app is killed by device memory requirements, it is possible to carry out background monitoring to relaunch it.
However, if the user killes the app background monitoring won't work.
Any idea for a workaround?
I see that Estimote added this feature in their new coming up SDK.
See here: https://forums.estimote.com/t/alpha-4-monitoring/7516
And here: https://github.com/Estimote/Android-SDK/issues/62#issuecomment-342519115
Can we add something like this here also? What do you think?
Hi All,
I enabled background update by setting BeaconsManager.allowsBackgroundLocationUpdates(true) in the beginning of componentWillMount
I would like to know if this suppose to work for only single region or multi region as I noticed when I am sending ranging information from multi region I will only get one region update instead of all of it. Or if I am doing something wrong so not all then not all region ranging update are send while in phone is locked.
Thank you :D
@MacKentoch , i tried v1.1 and background scanning not work. (i am in debug mode) do you have any idea?
version: "react": "16.3.1", "react-native": "0.55.4", "react-native-beacons-manager": "git+https://github.com/MacKentoch/react-native-beacons-manager.git"
Source:
import React, { Component } from 'react'; import { Platform, StyleSheet, Text, View } from 'react-native'; import Beacons from 'react-native-beacons-manager';
const instructions = Platform.select({ ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu', android: 'Double tap R on your keyboard to reload,\n' + 'Shake or press menu button for dev menu', });
type Props = {};
export default class App extends Component<Props> {
componentWillMount() {
Beacons.allowsBackgroundLocationUpdates(true)
Beacons.requestAlwaysAuthorization()
Beacons.shouldDropEmptyRanges(true);
const region = {
identifier: ***,
uuid: ***
};
Beacons.startMonitoringForRegion(region) // or like < v1.0.7: .startRangingBeaconsInRegion(identifier, uuid)
.then(() => console.warn('Beacons monitoring started succesfully'))
.catch(error =>
console.warn(Beacons monitoring not started, error: ${error}
),
);
Beacons.startRangingBeaconsInRegion(region) // or like < v1.0.7: .startRangingBeaconsInRegion(identifier, uuid)
.then(() => console.warn('Beacons ranging started succesfully'))
.catch(error =>
console.warn(Beacons ranging not started, error: ${error}
),
);
Beacons.startUpdatingLocation()
}
componentDidMount() {
this.authStateDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
'authorizationStatusDidChange',
info => console.warn('authorizationStatusDidChange: ', info),
);
// Ranging: Listen for beacon changes
this.beaconsDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
'beaconsDidRange',
data => {
console.warn(data)
}
);
// monitoring events
this.regionDidEnterEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidEnter',
({ uuid, identifier }) => {
console.warn('monitoring - regionDidEnter data: ', { uuid, identifier });
}
);
this.regionDidExitEvent = Beacons.BeaconsEventEmitter.addListener( 'regionDidExit', ({ identifier, uuid, minor, major }) => { console.warn('monitoring - regionDidExit data: ', { identifier, uuid, minor, major }); } ); }
render() { return ( <View style={styles.container}> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit App.js </Text> <Text style={styles.instructions}> {instructions} </Text> </View> ); } }
const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, });
I just solve it by change the privacy. as your suggested, both "location always use" + "location when in use" description must fill something to make it works in the plist!
We had the same problem.
Adding
self.locationManager.allowsBackgroundLocationUpdates = YES;
to the init method of RNIBeacon.m solved the problem.https://developer.apple.com/documentation/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates
maybe some function like
RCT_EXPORT_METHOD(allowsBackgroundLocationUpdates:(BOOL)allow) { self.locationManager.allowsBackgroundLocationUpdates = allow; }
would be helpful. This would allow the developer to control, when ranging in background really needed.
I was not able to see ranging in the background until I found your post. Thank you fine sir, this is exactly what I needed!
@oferRounds , can you show me what code did you use in your AppDelegate? Because I think you have pasted the one from Estimote but the needed for this library will be different. Thanks!