How to background service using Angular


#1

Hello
I try to make a simple app witch send current Location to the server (POST).
for the moment I can view the position and also send it to the server, but how to make my app running event in the background ?

    currentGeoLocation: any;

    enableLocationServices(): void {
        geoLocation.isEnabled().then(enabled => {
            if (!enabled) {
                geoLocation.enableLocationRequest().then(() => this.showLocation());
            } else {
                this.showLocation();
            }
        });
    }

    private showLocation(): void {
        geoLocation.watchLocation(location => {
            this.currentGeoLocation = location;
            console.log("lat : " + this.currentGeoLocation.latitude);
            console.log("lon : " + this.currentGeoLocation.longitude);
            console.log("alt : " + this.currentGeoLocation.altitude);
            console.log("speed : " + this.currentGeoLocation.speed);
            console.log("timestamp : " + this.currentGeoLocation.timestamp);

        }, error => {
            alert(error);
        }, {
                desiredAccuracy: 3,
                updateDistance: 10,
                minimumUpdateTime: 1000 * 9
            });
    }

#2

Create a background service where you can watch for location and keep posting same to your server.

Examples for background services:



#3

sadly, the android example app does not work… there’s an issue opened there and no replies :frowning:


#4

Which issue? I have been using it in my Angular apps, it works.


#5


strange issue, to be honest. do not understand why it happens (and even if i replace the cinstant w/ its value, found in android developer’s manual, it still crashes on the next line, like if it cant see native methods/classes)


#6

I have commented on the issue, that particular code needs Android 7.x.


#7

thanks! i was pretty sure this code is somehow necessary for the whole thing to be working, but now, looking more closely on the code, i see that it’s just setting up a local notification, and all the background thread business is above the line where it was crashing

maybe this question is not purely related to the original one, however i’m still missing how is it possible to communicate with the main thread of the app from the bg one. how can i pass my data from this job and/or trigger an event? thank you for you help
ps: im not actually using angular, just found this thread on the forum. would it be better to create one? not sure! :slight_smile:


#8

In Android you can use Intents / Broadcast Events. For iOS you may use NSNotificationCenter.