alann-maulana / flutter_beacon

An hybrid iBeacon scanner and transmitter SDK for Flutter Android and iOS.
Apache License 2.0
115 stars 142 forks source link

Background monitoring on Android #45

Open snadal opened 4 years ago

snadal commented 4 years ago

Hi!

I’ve tried to enable background monitoring on Android, as suggested on #40 adding that to the example app.

However, as soon as app goes to background, it stops receiving zone changes.

Do I need to add intent or something special to Android app for background beacon detection to work?

Thank you!

alann-maulana commented 4 years ago

Hi @snadal

What Android phone do you use? I use it at my daily office projects, some devices need to add some lines to enable background monitoring directly on Java/Kotlin code within Android project.

snadal commented 4 years ago

Hi Alann,

I am trying with a Pixel 2 and a very old Samsung Galaxy S4, among others.

Could you please share the lines on your Java / Kotlin code?

Thank you!

snadal commented 4 years ago

Hello,

Would you mind to share the lines you used to enable background monitoring on Android?

Thanks!

wescarr commented 4 years ago

@alann-maulana I'm having the same issue and don't get monitoring events on either Android or iOS when the app has been terminated. I see that the https://pub.dev/packages/beacons package has support for background monitoring but it looks like it's been abandoned and no longer works properly...

alann-maulana commented 4 years ago

Hi, sorry for delaying response..

In Android, I'm implementing BootstrapNotifier on my custom Application class. I set the region without UUID, major or minor. So when it detect beacon it will trigger the app to start again. Don't forget to make your MainActivity's android:launchMode to singleInstance.

In iOS, from Estimote community :

Even if the app is not running, location events (related to the beacons in this case) are handled the same way as any other app launching events. Every time a phone enters or exits a region while the app is terminated, it will be automatically launched.

application:didFinishLaunchingWithOptions: method (of AppDelegate class) is called with UIApplicationLaunchOptionsLocationKey key existing in launchOptions parameter.

Please read the full article here. You can put your iOS code at application:didFinishLaunchingWithOptions: to waking your app.

For both iOS and Android, you can silently send a push notification that trigger app to start scanning, if it is terminated.

Sometimes I use all of them, sometimes only one or two of it. Find what best suite for your case.

I hope this can helps.

Regards.

wescarr commented 4 years ago

@alann-maulana Thanks for your response. Do you have any plans to provide some more concrete code examples for both platforms? I'm unfortunately pretty new to native app development, and while I have a decent grasp of Flutter, my knowledge doesn't extend much beyond that. I do appreciate the pointers in the right direction.

darshithedpara commented 4 years ago

@alann-maulana I am also stuck in beacon-scanning for Android and IOS while app is in background. Can you please make one example like this https://pub.dev/packages/beacons package.

AlirezaDaryani commented 4 years ago

Hi, sorry for delaying response..

In Android, I'm implementing BootstrapNotifier on my custom Application class. I set the region without UUID, major or minor. So when it detect beacon it will trigger the app to start again. Don't forget to make your MainActivity's android:launchMode to singleInstance.

In iOS, from Estimote community :

Even if the app is not running, location events (related to the beacons in this case) are handled the same way as any other app launching events. Every time a phone enters or exits a region while the app is terminated, it will be automatically launched. application:didFinishLaunchingWithOptions: method (of AppDelegate class) is called with UIApplicationLaunchOptionsLocationKey key existing in launchOptions parameter.

Please read the full article here. You can put your iOS code at application:didFinishLaunchingWithOptions: to waking your app.

For both iOS and Android, you can silently send a push notification that trigger app to start scanning, if it is terminated.

Sometimes I use all of them, sometimes only one or two of it. Find what best suite for your case.

I hope this can helps.

Regards.

Hi, I have no issue in ios when app terminated and its work great. But, in android ,app can not scan beacon when terminated. any solution?

alann-maulana commented 3 years ago

maybe by migrating it to v2? #67

omerchen commented 3 years ago

Hi, sorry for delaying response.. In Android, I'm implementing BootstrapNotifier on my custom Application class. I set the region without UUID, major or minor. So when it detect beacon it will trigger the app to start again. Don't forget to make your MainActivity's android:launchMode to singleInstance. In iOS, from Estimote community :

Even if the app is not running, location events (related to the beacons in this case) are handled the same way as any other app launching events. Every time a phone enters or exits a region while the app is terminated, it will be automatically launched. application:didFinishLaunchingWithOptions: method (of AppDelegate class) is called with UIApplicationLaunchOptionsLocationKey key existing in launchOptions parameter.

Please read the full article here. You can put your iOS code at application:didFinishLaunchingWithOptions: to waking your app. For both iOS and Android, you can silently send a push notification that trigger app to start scanning, if it is terminated. Sometimes I use all of them, sometimes only one or two of it. Find what best suite for your case. I hope this can helps. Regards.

Hi, I have no issue in ios when app terminated and its work great. But, in android ,app can not scan beacon when terminated. any solution?

+1

@alann-maulana I'll glad to help you to migrate but my android native knowledge is pretty basic. :(

Marcoslima016 commented 3 years ago

Hello @alann-maulana, could you take some doubt on how is implemented the bootstrap notifier?  I'm not sure how to use my custom application class. Should I use the FlutterApplication class that comes set on android:name? or should I create another class and then set this new class on android:Name? I appreciate your numerous help with the community. Your package and explanations have saved me several times. Thank you

Marcoslima016 commented 3 years ago

Hi, after a few attempts I managed to implement the Custom Application class with bootstrap notifier. I'll try to make an explanation to help those who are having difficulty in this part. As soon as there's a time left, I'll post an example on github to make it easier to understand.

In the AndroidManifest file, you must modify the "android:name" parameter.

By default it is set to FlutterApplication, that would be something like "io.flutter.app.FlutterApplication". You should remove this value and replace it with the name of your custom application class, for example ".CustomApplication". So press option + enter to create this new class ( On android studio ), after that you should make some modifications to the class created to implement Bootstrap notifier. After implementing bootstrap notifier, your new class will look like this:

public class CustomApplication extends FlutterApplication implements BootstrapNotifier, BeaconConsumer, PluginRegistry.PluginRegistrantCallback {

    private RegionBootstrap regionBootstrap;
    BeaconManager beaconManager;

    @Override
    public void registerWith(PluginRegistry registry) {
        // GeneratedPluginRegistrant.registerWith(registry);
        FlutterBeaconPlugin.registerWith(registry.registrarFor("io.flutter.plugins.flutterbeacon.FlutterBeaconPlugin"));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        FlutterMain.startInitialization(this);

        beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);

        beaconManager.bind((BeaconConsumer) this);

        Region region = new Region("region", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);

        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("region", null, null, null));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void didEnterRegion(Region region) { }

    @Override
    public void didExitRegion(Region region) { }

    @Override
    public void didDetermineStateForRegion(int i, Region region) { }

    @Override
    public void onBeaconServiceConnect() { }

    private Activity mCurrentActivity = null;

    public Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    public void setCurrentActivity(Activity mCurrentActivity) {
        this.mCurrentActivity = mCurrentActivity;
    }
}

And the androidManifest file will look something like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.flutterbeaconexample">

    <application
        android:name=".CustomApplication"
        android:label="Flutter Beacon"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleInstance"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <meta-data
                android:name="io.flutter.embedding.android.SplashScreenDrawable"
                android:resource="@drawable/launch_background"
                />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
evanblasband commented 3 years ago

@Marcoslima016 I am facing the same issue, any update on whether you have posted that example somewhere?

EDIT: I got what you did above working but it seems to disable the implementation I have in flutter, did you see this too? is the idea to implement the functionality in the .CustomApplication entirely using the altBeacon library?

Marcoslima016 commented 3 years ago

hi @evanblasband, soon I put a complete example of an Android application working in the background. I'm busy, but I'm going to have some time to do it soon.

alan2030 commented 2 years ago

hi @evanblasband, soon I put a complete example of an Android application working in the background. I'm busy, but I'm going to have some time to do it soon.

Hi @Marcoslima016, I encountered the same issue as you. Could you please put a complete example as you mentioned?

ElKood-Sol commented 2 years ago

Yes Please

hi @evanblasband, soon I put a complete example of an Android application working in the background. I'm busy, but I'm going to have some time to do it soon.

Hi @Marcoslima016, I encountered the same issue as you. Could you please put a complete example as you mentioned?

official-ravi commented 1 year ago

hi @evanblasband, soon I put a complete example of an Android application working in the background. I'm busy, but I'm going to have some time to do it soon.

Hi @Marcoslima016,

I encountered the same issue as you. Could you please put a complete example as you mentioned?

devberkay commented 1 year ago

Hi @Marcoslima016,

I encountered the same issue as you. Could you please put a complete example as you mentioned?