Skip to content

Location Based Notifications

Before you start:

  • You must add the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription strings.
  • For iOS 11 you must add the NSLocationAlwaysAndWhenInUseUsageDescription string.
  • If beacons are implied in your use case, we suggest you to add the NSBluetoothPeripheralUsageDescription string too.

To achieve this, you can edit your config.xml file:

...
<platform name="ios">
    ...
    <config-file platform="ios" target="*-Info.plist" parent="NSBluetoothPeripheralUsageDescription">
        <string>REPLACE THIS --> This app uses Bluetooth communication to detect nearby beacons</string>
    </config-file>

    <config-file platform="ios" target="*-Info.plist" parent="NSLocationAlwaysAndWhenInUseUsageDescription">
        <string>REPLACE THIS -->YOUR MESSAGE FOR &apos;Always&apos; AND &apos;WhenInUse&apos; PERMISSION</string>
    </config-file>

    <config-file platform="ios" target="*-Info.plist" parent="NSLocationAlwaysUsageDescription">
        <string>REPLACE THIS -->YOUR MESSAGE FOR &apos;Always&apos; PERMISSION</string>
    </config-file>

    <config-file platform="ios" target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
        <string>REPLACE THIS -->YOUR MESSAGE FOR &apos;WhenInUse&apos; PERMISSION</string>
    </config-file>
    ...
</platform>
...

Request permissions

You can check and ask for notification and location permissions using our drop-in permission dialog:

nearit.checkPermissions(function(status) {
  if (status.notifications != "always" || status.location != "always" ||
      !status.bluetooth || !status.locationServices) {
    nearit.requestPermissions(
      "YOUR MESSAGE THAT EXPLAINS WHY YOU ARE REQUESTING THESE PERMISSIONS",
      function(result) {
        // You can get these values from the `result` object
        console.log(`Location permission status: ${result.location}`);
        console.log(`Notifications permission status: ${result.notifications}`);
        console.log(`Bluetooth enabled: ${result.bluetooth}`);
        console.log(`Location services on: ${result.locationServices}`);

        if (result.location == "always" && result.notifications == "always") {
          // Start the radar
          nearit.startRadar();
        } else {
          // Permissions not fully granted, should NOT start radar
        }
      }
    );
  } else {
    // Start the radar
    nearit.startRadar();
  }
});
nearit.checkPermissions(status => {
  if (status.notifications !== 'always' ||
      status.location !== 'always' ||
      !status.bluetooth ||
      !status.locationServices) {
    nearit.requestPermissions(
      'YOUR MESSAGE THAT EXPLAINS WHY YOU ARE REQUESTING THESE PERMISSIONS',
      result => {
        // You can get these values from the `result` object
        console.log(`Location permission granted: ${result.location}`);
        console.log(`Notifications permission granted: ${result.notifications}`);
        console.log(`Bluetooth enabled: ${result.bluetooth}`);
        console.log(`Location services: ${result.locationServices}`);

        if (result.location === 'always' && result.notifications === 'always') {
          // Start the radar
          nearit.startRadar();
        } else {
          // Permissions not fully granted, should NOT start radar
        }
      });
  } else {
    nearit.startRadar();
  }
});

This will produce the following behaviour (depending on the platform):

permissions

You are now able to test this type of notifications! You should create a recipe in your sandbox environment and see that you will receive a notification.

Note: On Android, if you were already inside the geofence you just created, switch off and on again your location settings.

Also, if you have your own permission request flow, just remember to call the method nearit.startRadar() after your app has acquired the proper permissions.