Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

location based push notification when app is not running?

Is it possible when your app is not running but the user is still able to receive a push notifcation when he's near a particular location. This will require checking of the user's current latitude and longitude (even app is not running).

If it's possible, can you give some guidelines on how can I achieve it?

like image 796
Jenn Eve Avatar asked Apr 01 '13 04:04

Jenn Eve


2 Answers

There are two methods you can make use of to track user location even when the app is not running:

  1. Significant-location change service.

    Call the method startMonitoringSignificantLocationChanges in a CLLocationManager instance. This will help significantly in saving the power but it does not provide with high precision compared to the second method below.

    You do not need to set the location key in UIBackgroundModes if you opt for this method.

  2. Standard location service.

    Call the method startUpdatingLocation in a CLLocationManager instance. This takes a lot of device power but it provides higher precision. Remember to set location key in UIBackgroundModes to make sure the tracking is still working even if the app is killed.

You can use either one of the methods above. I use the first method combined with Region Monitoring to trigger local notification.

You can take a look at the following Apple documentation for a more thorough explanations and examples:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

like image 107
Valent Richie Avatar answered Nov 18 '22 18:11

Valent Richie


Region(CLRegion) Based Notifications Even When the App is not Running at all

Well, this answer is not a quick one but Apple has introduced new concept in UILocalNotification. It is not required to send a push notification, iOS will automatically show a local notification when user enter/exits geographical area CLRegion. From iOS 8 and later, we can schedule a local notification based on location not by setting fireDate property.

    let localNotification = UILocalNotification()
    localNotification.alertTitle = "Hi there"
    localNotification.alertBody = "you are here"

    let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 4.254, longitude: 88.25), radius: CLLocationDistance(100), identifier: "")
    region.notifyOnEntry = true
    region.notifyOnExit = false
    localNotification.region = region       

    localNotification.timeZone = NSTimeZone.localTimeZone()
    localNotification.soundName = UILocalNotificationDefaultSoundName

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

Here are more details from Apple.

like image 43
Sauvik Dolui Avatar answered Nov 18 '22 17:11

Sauvik Dolui