Skip to main content

User notifications

If your application is in the background or the screen is locked during turn-by-turn navigation, NavigationViewController can automatically display local user notifications to inform the user of upcoming maneuvers. These notifications contain the same information as the instruction banners that would be visible on screen if your application were in the foreground. Tapping on a notification brings NavigationViewController back to the foreground.

Asking for permission to display notifications

The user needs to allow your application to display local user notifications. At some point before presenting NavigationViewController, such as when the application launches, ask the user for permission by calling the UNUserNotificationCenter.requestAuthorization(options:completionHandler:) method. This method is asynchronous, so if you also need to request Location Services authorization at the same time, call the CLLocationManager.requestWhenInUseAuthorization() method inside its completion handler:

UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { _, _ in
DispatchQueue.main.async {
CLLocationManager().requestWhenInUseAuthorization()
}
}

As long as your application is allowed to display user notifications, NavigationViewController displays them at the same time as spoken instructions. If you want the user to receive notifications from another part of your application but not from NavigationViewController, set the NavigationViewController.sendsNotifications property to false.

Displaying notifications manually

Your application can also manually display user notifications about the navigation session. For example, you can use a notification to inform the user about application-specific aspects of the trip or to have more fine-grained control over their timing. Manually displayed notifications can also be part of a custom turn-by-turn navigation interface that does not use NavigationViewController.

To manually display a user notification, implement the NavigationServiceDelegate.navigationService(_:didPassSpokenInstructionPoint:routeProgress:) method. Inside your implementation, populate a UNMutableNotificationContent using the current visual instruction, which you can get using the RouteProgress.currentLegProgress, RouteLegProgress.currentStep, and RouteStep.instructionsDisplayedAlongStep properties. Schedule this notification locally.

Further resources

Was this page helpful?