Wilson Desimini
Wilson Desimini

Reputation: 810

How to let one user get another user's location when other user's app is inactive? (Swift/Firebase)

I'm trying to implement a feature where one user that's linked as friends with another user can request to locate that other user even if their app is inactive.

Looking around I found this article showing how to update location using PushKit, but is there a way to do it using Apple's Push Notifications instead? I'm already parsing user locations to Firebase when the app is active or in the background.

I also read this SO answer mentioning an example of Firebase's Cloud-Messaging. Could that be a route to take with this?

I'm already sending location updates to Firebase when the user's app is active and in the background, I'm just having trouble updating location when it's inactive.

Upvotes: 0

Views: 361

Answers (2)

Daniel
Daniel

Reputation: 3597

I am actually not sure if this works––or is allowed by Apple––but I would perhaps try to do this:

override didReceive(_:withContentHandler:):

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    // call contentHandler(...)
}

And maybe––maybe––check the user's location and upload it to the server there.

Try using silent push notifications.

Let me know if this works.

Upvotes: 1

save the user's location in your data base and then compared in my case I use JSON to get lat and long values

let myLocation = CLLocation(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!)
                        print("My location: ", myLocation)

                        //My buddy's location
                        let myBuddysLocation = CLLocation(latitude: article.Latitud, longitude: article.Longitud)
                        print("my buddy's location: ", myBuddysLocation)

                        //Measuring my distance to my buddy's (in km)

                        let distance = myLocation.distance(from: myBuddysLocation) / 1000
                        print("distance between us: ", distance)

                        //Display the result in km
                        print(String(format: "The distance to my buddy is %.01fkm", distance))
                        //let distanceInMiles = distance / 1000
                        //print("KM DISTANCE BETWEEN US: ", distanceInMiles)

Upvotes: 0

Related Questions