Location Triggered Notification App using UNLocationNotificationTrigger
Example Project for the Blog: Location Triggered Notifications on iOS
Tapping the "Schedule Location Notification" button will schedule a notification to go off when the device is ~500 meters away from the Brooklyn Promenade
let locationNotificationScheduler = LocationNotificationScheduler()
let notificationInfo = LocationNotificationInfo(notificationId: "nyc_promenade_notification_id",
locationId: "nyc_promenade_location_id",
radius: 500.0,
latitude: 40.696503,
longitude: -73.997809,
title: "Welcome to the Brooklyn Promenade!",
body: "Tap to see more information",
data: ["location": "NYC Brooklyn Promenade"])
locationNotificationScheduler.requestNotification(with: notificationInfo)
The LocationNotificationScheduler
needs a LocationNotificationInfo
object to request a notification.
notificationId
- Unique Id used to identify this specific type of notificationlocationId
- Unique Id used to identify this specific locationradius
- Double radius from the center in meterslatitude
- Center latitude of the locationlongitude
- Center longitude of the locationtitle
- Title of the notificationbody
- Body message of the notificationdata
- Optional data that will be sent with the notification
The LocationNotificationSchedulerDelegate
will provide functions to interact with the LocationNotificationScheduler
.
locationNotificationScheduler.delegate = self
/// Called when the user has denied the notification permission prompt.
func notificationPermissionDenied()
/// Called when the user has denied the location permission prompt.
func locationPermissionDenied()
/// Called when the notification request completed.
///
/// - Parameter error: Optional error when trying to add the notification.
func notificationScheduled(error: Error?)
The LocationNotificationSchedulerDelegate
also extends the UNUserNotificationCenterDelegate
which provides extra functions that interact with the notification.
The didReceive
function below is used to handle when a user selects the notification. This function will be called when the app is in the foreground or background.
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == "nyc_promenade_notification_id" {
let notificationData = response.notification.request.content.userInfo
// Do something with this notification data.
// This is the same as the data in the LocationNotificationInfo
}
completionHandler()
}