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:2.1.1" // make sure you always use the latest library
}

Tracking a user location in Android will require the user's authorization, this means that your app must request authorization for location services before it can use our library. To simplify how you request this permission, we recommend that you use a 3rd party plugin. Start by adding it to your app's pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  notificare_push_lib: ^2.0.0  // make sure you always use the latest library
  location_permissions: '^2.0.0'

Then in your project's file, where you are going to request this permission, make sure you import this package as follows:

import 'package:location_permissions/location_permissions.dart';

Location Updates

Location services can only be requested after a successful device registration. To make sure this happens correctly, the best place to request location services is right after a device registration:

notificare.onEventReceived.listen((NotificareEvent event) {
    if (event.name == "deviceRegistered") {
        //It would be safe to request it after this point
        try {
          PermissionStatus permission = await LocationPermissions()
              .requestPermissions();
          if (permission == PermissionStatus.granted) {
            notificare.startLocationUpdates();
          }
        } catch (err) {
          //Handle error
        }
    }
});

But in most cases you will want to start location updates only after a user clicks on a certain button after going through some on-boarding screen that explains why location updates are needed. In those cases you would implement something like this:

... more code
class DemoPage extends StatelessWidget {
   startLocationUpdates() {
     try {
      PermissionStatus permission = await LocationPermissions()
          .requestPermissions();
      if (permission == PermissionStatus.granted) {
        notificare.startLocationUpdates();
      }
    } catch (err) {
      //Handle error
    }
   }

   
   Widget build(BuildContext context) {
     return Scaffold(
       body: Center(
         child: RaisedButton(
           onPressed: startLocationUpdates,
           child: Text('Start Location Updates'),
         ),
       ),
     );
   }
}

The first time this is requested, it will prompt the user with a permission dialogue. If the user agrees to share location with your app, Notificare will automatically start monitoring significant location changes (roughly every 500m/1,640ft). It will also start monitoring for the nearest regions and beacons you might have created in our dashboard or API. This is basically all it takes to use location services, at this point you should be able to manage all your regions, geo-triggers, beacons and send geo-targeted notifications through our dashboard or API.

You will also want to change the way location updates are requested after the user has accepted it. To make sure a location update is requested right away when your app launches and a user has already given permission to do so, you will want to implement the following:

notificare.onEventReceived.listen((NotificareEvent event) async {
    if (event.name == "ready") {
        if (! await notificare.isLocationServicesEnabled()) {
            notificare.startLocationUpdates();
        }
    }
});

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:2.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:

class _MyAppState extends State<MyApp> {

    ..more code

        try {
          PermissionStatus permission = await LocationPermissions().requestPermissions();
          if (permission == PermissionStatus.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();