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.

If you are going to use location services, you must include the notificare-location dependency in your /app/build.gradle:

dependencies {
    ...more
    implementation "re.notifica:notificare-location:VERSION"
}

Where VERSION should match the version of the Notificare's React Native module you are using, according to the table below:

RN Plugin VersionVERSION
2.2.22.2.1
2.2.12.2.1
2.2.02.2.0
2.1.32.1.2
2.1.22.1.2
2.1.12.1.1
2.1.02.1.1

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:

DeviceEventEmitter.addListener('ready', (data) => {

          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.startLocationUpdates()
            }
          } 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. This will require you to also include the notificare-beacon dependency in your app. Simply add the following to your /android/app/build.gradle file:

dependencies {
    ...more
    implementation "re.notifica:notificare-beacon:VERSION"
}

Where VERSION should match the version of the Notificare React Native module you are using, according to the table below:

RN Plugin VersionVERSION
2.2.22.2.1
2.2.12.2.1
2.2.02.2.0
2.1.32.1.2
2.1.22.1.2
2.1.12.1.1
2.1.02.1.1

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

DeviceEventEmitter.addListener('ready', (data) => {

    ..more code

          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.startLocationUpdates();
              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.

Beacon scanning with a foreground service

In Oreo and up, background scans are more limited. First scans of a beacon in a region will come in very quickly, but detection of changes or leaving the beacon's range will take up to 15 minutes when in background. This limitation is posed by Android itself and there is no workaround to do this in background mode.

The only way to have your app scan for beacons more often on Android version Oreo and up, is by starting the scan as a foreground service. Foreground services will be shown to the user as an ongoing notification. The Notificare SDK provides you with a default notification which will show a text, a progress indicator and a Cancel action to allow the user to stop the foreground scan.

It can be started (and stopped) at any time, but will only be shown if enableBeacons is already called.

Notificare.enableBeaconForegroundService();
Notificare.disableBeaconForegroundService();

You can customise the text by translating the R.strings.notificare_beacon_scanning resource.

Disable Location

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

Notificare.stopLocationUpdates();

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();