项目作者: jgkiano

项目描述 :
Reliable background location geofencing for React Native
高级语言: Java
项目地址: git://github.com/jgkiano/react-native-background-geofencing.git
创建时间: 2020-03-25T14:50:02Z
项目社区:https://github.com/jgkiano/react-native-background-geofencing

开源协议:MIT License

下载


React Native Background Geofencing

A reliable React Native geofencing library, that works in the background and survives device restarts.

WARNING: Currently only supports Android devices. Plans for iOS are underway

Getting started

$ yarn add react-native-background-geofencing

Android

Mostly automatic installation (for RN < 0.60)

$ react-native link react-native-background-geofencing

Permissions

Add the following permissions to your AndroidManifest.xml

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  3. <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"></uses-permission>
  4. <uses-permission android:name="android.permission.INTERNET" ></uses-permission>
  5. <uses-permission android:name="android.permission.WAKE_LOCK" ></uses-permission>
  6. </manifest>

Usage

1. Create an async JavaScript task file that will handle Geofence events

$ touch myTask.js

2. Place the following code inside myTask.js

The task will be executed in the background even after the device restarts.

  1. import {RNGeofenceEvent} from 'react-native-background-geofencing';
  2. export default async function myTask({event, data}) {
  3. // handle Geofence updates here
  4. if (event === RNGeofenceEvent.ENTER) {
  5. console.log(`welcome to ${data.lat},${data.lng}`);
  6. }
  7. if (event === RNGeofenceEvent.ERROR) {
  8. console.log(`Opps, something went wrong: ${data.errorMessage}`);
  9. }
  10. }

3. Configure your JavaScript task and/or webhook

Add the following in your app’s index.js file.

  1. import {AppRegistry} from 'react-native';
  2. import {
  3. configureJSTask,
  4. configureWebhook,
  5. } from 'react-native-background-geofencing';
  6. import App from './App';
  7. import myTask from './myTask.js';
  8. configureJSTask({
  9. task: myTask,
  10. // Required to enable your task to run as an Android foreground service
  11. notification: {
  12. title: "Don't mind me",
  13. text: "I'm just a notification",
  14. },
  15. });
  16. // If you'd like to ship the events to a server use this.
  17. // Its guaranteed to run when the device has an internet connection using Android's Work manager API
  18. configureWebhook({
  19. url: 'https://myapi.com/geofences',
  20. headers: {
  21. foo: 'Bar',
  22. },
  23. });
  24. AppRegistry.registerComponent(appName, () => App);

4. Add / Remove Geofences

  1. import RNBackgroundGeofencing from 'react-native-background-geofencing';
  2. const geofence = {
  3. id: 'mygeofence',
  4. lat: -1.29273,
  5. lng: 36.820389,
  6. };
  7. await RNBackgroundGeofencing.add(geofence);
  8. await RNBackgroundGeofencing.remove(geofence.id);

Contributions

Are welcomed ♥️ especially those specifically addressing iOS support and bug fixes.