SDK

Location Services

In this page you'll dive deeper into functionality like using GPS signals to get the user's location or monitor their visits to regions and proximity to BTLE devices. These services will bring a new level of contextuality to your app, allowing you to create geo-triggers to send notifications or categorize users based on their location behaviour.

As mentioned in the Implementation page, if you are going to use location services, you must include the following permissions in your /android/app/source/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Requesting Permission

Additionally to these manifest permissions, since Android 6 (a.k.a Marshmallow), applications will need to request the user permission to use location. These permissions need to be requested in your app, after which you can safely enable location updates. To make sure the location updates aren't started before the library is ready to be used or a device has been registered, as shown below:

...more code
DeviceEventEmitter.addListener('didReceiveDeviceToken', function(e: Event) {

    Notificare.registerDevice(e.device, null, null, (error, msg) => {

      if (!error) {
        (async function() {
          try {
            let granted = await PermissionsAndroid.requestPermission(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
              'title': 'Location Permission',
              'message': 'We need your location so we can send you relevant push notifications'
            });
            if (granted) {
              Notificare.enableLocationUpdates()
            }
          } catch (err) {
            //Handle error
          }
        }());
      }

    });

});

Once you have implemented the code above, if permission is granted, our library will automatically collect the user location and start monitoring for regions you've created via the dashboard or API.

Using Bluetooth Low-Energy beacons

Once you've implemented GPS location in your app, you can also listen to Bluetooth signals from BTLE beacons in your app. Our library will require you to also include the AltBeacon library in your app. Simply add the following to your /android/app/build.gradle file:

dependencies {
    ...more
    compile 'org.altbeacon:android-beacon-library:2.12.2'
}

After that you can enable beacons signals by simply invoking the following method, right after a user has granted permission for tracking location:

public class MyMainActivity extends ActionBarBaseActivity implements Notificare.OnNotificareReadyListener {

    ..more code

    Notificare.registerDevice(e.device, null, null, (error, msg) => {

      if (!error) {
        (async function() {
          try {
            let granted = await PermissionsAndroid.requestPermission(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
              'title': 'Location Permission',
              'message': 'We need your location so we can send you relevant push notifications'
            });
            if (granted) {
              Notificare.enableLocationUpdates();
              Notificare.enableBeacons();
            }
          } catch (err) {
            //Handle error
          }
        }());
      }

    }

    ...more code

}

By doing this, you app will start monitoring for any beacons you inserted via the dashboard or API, in any of your regions.

By default, the app will scan at least every 5 minutes in the background. If you want your app to be more responsive, e.g. once per minute, you can set the interval yourself. Be aware, though, that a shorter interval will mean more power consumption.

Notificare.enableBeacons(60000);

Disable Location

Pretty much the same you enable location, you can also stop tracking the user location by invoking the following method:

Notificare.disableNotifications();

Disable Beacons

Although beacons will not work without using location, you can also stop listening to beacon advertising signals by invoking the following method:

Notificare.disableBeacons();