0

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.

Wilson Desimini
  • 722
  • 5
  • 10
  • Do you want the user to just be able to ask for the user's permission or to automatically send the location (assuming permission has already been granted)? – Daniel Jan 10 '19 at 19:16
  • Automatically send location assuming permission already granted – Wilson Desimini Jan 10 '19 at 19:19
  • If I were to guess, what you ask for is impossible. For example, Snapchat (a company dedicated to this kind of social networking) has a map that shows the *last known* locations of users that opt in. If a user does not open the app, it can only show the location from when the app was last opened. – Daniel Jan 10 '19 at 19:34
  • Apps like Find My Friends are able to do so. I also see example online where people are able to record their location to plists when their app is closed, I just haven't found examples with Swift. – Wilson Desimini Jan 10 '19 at 19:55
  • 1
    Find my Friends is an Apple app. They can do things that we can't. Check out my answer. Hopefully it helps. – Daniel Jan 10 '19 at 19:59

2 Answers2

1

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.

Daniel
  • 3,188
  • 14
  • 34
  • Daniel, so I have been trying to do this, and I've had a little success, but I recently posted a question about it. it will only receive a silent notification correctly once and then is unable to after that.https://stackoverflow.com/questions/54273528/fcm-silent-notification-only-works-once-swift – Wilson Desimini Jan 20 '19 at 05:39
0

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)
  • 1
    So I understand how to save and compare user locations into Firebase, what I was looking for was how to do it when the other user's app is inactive – Wilson Desimini Jan 10 '19 at 19:29
  • 1
    you DB should provide you with LAT and LONG even after the user is inactive or closes the app, the register must remain active – José Raúl Toledano R Jan 10 '19 at 19:35
  • 1
    So I know the Database is always there. Here's an example of what I'm looking for: User1 is in Place1 today and is using the app. He requests User2's location. User2 was in Place1 yesterday with his app active, but today User2 closes his app and then travels to Place 2. How can I have it so that User2's device sends that he is in Place2 right now to the DB despite his app being closed. – Wilson Desimini Jan 10 '19 at 19:49