Kathleen
Kathleen

Reputation: 58

Location Based Push Notifications using Parse

I am working on an app that sends out push notifications to all users within 1 mile of where the push originated. There is a lot of good documentation regarding using Parse for location based push notifications. I plan to use the code below (or something similar) to send out my pushes.

// Find users near a given location
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"location" 
nearGeoPoint:stadiumLocation    
withinMiles:[NSNumber numberWithInt:1]]

// Find devices associated with these users 
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"user" matchesQuery:userQuery];

// Send push notification to query
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery]; // Set our installation query
[push setMessage:@"Free hotdogs at the Parse concession stand!"];
[push sendPushInBackground];

In order to do this push query, I need to have every user's current location stored accurately in the Parse database. I plan to do this using CoreLocation and every time the app is notified of a location change it would update the user's location in the Parse database. Is this the best way to do this? I'm imagining a user traveling across country and their phone updating the parse database every mile. Is that the behavior? This will require very frequent updates to the Parse database by every user.

Am I missing something? Is that really the best way to do this? Thank you for any help or better ideas.

Upvotes: 0

Views: 665

Answers (1)

BHendricks
BHendricks

Reputation: 4493

What you can do, too, is use the significantLocationChange API from Apple, which only triggers a location change on a change of cell tower. This could reduce the number of location updates you have to perform, while still maintaining a seemingly good measure of where the user is.

In code, this would look something like this:

    [locationManager startMonitoringSignificantLocationChanges];

The documentation is at: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

In addition, you'll need to request permissions to track location in the background, something you need to declare in your Info.plist file.

You could also do some calculations on the client side to see how far off the new point is from the currently stored point, and only update the Parse database if the distance is greater than a certain threshold. This would reduce your Parse requests/sec and keep you in the free 30req/s range.

Upvotes: 1

Related Questions