1import kotlinx.coroutines.delay
2import kotlinx.coroutines.runBlocking
3import kotlinx.coroutines.GlobalScope
4import kotlinx.coroutines.launch
7 * NotificationManager usage example for Kotlin
8 * Demonstrates working with local notifications, iBeacon signals, and notification management
10class NotificationManagerExample {
11 private var sdk: NavigineSdk? = null
12 private var locationManager: LocationManager? = null
13 private var notificationManager: NotificationManager? = null
14 private var notificationListener: NotificationListener? = null
18 setupNotificationListener()
22 * Initialize SDK and get managers
24 private fun initializeSdk() {
26 // [kotlin_NavigineSdk_getInstance]
28 sdk = NavigineSdk.getInstance()
29 // [kotlin_NavigineSdk_getInstance]
31 // [kotlin_NavigineSdk_setUserHash]
33 sdk?.setUserHash("USER-HASH-HERE")
34 // [kotlin_NavigineSdk_setUserHash]
36 // [kotlin_NavigineSdk_setServer]
37 // Set server URL (optional)
38 sdk?.setServer("https://custom.navigine.com")
39 // [kotlin_NavigineSdk_setServer]
41 // [kotlin_NavigineSdk_getLocationManager]
42 // Get LocationManager for working with locations
43 locationManager = sdk?.getLocationManager()
44 // [kotlin_NavigineSdk_getLocationManager]
46 // [kotlin_NavigineSdk_getNotificationManager]
47 // Get NotificationManager for working with notifications
48 notificationManager = sdk?.getNotificationManager(locationManager)
49 // [kotlin_NavigineSdk_getNotificationManager]
51 if (locationManager != null && notificationManager != null) {
52 println("LocationManager and NotificationManager successfully initialized")
54 } catch (e: Exception) {
55 System.err.println("Error initializing SDK: ${e.message}")
60 * Setup notification listener
62 private fun setupNotificationListener() {
63 notificationListener = object : NotificationListener() {
64 // [kotlin_NotificationListener_onNotificationLoaded]
65 override fun onNotificationLoaded(notification: Notification) {
66 println("Notification loaded")
67 demonstrateNotificationUsage(notification)
69 // [kotlin_NotificationListener_onNotificationLoaded]
71 // [kotlin_NotificationListener_onNotificationFailed]
72 override fun onNotificationFailed(error: java.lang.Error) {
73 println("Notification failed: ${error.message}")
74 demonstrateErrorHandling(error)
76 // [kotlin_NotificationListener_onNotificationFailed]
81 * Demonstrate NotificationManager methods
83 fun demonstrateNotificationManagerMethods() {
84 val manager = notificationManager ?: run {
85 System.err.println("NotificationManager not initialized")
89 val listener = notificationListener ?: return
91 // [kotlin_NotificationManager_addNotificationListener]
92 // Add notification listener
93 manager.addNotificationListener(listener)
94 println("Added notification listener")
95 // [kotlin_NotificationManager_addNotificationListener]
97 // Simulate some time passing for notifications
101 // [kotlin_NotificationManager_removeNotificationListener]
102 // Remove notification listener
103 manager.removeNotificationListener(listener)
104 println("Removed notification listener")
105 // [kotlin_NotificationManager_removeNotificationListener]
110 * Demonstrate Notification usage
112 fun demonstrateNotificationUsage(notification: Notification) {
113 if (notification == null) {
114 System.err.println("Notification is null")
118 // [kotlin_Notification_getId]
119 // Get notification ID
120 val id = notification.id
121 println("Notification ID: $id")
122 // [kotlin_Notification_getId]
124 // [kotlin_Notification_getTitle]
125 // Get notification title
126 val title = notification.title
127 println("Notification title: $title")
128 // [kotlin_Notification_getTitle]
130 // [kotlin_Notification_getContent]
131 // Get notification content
132 val content = notification.content
133 println("Notification content: $content")
134 // [kotlin_Notification_getContent]
136 // [kotlin_Notification_getImageUrl]
137 // Get notification image URL
138 val imageUrl = notification.imageUrl
139 if (imageUrl != null && imageUrl.isNotEmpty()) {
140 println("Notification image URL: $imageUrl")
142 println("Notification has no image URL")
144 // [kotlin_Notification_getImageUrl]
148 * Demonstrate error handling
150 fun demonstrateErrorHandling(error: java.lang.Error) {
151 println("Handling notification error:")
152 println(" Error message: ${error.message}")
153 println(" Error type: ${error.javaClass.simpleName}")
157 * Main demonstration method
159 suspend fun runExample() {
160 println("=== NotificationManager Example ===")
162 demonstrateNotificationManagerMethods()
164 // Wait a bit for notifications
167 println("=== Example completed ===")
172 * Function to run the example
174fun main() = runBlocking {
175 val example = NotificationManagerExample()