4

If I enter a region of a beacon with my Android device I like to show a notification. Thats well documented at Android SDK Quickstart

This is just working as long as the app is active. How do I get notifications when the app is closed?

erdna
  • 3,962
  • 3
  • 14
  • 9

4 Answers4

3

I build a service by myself:

BeaconRangingService.java

    public class BeaconRangingService extends Service {

        private static final String TAG = BeaconRangingService.class.getSimpleName();
        private BeaconManager beaconManager;

        @Override
        public void onCreate() {
            super.onCreate();
            beaconManager = BeaconManager.newInstance(getApplicationContext());
            beaconManager.setMonitorPeriod(MonitorPeriod.MINIMAL);
            beaconManager.setForceScanConfiguration(ForceScanConfiguration.DEFAULT);
            beaconManager.registerMonitoringListener(new BeaconManager.MonitoringListener() {
                @Override
                public void onMonitorStart() {
                    Log.v(TAG, "start monitoring beacons");
                }

                @Override
                public void onMonitorStop() {
                    Log.wtf(TAG, "stop monitoring beacons");
                }

                @Override
                public void onBeaconsUpdated(Region region, List<Beacon> list) {

                }

                @Override
                public void onBeaconAppeared(Region region, Beacon beacon) {
                   Toast.makeText(getApplicationContext(), "Beacon appeared\n BEACON ID: " + beacon.getBeaconUniqueId(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onRegionEntered(Region region) {

                }

                @Override
                public void onRegionAbandoned(Region region) {

                }
            });

        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            Log.v(TAG, "service started");

            if (!beaconManager.isBluetoothEnabled()) {
                Log.w(TAG, "bluetooth disabled, stop service");
                stopSelf();
            } else {
                connect();
            }

            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onDestroy() {
            Log.v(TAG, "service destroyed");
            beaconManager.stopMonitoring();
            beaconManager.disconnect();
            beaconManager = null;
            super.onDestroy();
        }

        private void connectBeaconManager() {
            try {
                beaconManager.connect(new OnServiceBoundListener() {
                    @Override
                    public void onServiceBound() {
                        try {
                            HashSet<Region> regions = new HashSet<>();
                            regions.add(Region.EVERYWHERE);
                            beaconManager.startMonitoring(regions);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (RemoteException e) {
                throw new IllegalStateException(e);
            }
        }

    }     

AndroidManifest.xml

 <service
            android:name="com.your.package.BeaconRangingService"
            android:exported="false"/>

start service

 Intent intent = new Intent(this, BeaconRangingService.class);
 startService(intent);
erdna
  • 3,962
  • 3
  • 14
  • 9
  • sorry do you remember how to use this code? I get no notification using it.. I tried to call the intent when my Activity goes onPause() but It doesn't work.. – LS_ May 12 '15 at 13:52
  • Hi erdna. i am also not getting any Log by using this code can you share some sample. – Mr Nice May 19 '15 at 12:04
  • I think in the above code there is no notification invoked. So, you should invoke it in 'onBeaconAppeared' method. – JackAW Jun 16 '15 at 13:27
1

I know nothing regarding kontakt.io but there is a library and reference app for AltBeacon which provides this functionality. https://github.com/AltBeacon

I believe this is generic Android functionality, and not anything magic to their implementation.

usernotdev
  • 117
  • 1
  • 7
  • Thanks for the hint. But I definitely have to use the Library from [kontakt.io](http://docs.kontakt.io/). We also bought the beacons from them. – erdna Jan 26 '15 at 08:00
  • 1
    @erdna altbeacon does not prevent you from using kontakt.io beacons. It just do not provide ways to modify their beacons. By the way I think both libraries does not correctly handle background monitoring. An always running service on Android is 99% of the times a bad idea. – Daniele Segato Jul 23 '15 at 11:48
0

@erdna have you tested the sample kontakt admin app? There is an approach in which Toasts appear when the application is in foreground and standard notifications appear when the app is working in background.

https://github.com/kontaktio/kontakt-beacon-admin-sample-app

dawid gdanski
  • 2,432
  • 3
  • 21
  • 29
0

@erdna I guess that it should be IntentService for long running services, that monitors incoming beacon events as intents in a onHandleIntent(Intent intent) method. @dawid-gdanski I'm not sure what class do you refer to.

JackAW
  • 164
  • 2
  • 14