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:

1
2
3
4
dependencies {
	...more
	implementation "re.notifica:notificare-location:2.1.1"
}

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:

1
2
3
4
5
dependencies:
  flutter:
	sdk: flutter
  notificare_push_lib: ^2.0.0
  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:

1
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
... more code
class DemoPage extends StatelessWidget {
   startLocationUpdates() {
	 try {
	  PermissionStatus permission = await LocationPermissions()
		  .requestPermissions();
	  if (permission == PermissionStatus.granted) {
		notificare.startLocationUpdates();
	  }
	} catch (err) {
	  //Handle error
	}
   }

   @override
   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:

1
2
3
4
5
6
7
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:

1
2
3
4
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MyMainActivity extends ActionBarBaseActivity implements Notificare.OnNotificareReadyListener {

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

Disable Location

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

1
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:

1
notificare.disableBeacons();