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?
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?
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);
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.
@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
@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.