Get started
- 1. Configure credentials and set up repositories
- 2. Add the dependency
- 3. Instantiate and start
DriverNotificationManager - 4. Add notification provider
- 5. Observe notifications with
DriverNotificationManagerinstance - 6. Detach notifications provider from
DriverNotificationManagerinstance - 7. Stop
DriverNotificationManagerinstance
This guide covers the most important steps to get started using the Driver Notifications API, with code snippets to help you start your implementation.
1. Configure credentials and set up repositories
For detailed instructions on configuring credentials, refer to the Get started page, and for guidance on initializing the SDK entry point, see the SDK Initialization guide.
2. Add the dependency
Add the library dependency to your build.gradle. The library version matches Navigation SDK version, they have the same release cadence. This way, you should not worry about version compatibility.
dependencies {
implementation "com.mapbox.navigationcore:driver-notification:3.16.2"
}
3. Instantiate and start DriverNotificationManager
val driverNotificationManagerOptions = DriverNotificationManagerOptions.Builder().build()
val driverNotificationManager = DriverNotificationManager(driverNotificationManagerOptions)
driverNotificationManager.start()
DriverNotificationManagerOptions is optional and can be omitted.
4. Add notification provider
Add one of the exposed providers that implements DriverNotificationProvider, such as SlowTrafficNotificationProvider.
val slowTrafficNotificationProvider = SlowTrafficNotificationProvider()
driverNotificationManager.attachDriverNotificationProvider(slowTrafficNotificationProvider)
5. Observe notifications with DriverNotificationManager instance
Use the before created instance of DriverNotificationManager to observe DriverNotification. All attached notification providers generate notifications that inherit from
DriverNotification. Check the type to retrieve the details of the notification.
driverNotificationManager.observeDriverNotifications().collect { notification ->
when(notification) {
is SlowTrafficNotification -> handleSlowTrafficNotification(notification)
else -> handleOtherNotifications(notification)
}
}
6. Detach notifications provider from DriverNotificationManager instance
If you no longer need to receive a specific notification type, detach the before created DriverNotificationProvider instance from DriverNotificationManager.
driverNotificationManager.detachDriverNotificationProvider(slowTrafficNotificationProvider)
7. Stop DriverNotificationManager instance
Stop DriverNotificationManager instance when notifications are no longer required. For example, stop it if a user disables this feature in the application settings.
driverNotificationManager.stop()