Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: No scanning when app is killed #228

Open
usichenko-nikita opened this issue Oct 20, 2021 · 6 comments
Open

Android: No scanning when app is killed #228

usichenko-nikita opened this issue Oct 20, 2021 · 6 comments

Comments

@usichenko-nikita
Copy link

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?

@dmontecillo
Copy link

dmontecillo commented Oct 22, 2021

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.

@dmontecillo
Copy link

dmontecillo commented Nov 9, 2021

You can use mine spurdow@86222d6. 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" />
        ```

@usichenko-nikita
Copy link
Author

usichenko-nikita commented Nov 11, 2021

@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

@dmontecillo
Copy link

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:

  1. No, if app is killed by the user. - You should tell the user that killing the app will not perform better.
  2. 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.

@AjeyBS
Copy link

AjeyBS commented Feb 15, 2022

@usichenko-nikita Hi, This article has detailed how to get foreground service work on RN.

@Srikanth-Enuguru
Copy link

Srikanth-Enuguru commented Aug 3, 2023

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants