Loading...
Searching...
No Matches
NotificationManagerExample.java
Go to the documentation of this file.
1package com.navigine.examples;
2
8 private NavigineSdk sdk;
9 private LocationManager locationManager;
10 private NotificationManager notificationManager;
11 private NotificationListener notificationListener;
12
14 initializeSdk();
15 setupNotificationListener();
16 }
17
21 private void initializeSdk() {
22 try {
23 // [java_NavigineSdk_getInstance]
24 // Get SDK instance
25 sdk = NavigineSdk.getInstance();
26 // [java_NavigineSdk_getInstance]
27
28 // [java_NavigineSdk_setUserHash]
29 // Set user hash
30 sdk.setUserHash("USER-HASH-HERE");
31 // [java_NavigineSdk_setUserHash]
32
33 // [java_NavigineSdk_setServer]
34 // Set server URL (optional)
35 sdk.setServer("https://custom.navigine.com");
36 // [java_NavigineSdk_setServer]
37
38 // [java_NavigineSdk_getLocationManager]
39 // Get LocationManager for working with locations
40 locationManager = sdk.getLocationManager();
41 // [java_NavigineSdk_getLocationManager]
42
43 // [java_NavigineSdk_getNotificationManager]
44 // Get NotificationManager for working with notifications
45 notificationManager = sdk.getNotificationManager(locationManager);
46 // [java_NavigineSdk_getNotificationManager]
47
48 if (locationManager != null && notificationManager != null) {
49 System.out.println("LocationManager and NotificationManager successfully initialized");
50 }
51 } catch (Exception e) {
52 System.err.println("Error initializing SDK: " + e.getMessage());
53 }
54 }
55
59 private void setupNotificationListener() {
60 notificationListener = new NotificationListener() {
61 @Override
62 // [java_NotificationListener_onNotificationLoaded]
63 public void onNotificationLoaded(Notification notification) {
64 System.out.println("Notification loaded");
65 demonstrateNotificationUsage(notification);
66 }
67 // [java_NotificationListener_onNotificationLoaded]
68
69 @Override
70 // [java_NotificationListener_onNotificationFailed]
71 public void onNotificationFailed(java.lang.Error error) {
72 System.out.println("Notification failed: " + error.getMessage());
74 }
75 // [java_NotificationListener_onNotificationFailed]
76 };
77 }
78
83 if (notificationManager == null) {
84 System.err.println("NotificationManager not initialized");
85 return;
86 }
87
88 // [java_NotificationManager_addNotificationListener]
89 // Add notification listener
90 notificationManager.addNotificationListener(notificationListener);
91 System.out.println("Added notification listener");
92 // [java_NotificationManager_addNotificationListener]
93
94 // Simulate some time passing for notifications
95 new Thread(() -> {
96 try {
97 Thread.sleep(2000);
98 } catch (InterruptedException e) {
99 Thread.currentThread().interrupt();
100 }
101
102 // [java_NotificationManager_removeNotificationListener]
103 // Remove notification listener
104 notificationManager.removeNotificationListener(notificationListener);
105 System.out.println("Removed notification listener");
106 // [java_NotificationManager_removeNotificationListener]
107 }).start();
108 }
109
113 public void demonstrateNotificationUsage(Notification notification) {
114 if (notification == null) {
115 System.err.println("Notification is null");
116 return;
117 }
118
119 // [java_Notification_getId]
120 // Get notification ID
121 int id = notification.getId();
122 System.out.println("Notification ID: " + id);
123 // [java_Notification_getId]
124
125 // [java_Notification_getTitle]
126 // Get notification title
127 String title = notification.getTitle();
128 System.out.println("Notification title: " + title);
129 // [java_Notification_getTitle]
130
131 // [java_Notification_getContent]
132 // Get notification content
133 String content = notification.getContent();
134 System.out.println("Notification content: " + content);
135 // [java_Notification_getContent]
136
137 // [java_Notification_getImageUrl]
138 // Get notification image URL
139 String imageUrl = notification.getImageUrl();
140 if (imageUrl != null && !imageUrl.isEmpty()) {
141 System.out.println("Notification image URL: " + imageUrl);
142 } else {
143 System.out.println("Notification has no image URL");
144 }
145 // [java_Notification_getImageUrl]
146
147 // Simulate notification processing
148 processNotification(notification);
149 }
150
154 public void demonstrateErrorHandling(java.lang.Error error) {
155 System.out.println("Handling notification error:");
156 System.out.println(" Error message: " + error.getMessage());
157 System.out.println(" Error type: " + error.getClass().getSimpleName());
158
159 // Simulate error recovery
160 handleNotificationError(error);
161 }
162
163
164
165
166
167
168
169
170
174 public void runExample() {
175 System.out.println("=== NotificationManager Example ===");
176
178
179 // Wait a bit for notifications
180 try {
181 Thread.sleep(5000);
182 } catch (InterruptedException e) {
183 Thread.currentThread().interrupt();
184 }
185
186 cleanup();
187 System.out.println("=== Example completed ===");
188 }
189
193 public static void main(String[] args) {
195 example.runExample();
196 }
197}