UNPKG

react-native-notificare-geo

Version:
307 lines (282 loc) 11.5 kB
"use strict"; import { AppRegistry, NativeEventEmitter, NativeModules, Platform } from 'react-native'; const LINKING_ERROR = `The package 'react-native-notificare-geo' doesn't seem to be linked. Make sure: \n\n` + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n'; // @ts-expect-error const isTurboModuleEnabled = global.__turboModuleProxy != null; const NotificareGeoModule = isTurboModuleEnabled ? require('./NativeNotificareGeoModule').default : NativeModules.NotificareGeoModule; const NativeModule = NotificareGeoModule ? NotificareGeoModule : new Proxy({}, { get() { throw new Error(LINKING_ERROR); } }); export class NotificareGeo { static eventEmitter = new NativeEventEmitter(NativeModule); // // Methods // /** * Indicates whether location services are enabled. * * @returns {Promise<boolean>} - A promise that resolves to `true` if the * location services are enabled by the application, and `false` otherwise. */ static async hasLocationServicesEnabled() { return await NativeModule.hasLocationServicesEnabled(); } /** * Indicates whether Bluetooth is enabled. * * @returns {Promise<boolean>} - A promise that resolves to `true` if Bluetooth * is enabled and available for beacon detection and ranging, and `false` otherwise. */ static async hasBluetoothEnabled() { return await NativeModule.hasBluetoothEnabled(); } /** * Provides a list of regions currently being monitored. * * @returns {Promise<NotificareRegion[]>} - A promise that resolves to a list * of {@link NotificareRegion} objects representing the geographical regions * being actively monitored for entry and exit events. */ static async getMonitoredRegions() { return await NativeModule.getMonitoredRegions(); } /** * Provides a list of regions the user has entered. * * @returns {Promise<NotificareRegion[]>} - A promise that resolves to a list * of {@link NotificareRegion} objects representing the regions that the user * has entered and not yet exited. */ static async getEnteredRegions() { return await NativeModule.getEnteredRegions(); } /** * Enables location updates, activating location tracking, region monitoring, * and beacon detection. * * **Note**: This function requires explicit location permissions from the user. * Starting with Android 10 (API level 29), background location access requires * the ACCESS_BACKGROUND_LOCATION permission. For beacon detection, Bluetooth * permissions are also necessary. Ensure all permissions are requested before * invoking this method. * * The behavior varies based on granted permissions: * - **Permission denied**: Clears the device's location information. * - **When In Use permission granted**: Tracks location only while the * app is in use. * - **Always permission granted**: Enables geofencing capabilities. * - **Always + Bluetooth permissions granted**: Enables geofencing * and beacon detection. * * @returns {Promise<void>} - A promise that resolves when location updates * have been successfully enabled. */ static async enableLocationUpdates() { await NativeModule.enableLocationUpdates(); } /** * Disables location updates. * * This method stops receiving location updates, monitoring regions, and * detecting nearby beacons. * * @returns {Promise<void>} - A promise that resolves when location updates * have been successfully disabled. */ static async disableLocationUpdates() { await NativeModule.disableLocationUpdates(); } // // Background methods // /** * Sets a callback that will be invoked when an onLocationUpdated event is * triggered in the background. * * @param callback - A callback that will be invoked with the result of the * onLocationUpdated event, when in background. It will provide the updated * {@link NotificareLocation} object representing the user's new location. */ static setLocationUpdatedBackgroundCallback(callback) { if (Platform.OS === 'android') { AppRegistry.registerHeadlessTask('re.notifica.geo.location_updated_background_callback', () => callback); } } /** * Sets a callback that will be invoked when an onRegionEntered event is * triggered in the background. * * @param callback - A callback that will be invoked with the result of the * onRegionEntered event. It will provide the {@link NotificareRegion} * representing the region the user has entered. */ static setRegionEnteredBackgroundCallback(callback) { if (Platform.OS === 'android') { AppRegistry.registerHeadlessTask('re.notifica.geo.region_entered_background_callback', () => callback); } } /** * Sets a callback that will be invoked when an onRegionExited event is * triggered in the background. * * @param callback - A callback that will be invoked with the result of the * onRegionExited event. It will provide the {@link NotificareRegion} * representing the region the user has exited. */ static setRegionExitedBackgroundCallback(callback) { if (Platform.OS === 'android') { AppRegistry.registerHeadlessTask('re.notifica.geo.region_exited_background_callback', () => callback); } } /** * Sets a callback that will be invoked when an onBeaconsEntered event is * triggered in the background. * * @param callback - A callback that will be invoked with the result of the * onBeaconEntered event. It will provide the {@link NotificareBeacon} * representing the beacon the user has entered the proximity of. */ static setBeaconEnteredBackgroundCallback(callback) { if (Platform.OS === 'android') { AppRegistry.registerHeadlessTask('re.notifica.geo.beacon_entered_background_callback', () => callback); } } /** * Sets a callback that will be invoked when an onBeaconsExited event is * triggered in the background. * * @param callback - A callback that will be invoked with the result of the * onBeaconExited event. It will provide the {@link NotificareBeacon} * representing the beacon the user has exited the proximity of. */ static setBeaconExitedBackgroundCallback(callback) { if (Platform.OS === 'android') { AppRegistry.registerHeadlessTask('re.notifica.geo.beacon_exited_background_callback', () => callback); } } /** * Sets a callback that will be invoked when an onBeaconsRanged event is * triggered in the background. * * @param callback - A callback that will be invoked with the result of the * onBeaconsRanged event. It will provide a list of {@link NotificareBeacon} * that were detected and the {@link NotificareRegion} where they were detected. */ static setBeaconsRangedBackgroundCallback(callback) { if (Platform.OS === 'android') { AppRegistry.registerHeadlessTask('re.notifica.geo.beacons_ranged_background_callback', () => callback); } } // // Events // /** * Called when a new location update is received. * * @param callback - A callback that will be invoked with the result of the * onLocationUpdated event. It will provide the updated {@link NotificareLocation} * object representing the user's new location. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onLocationUpdated event. */ static onLocationUpdated(callback) { return this.eventEmitter.addListener('re.notifica.geo.location_updated', callback); } /** * Called when the user enters a monitored region. * * @param callback - A callback that will be invoked with the result of the * onRegionEntered event. It will provide the {@link NotificareRegion} * representing the region the user has entered. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onRegionEntered event. */ static onRegionEntered(callback) { return this.eventEmitter.addListener('re.notifica.geo.region_entered', callback); } /** * Called when the user exits a monitored region. * * @param callback - A callback that will be invoked with the result of the * onRegionExited event. It will provide the {@link NotificareRegion} * representing the region the user has exited. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onRegionExited event. */ static onRegionExited(callback) { return this.eventEmitter.addListener('re.notifica.geo.region_exited', callback); } /** * Called when the user enters the proximity of a beacon. * * @param callback - A callback that will be invoked with the result of the * onBeaconEntered event. It will provide the {@link NotificareBeacon} * representing the beacon the user has entered the proximity of. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onBeaconEntered event. */ static onBeaconEntered(callback) { return this.eventEmitter.addListener('re.notifica.geo.beacon_entered', callback); } /** * Called when the user exits the proximity of a beacon. * * @param callback - A callback that will be invoked with the result of the * onBeaconExited event. It will provide the {@link NotificareBeacon} * representing the beacon the user has exited the proximity of. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onBeaconExited event. */ static onBeaconExited(callback) { return this.eventEmitter.addListener('re.notifica.geo.beacon_exited', callback); } /** * Called when beacons are ranged in a monitored region. * * This method provides the list of beacons currently detected within the given * region. * * @param callback - A callback that will be invoked with the result of the * onBeaconsRanged event. It will provide a list of {@link NotificareBeacon} * that were detected and the {@link NotificareRegion} where they were detected. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onBeaconsRanged event. */ static onBeaconsRanged(callback) { return this.eventEmitter.addListener('re.notifica.geo.beacons_ranged', callback); } /** * Called when the device registers a location visit. * * **Note**: This method is only supported on iOS. * * @param callback - A callback that will be invoked with the result of the * onVisit event. It will provide a {@link NotificareVisit} object representing * the details of the visit. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onVisit event. */ static onVisit(callback) { return this.eventEmitter.addListener('re.notifica.geo.visit', callback); } /** * Called when there is an update to the device’s heading. * * **Note**: This method is only supported on iOS. * * @param callback - A callback that will be invoked with the result of the * onHeadingUpdated event. It will provide a {@link NotificareHeading} object * containing the details of the updated heading. * @returns {EmitterSubscription} - The {@link EmitterSubscription} for the * onHeadingUpdated event. */ static onHeadingUpdated(callback) { return this.eventEmitter.addListener('re.notifica.geo.heading_updated', callback); } } //# sourceMappingURL=notificare-geo.js.map