Scheduling a location-based notification

The last type of notification you can schedule is a location-based notification. To set up a location-based trigger, you use Core Location to set up a region to which the trigger should belong.

A location-based trigger is actually very similar to implementing geofencing. If you need a refresher on geofences, skip back to Chapter 17, Improving Apps With Location Services. The main difference between a location-based notification and a geofence is that when a geofence triggers, the user is not necessarily informed about this. If a location-based notification triggers, the intent is to inform the user about it so they see a notification. Let's look at an example of how a location-based notification is set up:

let coords = CLLocationCoordinate2D(latitude: 52.373095, longitude: 4.8909129)  
let region = CLCircularRegion(center: coords, radius: 1000, identifier: "quote-notification-region")  
region.notifyOnExit = false  
region.notifyOnEntry = true  
let trigger = UNLocationNotificationTrigger(region: region, repeats: false)

The preceding snippet sets up a location-based notification, using a circular region with a radius of 1000 meters. The notifyOnExit and notifyOnEntry properties are used to specify whether the user should be notified when they enter the region, exit it, or when either event occurs.

Similar to the other notification triggers, you can choose whether the notification should be fired repeatedly, or just once.

This wraps up the exploration of all the ways for you to schedule notifications. Let's explore the last piece of the puzzle of notifications: handling incoming notifications and notification actions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.22.136