Loading...
Searching...
No Matches
notification_manager_example.dart
Go to the documentation of this file.
1import 'dart:async';
2import 'dart:io';
3import 'package:navigine_sdk/navigine_sdk.dart';
4
9class NotificationManagerExample {
10 NavigineSdk? _sdk;
11 LocationManager? _locationManager;
12 NotificationManager? _notificationManager;
13 NotificationListener? _notificationListener;
14
15 NotificationManagerExample() {
16 _initializeSdk();
17 _setupNotificationListener();
18 }
19
23 void _initializeSdk() {
24 try {
25 // [dart_NavigineSdk_getInstance]
26 // Get SDK instance
27 _sdk = NavigineSdk.getInstance();
28 // [dart_NavigineSdk_getInstance]
29
30 // [dart_NavigineSdk_setUserHash]
31 // Set user hash
32 _sdk?.setUserHash("USER-HASH-HERE");
33 // [dart_NavigineSdk_setUserHash]
34
35 // [dart_NavigineSdk_setServer]
36 // Set server URL (optional)
37 _sdk?.setServer("https://custom.navigine.com");
38 // [dart_NavigineSdk_setServer]
39
40 // [dart_NavigineSdk_getLocationManager]
41 // Get LocationManager for working with locations
42 _locationManager = _sdk?.getLocationManager();
43 // [dart_NavigineSdk_getLocationManager]
44
45 // [dart_NavigineSdk_getNotificationManager]
46 // Get NotificationManager for working with notifications
47 _notificationManager = _sdk?.getNotificationManager(_locationManager!);
48 // [dart_NavigineSdk_getNotificationManager]
49
50 if (_locationManager != null && _notificationManager != null) {
51 print("LocationManager and NotificationManager successfully initialized");
52 }
53 } catch (e) {
54 print("Error initializing SDK: $e");
55 }
56 }
57
61 void _setupNotificationListener() {
62 _notificationListener = NotificationListener(
63 // [dart_NotificationListener_onNotificationLoaded]
64 onNotificationLoaded: (Notification notification) {
65 print("Notification loaded");
66 _demonstrateNotificationUsage(notification);
67 },
68 // [dart_NotificationListener_onNotificationLoaded]
69
70 // [dart_NotificationListener_onNotificationFailed]
71 onNotificationFailed: (Error error) {
72 print("Notification failed: ${error.message}");
73 _demonstrateErrorHandling(error);
74 },
75 // [dart_NotificationListener_onNotificationFailed]
76 );
77 }
78
82 void demonstrateNotificationManagerMethods() {
83 if (_notificationManager == null) {
84 print("NotificationManager not initialized");
85 return;
86 }
87
88 // [dart_NotificationManager_addNotificationListener]
89 // Add notification listener
90 _notificationManager!.addNotificationListener(_notificationListener!);
91 print("Added notification listener");
92 // [dart_NotificationManager_addNotificationListener]
93
94 // Simulate some time passing for notifications
95 Future.delayed(Duration(seconds: 2), () {
96 // [dart_NotificationManager_removeNotificationListener]
97 // Remove notification listener
98 _notificationManager!.removeNotificationListener(_notificationListener!);
99 print("Removed notification listener");
100 // [dart_NotificationManager_removeNotificationListener]
101 });
102 }
103
107 void _demonstrateNotificationUsage(Notification notification) {
108 if (notification == null) {
109 print("Notification is null");
110 return;
111 }
112
113 // [dart_Notification_getId]
114 // Get notification ID
115 int id = notification.id;
116 print("Notification ID: $id");
117 // [dart_Notification_getId]
118
119 // [dart_Notification_getTitle]
120 // Get notification title
121 String title = notification.title;
122 print("Notification title: $title");
123 // [dart_Notification_getTitle]
124
125 // [dart_Notification_getContent]
126 // Get notification content
127 String content = notification.content;
128 print("Notification content: $content");
129 // [dart_Notification_getContent]
130
131 // [dart_Notification_getImageUrl]
132 // Get notification image URL
133 String? imageUrl = notification.imageUrl;
134 if (imageUrl != null) {
135 print("Notification image URL: $imageUrl");
136 } else {
137 print("Notification has no image URL");
138 }
139 // [dart_Notification_getImageUrl]
140 }
141
145 void _demonstrateErrorHandling(Error error) {
146 print("Handling notification error:");
147 print(" Error message: ${error.message}");
148 print(" Error type: ${error.runtimeType}");
149 }
150
154 Future<void> runExample() async {
155 print("=== NotificationManager Example ===");
156
157 demonstrateNotificationManagerMethods();
158
159 // Wait a bit for notifications
160 await Future.delayed(Duration(seconds: 5));
161
162 print("=== Example completed ===");
163 }
164}
165
169Future<void> main() async {
171 await example.runExample();
172}