Loading...
Searching...
No Matches
NotificationManagerExample.m
Go to the documentation of this file.
2
7@implementation NotificationManagerExample
8
9- (instancetype)init {
10 self = [super init];
11 if (self) {
12 [self initializeSdk];
13 [self setupNotificationListener];
14 }
15 return self;
16}
17
21- (void)initializeSdk {
22 @try {
23 // [objc_NavigineSdk_getInstance]
24 // Get SDK instance
25 self.sdk = [NCNavigineSdk getInstance];
26 // [objc_NavigineSdk_getInstance]
27
28 // [objc_NavigineSdk_setUserHash]
29 // Set user hash
30 [self.sdk setUserHash:@"USER-HASH-HERE"];
31 // [objc_NavigineSdk_setUserHash]
32
33 // [objc_NavigineSdk_setServer]
34 // Set server URL (optional)
35 [self.sdk setServer:@"https://custom.navigine.com"];
36 // [objc_NavigineSdk_setServer]
37
38 // [objc_NavigineSdk_getLocationManager]
39 // Get LocationManager for working with locations
40 self.locationManager = [self.sdk getLocationManager];
41 // [objc_NavigineSdk_getLocationManager]
42
43 // [objc_NavigineSdk_getNotificationManager]
44 // Get NotificationManager for working with notifications
45 self.notificationManager = [self.sdk getNotificationManager:self.locationManager];
46 // [objc_NavigineSdk_getNotificationManager]
47
48 if (self.locationManager != nil && self.notificationManager != nil) {
49 NSLog(@"LocationManager and NotificationManager successfully initialized");
50 }
51 } @catch (NSException *exception) {
52 NSLog(@"Error initializing SDK: %@", exception.reason);
53 }
54}
55
59- (void)setupNotificationListener {
60 self.notificationListener = [[NotificationListenerImpl alloc] initWithExample:self];
61}
62
66- (void)demonstrateNotificationManagerMethods {
67 if (self.notificationManager == nil) {
68 NSLog(@"NotificationManager not initialized");
69 return;
70 }
71
72 // [objc_NotificationManager_addNotificationListener]
73 // Add notification listener
74 [self.notificationManager addNotificationListener:self.notificationListener];
75 NSLog(@"Added notification listener");
76 // [objc_NotificationManager_addNotificationListener]
77
78 // Simulate some time passing for notifications
79 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
80 // [objc_NotificationManager_removeNotificationListener]
81 // Remove notification listener
82 [self.notificationManager removeNotificationListener:self.notificationListener];
83 NSLog(@"Removed notification listener");
84 // [objc_NotificationManager_removeNotificationListener]
85 });
86}
87
91- (void)demonstrateNotificationUsage:(NCNotification *)notification {
92 if (notification == nil) {
93 NSLog(@"Notification is nil");
94 return;
95 }
96
97 // [objc_Notification_getId]
98 // Get notification ID
99 int32_t id = notification.id;
100 NSLog(@"Notification ID: %d", id);
101 // [objc_Notification_getId]
102
103 // [objc_Notification_getTitle]
104 // Get notification title
105 NSString *title = notification.title;
106 NSLog(@"Notification title: %@", title);
107 // [objc_Notification_getTitle]
108
109 // [objc_Notification_getContent]
110 // Get notification content
111 NSString *content = notification.content;
112 NSLog(@"Notification content: %@", content);
113 // [objc_Notification_getContent]
114
115 // [objc_Notification_getImageUrl]
116 // Get notification image URL
117 NSString *imageUrl = notification.imageUrl;
118 if (imageUrl != nil && imageUrl.length > 0) {
119 NSLog(@"Notification image URL: %@", imageUrl);
120 } else {
121 NSLog(@"Notification has no image URL");
122 }
123 // [objc_Notification_getImageUrl]
124}
125
129- (void)demonstrateErrorHandling:(NSError *)error {
130 NSLog(@"Handling notification error:");
131 NSLog(@" Error message: %@", error.localizedDescription);
132 NSLog(@" Error domain: %@", error.domain);
133 NSLog(@" Error code: %ld", (long)error.code);
134}
135
139- (void)runExample {
140 NSLog(@"=== NotificationManager Example ===");
141
142 [self demonstrateNotificationManagerMethods];
143
144 // Wait a bit for notifications
145 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
146 NSLog(@"=== Example completed ===");
147 });
148}
149
150@end
151
155@interface NotificationListenerImpl : NSObject <NCNotificationListener>
156
157@property (nonatomic, weak) NotificationManagerExample *example;
158
159- (instancetype)initWithExample:(NotificationManagerExample *)example;
160
161@end
162
163@implementation NotificationListenerImpl
164
165- (instancetype)initWithExample:(NotificationManagerExample *)example {
166 self = [super init];
167 if (self) {
168 self.example = example;
169 }
170 return self;
171}
172
173// [objc_NotificationListener_onNotificationLoaded]
174- (void)onNotificationLoaded:(NCNotification *)notification {
175 NSLog(@"Notification loaded");
176 [self.example demonstrateNotificationUsage:notification];
177}
178// [objc_NotificationListener_onNotificationLoaded]
179
180// [objc_NotificationListener_onNotificationFailed]
181- (void)onNotificationFailed:(NSError *)error {
182 NSLog(@"Notification failed");
183 [self.example demonstrateErrorHandling:error];
184}
185// [objc_NotificationListener_onNotificationFailed]
186
187@end
188
192int main(int argc, const char * argv[]) {
193 @autoreleasepool {
195 [example runExample];
196
197 // Keep the app running for a while to see the output
198 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];
199 }
200 return 0;
201}