React-native-background-geolocation: Background Geolocation with Headless JS

Created on 31 Mar 2017  路  2Comments  路  Source: transistorsoft/react-native-background-geolocation

I've been toying with React Native's HeadlessJS

BackgroundGeolocation can easily be plugged into this for you in about 15 minutes.

If anyone's interested, I'm working on a sample here

The Javascript end looks like this

index.android.js

import HeadlessService from './components/HeadlessService';
.
.
.
AppRegistry.registerHeadlessTask('HeadlessService', () => HeadlessService)
Android Announcement

Most helpful comment

BackgroundGeolocation Headless JS Support is now available in this Free Version.

Unfortunately, I haven't documented it much yet but it's really simple to setup.

NOTE: The HeadlessService runs only when the app is terminated.

  1. Add the following block into your AndroidManifest.xml
<application>
    <receiver android:name="com.transistorsoft.rnbackgroundgeolocation.RNBackgroundGeolocationEventReceiver">
        <intent-filter>
            <!-- You may choose to listen to any, all or none of the following events.  If you don't wish to listen to a particular event, remove it from your AndroidManifest -->
            <action android:name="com.transistorsoft.locationmanager.event.BOOT" />
            <action android:name="com.transistorsoft.locationmanager.event.TERMINATE" />
            <action android:name="com.transistorsoft.locationmanager.event.HEARTBEAT" />
            <action android:name="com.transistorsoft.locationmanager.event.MOTIONCHANGE" />
            <action android:name="com.transistorsoft.locationmanager.event.LOCATION" />
            <action android:name="com.transistorsoft.locationmanager.event.GEOFENCE" />
            <action android:name="com.transistorsoft.locationmanager.event.HTTP" />
            <action android:name="com.transistorsoft.locationmanager.event.SCHEDULE" />
            <action android:name="com.transistorsoft.locationmanager.event.ACTIVITYCHANGE" />
            <action android:name="com.transistorsoft.locationmanager.event.PROVIDERCHANGE" />
            <action android:name="com.transistorsoft.locationmanager.event.GEOFENCESCHANGE" />
        </intent-filter>
     </receiver>
 </application>
  1. In your index.android.js, register your Headless Service:
// First define or import your Service:
const BackgroundGeolocationHeadlessService = async (event) => {
    // do stuff
    console.log('[js] BackgroundGeolocationHeadlessService: ', event);

    switch (event.name) {
        case 'boot':
            break;
        case 'terminate':
            break;
        case 'heartbeat':
            break;
        case 'motionchange':
            break;
        case 'location':
            break;
        case 'geofence':
            break;
        case 'http':
            break;
        case 'schedule':
            break;
        case 'activitychange':
            break;
        case 'providerchange':
            break;
        case 'geofenceschange':
            break;
    }
};

// Now register it:
AppRegistry.registerHeadlessTask('BackgroundGeolocation', () => BackgroundGeolocationHeadlessService);

All 2 comments

Update: Headless JS support has now been integrated into the Premium version. It will arrive here after some testing and feedback.

What is "Headless JS"?

It's a mechanism to respond to any Background Geolocation event with your own custom Javascript, even after your app has been terminated.

BackgroundGeolocation Headless JS Support is now available in this Free Version.

Unfortunately, I haven't documented it much yet but it's really simple to setup.

NOTE: The HeadlessService runs only when the app is terminated.

  1. Add the following block into your AndroidManifest.xml
<application>
    <receiver android:name="com.transistorsoft.rnbackgroundgeolocation.RNBackgroundGeolocationEventReceiver">
        <intent-filter>
            <!-- You may choose to listen to any, all or none of the following events.  If you don't wish to listen to a particular event, remove it from your AndroidManifest -->
            <action android:name="com.transistorsoft.locationmanager.event.BOOT" />
            <action android:name="com.transistorsoft.locationmanager.event.TERMINATE" />
            <action android:name="com.transistorsoft.locationmanager.event.HEARTBEAT" />
            <action android:name="com.transistorsoft.locationmanager.event.MOTIONCHANGE" />
            <action android:name="com.transistorsoft.locationmanager.event.LOCATION" />
            <action android:name="com.transistorsoft.locationmanager.event.GEOFENCE" />
            <action android:name="com.transistorsoft.locationmanager.event.HTTP" />
            <action android:name="com.transistorsoft.locationmanager.event.SCHEDULE" />
            <action android:name="com.transistorsoft.locationmanager.event.ACTIVITYCHANGE" />
            <action android:name="com.transistorsoft.locationmanager.event.PROVIDERCHANGE" />
            <action android:name="com.transistorsoft.locationmanager.event.GEOFENCESCHANGE" />
        </intent-filter>
     </receiver>
 </application>
  1. In your index.android.js, register your Headless Service:
// First define or import your Service:
const BackgroundGeolocationHeadlessService = async (event) => {
    // do stuff
    console.log('[js] BackgroundGeolocationHeadlessService: ', event);

    switch (event.name) {
        case 'boot':
            break;
        case 'terminate':
            break;
        case 'heartbeat':
            break;
        case 'motionchange':
            break;
        case 'location':
            break;
        case 'geofence':
            break;
        case 'http':
            break;
        case 'schedule':
            break;
        case 'activitychange':
            break;
        case 'providerchange':
            break;
        case 'geofenceschange':
            break;
    }
};

// Now register it:
AppRegistry.registerHeadlessTask('BackgroundGeolocation', () => BackgroundGeolocationHeadlessService);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lyseiha picture lyseiha  路  6Comments

leighquince picture leighquince  路  3Comments

randymuhroji picture randymuhroji  路  3Comments

JAStanton picture JAStanton  路  3Comments

vishgoel007 picture vishgoel007  路  3Comments