SDK

Location Services

In this page you'll dive deeper into how to use the browser's location to bring a new level of contextuality to your messages.

Requesting Permission

The use of the browser's location is dependent on the user authorization. Once authorized everytime the user opens your website or moves significantly while using your website, we will automatically collect the location and monitor visits to your regions. Unlike native mobile apps location can only be retrieve while the user is browsing your website.

To initiate location services in your website, you should request it after registering the device with Notificare, as shown below:

$("body").bind("notificare:didRegisterDevice", function(event, data) {

    ...more code

    instance.notificare("startLocationUpdates", function(data){
        //Location updates started
    }, function(error){
        //Handle error
    });

    ...more code

});

This will prompt the user with a permission dialog request to use location and if granted, our library will automatically collect the user location and start monitoring for regions you've created via the dashboard or API.

But this might not always be the best approach, as you may want to let the user choose when to start these location updates. Consider the example below for a better approach to location updates:

...more code

var isReadyForLocation = false;

...more code

$("body").bind("notificare:didRegisterDevice", function(event, data) {
    isReadyForLocation = true;
});

... more code

if (isReadyForLocation) {
    //Show the enable notifications button
    $("#enable-location-button").show();
} else {
    //Hide the enable notifications button
    $("#enable-location-button").hide();
}

$("#enable-location-button").click(function(e) {
    e.preventDefault();

    instance.notificare("startLocationUpdates", function(data){
        //Location updates started
    }, function(error){
        //Handle error
    });
});

Disable Location

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

...more code

instance.notificare("stopLocationUpdates", function(data){

}, function(error){

});