Loading...
Searching...
No Matches
NotificationManagerExample.kt
Go to the documentation of this file.
1import kotlinx.coroutines.delay
2import kotlinx.coroutines.runBlocking
3import kotlinx.coroutines.GlobalScope
4import kotlinx.coroutines.launch
5
6/**
7 * NotificationManager usage example for Kotlin
8 * Demonstrates working with local notifications, iBeacon signals, and notification management
9 */
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
15
16 init {
17 initializeSdk()
18 setupNotificationListener()
19 }
20
21 /**
22 * Initialize SDK and get managers
23 */
24 private fun initializeSdk() {
25 try {
26 // [kotlin_NavigineSdk_getInstance]
27 // Get SDK instance
28 sdk = NavigineSdk.getInstance()
29 // [kotlin_NavigineSdk_getInstance]
30
31 // [kotlin_NavigineSdk_setUserHash]
32 // Set user hash
33 sdk?.setUserHash("USER-HASH-HERE")
34 // [kotlin_NavigineSdk_setUserHash]
35
36 // [kotlin_NavigineSdk_setServer]
37 // Set server URL (optional)
38 sdk?.setServer("https://custom.navigine.com")
39 // [kotlin_NavigineSdk_setServer]
40
41 // [kotlin_NavigineSdk_getLocationManager]
42 // Get LocationManager for working with locations
43 locationManager = sdk?.getLocationManager()
44 // [kotlin_NavigineSdk_getLocationManager]
45
46 // [kotlin_NavigineSdk_getNotificationManager]
47 // Get NotificationManager for working with notifications
48 notificationManager = sdk?.getNotificationManager(locationManager)
49 // [kotlin_NavigineSdk_getNotificationManager]
50
51 if (locationManager != null && notificationManager != null) {
52 println("LocationManager and NotificationManager successfully initialized")
53 }
54 } catch (e: Exception) {
55 System.err.println("Error initializing SDK: ${e.message}")
56 }
57 }
58
59 /**
60 * Setup notification listener
61 */
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)
68 }
69 // [kotlin_NotificationListener_onNotificationLoaded]
70
71 // [kotlin_NotificationListener_onNotificationFailed]
72 override fun onNotificationFailed(error: java.lang.Error) {
73 println("Notification failed: ${error.message}")
74 demonstrateErrorHandling(error)
75 }
76 // [kotlin_NotificationListener_onNotificationFailed]
77 }
78 }
79
80 /**
81 * Demonstrate NotificationManager methods
82 */
83 fun demonstrateNotificationManagerMethods() {
84 val manager = notificationManager ?: run {
85 System.err.println("NotificationManager not initialized")
86 return
87 }
88
89 val listener = notificationListener ?: return
90
91 // [kotlin_NotificationManager_addNotificationListener]
92 // Add notification listener
93 manager.addNotificationListener(listener)
94 println("Added notification listener")
95 // [kotlin_NotificationManager_addNotificationListener]
96
97 // Simulate some time passing for notifications
98 GlobalScope.launch {
99 delay(2000)
100
101 // [kotlin_NotificationManager_removeNotificationListener]
102 // Remove notification listener
103 manager.removeNotificationListener(listener)
104 println("Removed notification listener")
105 // [kotlin_NotificationManager_removeNotificationListener]
106 }
107 }
108
109 /**
110 * Demonstrate Notification usage
111 */
112 fun demonstrateNotificationUsage(notification: Notification) {
113 if (notification == null) {
114 System.err.println("Notification is null")
115 return
116 }
117
118 // [kotlin_Notification_getId]
119 // Get notification ID
120 val id = notification.id
121 println("Notification ID: $id")
122 // [kotlin_Notification_getId]
123
124 // [kotlin_Notification_getTitle]
125 // Get notification title
126 val title = notification.title
127 println("Notification title: $title")
128 // [kotlin_Notification_getTitle]
129
130 // [kotlin_Notification_getContent]
131 // Get notification content
132 val content = notification.content
133 println("Notification content: $content")
134 // [kotlin_Notification_getContent]
135
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")
141 } else {
142 println("Notification has no image URL")
143 }
144 // [kotlin_Notification_getImageUrl]
145 }
146
147 /**
148 * Demonstrate error handling
149 */
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}")
154 }
155
156 /**
157 * Main demonstration method
158 */
159 suspend fun runExample() {
160 println("=== NotificationManager Example ===")
161
162 demonstrateNotificationManagerMethods()
163
164 // Wait a bit for notifications
165 delay(5000)
166
167 println("=== Example completed ===")
168 }
169}
170
171/**
172 * Function to run the example
173 */
174fun main() = runBlocking {
175 val example = NotificationManagerExample()
176 example.runExample()
177}