Loading...
Searching...
No Matches
NotificationManagerExample.swift
Go to the documentation of this file.
1import Foundation
2
3/**
4 * NotificationManager usage example for Swift
5 * Demonstrates working with local notifications, iBeacon signals, and notification management
6 */
7class NotificationManagerExample: NSObject {
8 private var sdk: NavigineSdk?
9 private var locationManager: LocationManager?
10 private var notificationManager: NotificationManager?
11 private var notificationListener: NotificationListener?
12
13 override init() {
14 super.init()
15 initializeSdk()
16 setupNotificationListener()
17 }
18
19 /**
20 * Initialize SDK and get managers
21 */
22 private func initializeSdk() {
23 do {
24 // [swift_NavigineSdk_getInstance]
25 // Get SDK instance
26 sdk = NavigineSdk.getInstance()
27 // [swift_NavigineSdk_getInstance]
28
29 // [swift_NavigineSdk_setUserHash]
30 // Set user hash
31 sdk?.setUserHash("USER-HASH-HERE")
32 // [swift_NavigineSdk_setUserHash]
33
34 // [swift_NavigineSdk_setServer]
35 // Set server URL (optional)
36 sdk?.setServer("https://custom.navigine.com")
37 // [swift_NavigineSdk_setServer]
38
39 // [swift_NavigineSdk_getLocationManager]
40 // Get LocationManager for working with locations
41 locationManager = sdk?.getLocationManager()
42 // [swift_NavigineSdk_getLocationManager]
43
44 // [swift_NavigineSdk_getNotificationManager]
45 // Get NotificationManager for working with notifications
46 notificationManager = sdk?.getNotificationManager(locationManager)
47 // [swift_NavigineSdk_getNotificationManager]
48
49 if let locationManager = locationManager, let notificationManager = notificationManager {
50 print("LocationManager and NotificationManager successfully initialized")
51 }
52 } catch {
53 print("Error initializing SDK: \‍(error)")
54 }
55 }
56
57 /**
58 * Setup notification listener
59 */
60 private func setupNotificationListener() {
61 notificationListener = NotificationListenerImpl(example: self)
62 }
63
64 /**
65 * Demonstrate NotificationManager methods
66 */
67 func demonstrateNotificationManagerMethods() {
68 guard let manager = notificationManager, let listener = notificationListener else {
69 print("NotificationManager not initialized")
70 return
71 }
72
73 // [swift_NotificationManager_addNotificationListener]
74 // Add notification listener
75 manager.addNotificationListener(listener)
76 print("Added notification listener")
77 // [swift_NotificationManager_addNotificationListener]
78
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]
86 }
87 }
88
89 /**
90 * Demonstrate Notification usage
91 */
92 func demonstrateNotificationUsage(_ notification: Notification) {
93 guard let notification = notification else {
94 print("Notification is nil")
95 return
96 }
97
98 // [swift_Notification_getId]
99 // Get notification ID
100 let id = notification.id
101 print("Notification ID: \‍(id)")
102 // [swift_Notification_getId]
103
104 // [swift_Notification_getTitle]
105 // Get notification title
106 let title = notification.title
107 print("Notification title: \‍(title)")
108 // [swift_Notification_getTitle]
109
110 // [swift_Notification_getContent]
111 // Get notification content
112 let content = notification.content
113 print("Notification content: \‍(content)")
114 // [swift_Notification_getContent]
115
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)")
121 } else {
122 print("Notification has no image URL")
123 }
124 // [swift_Notification_getImageUrl]
125 }
126
127 /**
128 * Demonstrate error handling
129 */
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)")
135 }
136
137 /**
138 * Main demonstration method
139 */
140 func runExample() {
141 print("=== NotificationManager Example ===")
142
143 demonstrateNotificationManagerMethods()
144
145 // Wait a bit for notifications
146 DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
147 print("=== Example completed ===")
148 }
149 }
150}
151
152/**
153 * NotificationListener implementation
154 */
155class NotificationListenerImpl: NSObject, NotificationListener {
156 private weak var example: NotificationManagerExample?
157
158 init(example: NotificationManagerExample) {
159 self.example = example
160 super.init()
161 }
162
163 // [swift_NotificationListener_onNotificationLoaded]
164 func onNotificationLoaded(_ notification: Notification) {
165 print("Notification loaded")
166 example?.demonstrateNotificationUsage(notification)
167 }
168 // [swift_NotificationListener_onNotificationLoaded]
169
170 // [swift_NotificationListener_onNotificationFailed]
171 func onNotificationFailed(_ error: Error) {
172 print("Notification failed")
173 example?.demonstrateErrorHandling(error)
174 }
175 // [swift_NotificationListener_onNotificationFailed]
176}
177
178/**
179 * Function to run the example
180 */
181func main() {
182 let example = NotificationManagerExample()
183 example.runExample()
184
185 // Keep the app running for a while to see the output
186 RunLoop.main.run(until: Date().addingTimeInterval(10.0))
187}
188
189// Run the example
190main()