react-native-beacons-manager
react-native-beacons-manager copied to clipboard
Android: No scanning when app is killed
Version
"react-native-beacons-manager": "git://github.com/MacKentoch/react-native-beacons-manager" "react-native": "0.64.0",
Platform
Android
Expected behavior
The app continue detecting beacons even if app is killed
Actual behavior
The scan stops when app is killed
It works correctly on IOS but It does not work on android.. Can someone provide the solution?
Same issue here. I used react-native-background-actions but so far on my tests it receives empty beacon when in background mode in Android.
You can use mine https://github.com/dmontecillo/react-native-beacons-manager/commit/86222d60244c748bc58710f57ae70553b017ecf1. How to add in your package.json
"react-native-beacons-manager": "git://github.com/dmontecillo/react-native-beacons-manager.git",
then yarn
I wasn;t yet able to add readme and on how to configure foreground service due to time constraint , but I can give you what i used: Its important to execute start foreground first if you want to scan endlessly.
const notifData = {
channelId: "Channel id",
notificationId: 11111,
contentTitle: title,
contentMessage: message,
drawableIcon: "ic_launcher",
showWhen: true,
priority: "",
visibility: "",
}
console.log("Initiating foreground service... " , notifData)
Beacons.startForegroundService(notifData)
.then(result => {
console.log("Foreground service started successfully: ", result)
Beacons.startMonitoringForRegion(region)
.then(async () => {
console.log('Beacons monitoring started succesfully')
await Beacons.startRangingBeaconsInRegion(region)
Beacons.BeaconsEventEmitter.addListener('beaconsDidRange', (data) => {
console.log("Beacon Ranging .... " , data.beacons)
})
console.log('Beacon ranging started within region ', region)
})
.catch(error => {
console.log(`Beacons monitoring not started, error: ${error}`);
});
})
.catch( err => {
console.log("Error: ", err)
})
And make sure you have something like the below as well to support chinese or forked oems.
"react-native-autostart": "git://github.com/dmontecillo/react-native-autostart",
And last prerequisite is the below make sure you have this on your manifest.
<service android:name="org.altbeacon.beacon.service.BeaconService" android:foregroundServiceType="location"
tools:node="replace">
<meta-data android:name="longScanForcingEnabled" android:value="true"/>
</service>
<service android:name="org.altbeacon.beacon.service.ScanJob" android:permission="android.permission.BIND_JOB_SERVICE" />
```
@dmontecillo Thanks for your comment, but I have some questions.. first of all I tried to use your code and get eternal push notification with data that I have set to notifData obj and it is not very understandable how should I use it. Because in usual ios case I use Beacons.BeaconsEventEmitter.addListener('regionDidEnter', data => {...}) and when regionDidEnter I just sent push notification (and looks like you create your own push notifications). But in your case it look like you sent push notification before startMonitoringForRegion initialization. Questions: Am I right that it should work even when app is killed? Could you provide some more information why we need set notifData and how use it correct? If you can write more details about android implementation, please. Thanks
Hello @usichenko-nikita To summarize the purpose of the foreground service and notification is In Android if you want a long running service foregound notification more from here is required unless if you want to use background service(which i understand as the default behaviour) then you don't need to execute the foreground service method.
To answer your questions:
- No, if app is killed by the user. - You should tell the user that killing the app will not perform better.
- notifData is for the notification service information - you can read more from here
Sorry but for now due to time constraint to my project and since I was implementing it rapidly, I cannot provide you but I will try this week if i can create one separate app and public it in github.
@usichenko-nikita Hi, This article has detailed how to get foreground service work on RN.
@usichenko-nikita @dmontecillo @AjeyBS Using the code changes sugegsted by @dmontecillo in Android beacons scanning is working ONLY for a couple of minutes, after the app is killed.
Is this expected behavior? We want to run the service continuously even the app is killed. Is this possible?
But in iOS, when I close the app, am unable to see the logs in the console. I doubt that beacons scannig is not working in iOS when app is killed. Any idea how to get this working? Please help me