4 * NotificationManager usage example for Swift
5 * Demonstrates working with local notifications, iBeacon signals, and notification management
7class NotificationManagerExample: NSObject {
8 private var sdk: NavigineSdk?
9 private var locationManager: LocationManager?
10 private var notificationManager: NotificationManager?
11 private var notificationListener: NotificationListener?
16 setupNotificationListener()
20 * Initialize SDK and get managers
22 private func initializeSdk() {
24 // [swift_NavigineSdk_getInstance]
26 sdk = NavigineSdk.getInstance()
27 // [swift_NavigineSdk_getInstance]
29 // [swift_NavigineSdk_setUserHash]
31 sdk?.setUserHash("USER-HASH-HERE")
32 // [swift_NavigineSdk_setUserHash]
34 // [swift_NavigineSdk_setServer]
35 // Set server URL (optional)
36 sdk?.setServer("https://custom.navigine.com")
37 // [swift_NavigineSdk_setServer]
39 // [swift_NavigineSdk_getLocationManager]
40 // Get LocationManager for working with locations
41 locationManager = sdk?.getLocationManager()
42 // [swift_NavigineSdk_getLocationManager]
44 // [swift_NavigineSdk_getNotificationManager]
45 // Get NotificationManager for working with notifications
46 notificationManager = sdk?.getNotificationManager(locationManager)
47 // [swift_NavigineSdk_getNotificationManager]
49 if let locationManager = locationManager, let notificationManager = notificationManager {
50 print("LocationManager and NotificationManager successfully initialized")
53 print("Error initializing SDK: \(error)")
58 * Setup notification listener
60 private func setupNotificationListener() {
61 notificationListener = NotificationListenerImpl(example: self)
65 * Demonstrate NotificationManager methods
67 func demonstrateNotificationManagerMethods() {
68 guard let manager = notificationManager, let listener = notificationListener else {
69 print("NotificationManager not initialized")
73 // [swift_NotificationManager_addNotificationListener]
74 // Add notification listener
75 manager.addNotificationListener(listener)
76 print("Added notification listener")
77 // [swift_NotificationManager_addNotificationListener]
79 // Simulate some time passing for notifications
80 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
81 // [swift_NotificationManager_removeNotificationListener]
82 // Remove notification listener
83 manager.removeNotificationListener(listener)
84 print("Removed notification listener")
85 // [swift_NotificationManager_removeNotificationListener]
90 * Demonstrate Notification usage
92 func demonstrateNotificationUsage(_ notification: Notification) {
93 guard let notification = notification else {
94 print("Notification is nil")
98 // [swift_Notification_getId]
99 // Get notification ID
100 let id = notification.id
101 print("Notification ID: \(id)")
102 // [swift_Notification_getId]
104 // [swift_Notification_getTitle]
105 // Get notification title
106 let title = notification.title
107 print("Notification title: \(title)")
108 // [swift_Notification_getTitle]
110 // [swift_Notification_getContent]
111 // Get notification content
112 let content = notification.content
113 print("Notification content: \(content)")
114 // [swift_Notification_getContent]
116 // [swift_Notification_getImageUrl]
117 // Get notification image URL
118 let imageUrl = notification.imageUrl
119 if let imageUrl = imageUrl, !imageUrl.isEmpty {
120 print("Notification image URL: \(imageUrl)")
122 print("Notification has no image URL")
124 // [swift_Notification_getImageUrl]
128 * Demonstrate error handling
130 func demonstrateErrorHandling(_ error: Error) {
131 print("Handling notification error:")
132 print(" Error message: \(error.localizedDescription)")
133 print(" Error domain: \(error._domain)")
134 print(" Error code: \(error._code)")
138 * Main demonstration method
141 print("=== NotificationManager Example ===")
143 demonstrateNotificationManagerMethods()
145 // Wait a bit for notifications
146 DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
147 print("=== Example completed ===")
153 * NotificationListener implementation
155class NotificationListenerImpl: NSObject, NotificationListener {
156 private weak var example: NotificationManagerExample?
158 init(example: NotificationManagerExample) {
159 self.example = example
163 // [swift_NotificationListener_onNotificationLoaded]
164 func onNotificationLoaded(_ notification: Notification) {
165 print("Notification loaded")
166 example?.demonstrateNotificationUsage(notification)
168 // [swift_NotificationListener_onNotificationLoaded]
170 // [swift_NotificationListener_onNotificationFailed]
171 func onNotificationFailed(_ error: Error) {
172 print("Notification failed")
173 example?.demonstrateErrorHandling(error)
175 // [swift_NotificationListener_onNotificationFailed]
179 * Function to run the example
182 let example = NotificationManagerExample()
185 // Keep the app running for a while to see the output
186 RunLoop.main.run(until: Date().addingTimeInterval(10.0))