Skip to content

Commit b3e770b

Browse files
authored
Event metadata for campaigns (#433)
* use event metadata * update parsing * add campaign type * condition on correct type * use isequal instead for lint * use campaignType * bump minor version
1 parent 9afb4c4 commit b3e770b

File tree

9 files changed

+95
-44
lines changed

9 files changed

+95
-44
lines changed

RadarSDK.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'RadarSDK'
3-
s.version = '3.21.3'
3+
s.version = '3.21.4'
44
s.summary = 'iOS SDK for Radar, the leading geofencing and location tracking platform'
55
s.homepage = 'https://radar.com'
66
s.author = { 'Radar Labs, Inc.' => '[email protected]' }

RadarSDK.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@
10621062
GCC_WARN_UNUSED_FUNCTION = YES;
10631063
GCC_WARN_UNUSED_VARIABLE = YES;
10641064
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
1065-
MARKETING_VERSION = 3.21.3;
1065+
MARKETING_VERSION = 3.21.4;
10661066
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
10671067
MTL_FAST_MATH = YES;
10681068
ONLY_ACTIVE_ARCH = YES;
@@ -1120,7 +1120,7 @@
11201120
GCC_WARN_UNUSED_FUNCTION = YES;
11211121
GCC_WARN_UNUSED_VARIABLE = YES;
11221122
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
1123-
MARKETING_VERSION = 3.21.3;
1123+
MARKETING_VERSION = 3.21.4;
11241124
MTL_ENABLE_DEBUG_INFO = NO;
11251125
MTL_FAST_MATH = YES;
11261126
OTHER_CFLAGS = "-fembed-bitcode";

RadarSDK/RadarEvent.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,17 @@ - (instancetype _Nullable)initWithObject:(id)object {
315315
}
316316

317317
id metadataObj = dict[@"metadata"];
318-
if (metadataObj && [metadataObj isKindOfClass:[NSDictionary class]]) {
319-
metadata = (NSDictionary *)metadataObj;
318+
if (metadataObj) {
319+
if ([metadataObj isKindOfClass:[NSDictionary class]]) {
320+
metadata = (NSDictionary *)metadataObj;
321+
} else if ([metadataObj isKindOfClass:[NSString class]]) {
322+
NSError *jsonError;
323+
NSData *jsonData = [((NSString *)metadataObj) dataUsingEncoding:NSUTF8StringEncoding];
324+
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
325+
if (!jsonError && [jsonObj isKindOfClass:[NSDictionary class]]) {
326+
metadata = (NSDictionary *)jsonObj;
327+
}
328+
}
320329
}
321330

322331
if (_id && createdAt) {

RadarSDK/RadarLocationManager.m

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -554,37 +554,8 @@ - (void)replaceSyncedGeofences:(NSArray<RadarGeofence *> *)geofences {
554554

555555
NSDictionary *metadata = geofence.metadata;
556556
if (metadata) {
557-
NSString *notificationText = [geofence.metadata objectForKey:@"radar:notificationText"];
558-
NSString *notificationTitle = [geofence.metadata objectForKey:@"radar:notificationTitle"];
559-
NSString *notificationSubtitle = [geofence.metadata objectForKey:@"radar:notificationSubtitle"];
560-
NSString *notificationURL = [geofence.metadata objectForKey:@"radar:notificationURL"];
561-
NSString *campaignId = [geofence.metadata objectForKey:@"radar:campaignId"];
562-
if (notificationText) {
563-
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
564-
if (notificationTitle) {
565-
content.title = [NSString localizedUserNotificationStringForKey:notificationTitle arguments:nil];
566-
}
567-
if (notificationSubtitle) {
568-
content.subtitle = [NSString localizedUserNotificationStringForKey:notificationSubtitle arguments:nil];
569-
}
570-
content.body = [NSString localizedUserNotificationStringForKey:notificationText arguments:nil];
571-
572-
NSMutableDictionary *mutableUserInfo = [geofence.metadata mutableCopy];
573-
574-
mutableUserInfo[@"geofenceId"] = geofence._id;
575-
NSDate *now = [NSDate new];
576-
NSTimeInterval lastSyncInterval = [now timeIntervalSince1970];
577-
mutableUserInfo[@"registeredAt"] = [NSString stringWithFormat:@"%f", lastSyncInterval];
578-
579-
if (notificationURL) {
580-
mutableUserInfo[@"url"] = notificationURL;
581-
}
582-
583-
if (campaignId) {
584-
mutableUserInfo[@"campaignId"] = campaignId;
585-
}
586-
587-
content.userInfo = [mutableUserInfo copy];
557+
UNMutableNotificationContent *content = [RadarNotificationHelper extractContentFromMetadata:metadata geofenceId:geofence._id];
558+
if (content) {
588559

589560
region.notifyOnEntry = YES;
590561
region.notifyOnExit = NO;

RadarSDK/RadarNotificationHelper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ typedef void (^NotificationPermissionCheckCompletion)(BOOL granted);
2828

2929
+ (void)openURLFromNotification:(UNNotification *)notification;
3030

31-
#
31+
+ (nullable UNMutableNotificationContent *)extractContentFromMetadata:(nullable NSDictionary *)metadata geofenceId:(nullable NSString *)geofenceId;
32+
3233
+ (void)getNotificationDiffWithCompletionHandler:(void (^)(NSArray *notificationsDelivered, NSArray *notificationsRemaining))completionHandler;
3334
@end
3435

RadarSDK/RadarNotificationHelper.m

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ + (void)showNotificationsForEvents:(NSArray<RadarEvent *> *)events {
2828
}
2929

3030
for (RadarEvent *event in events) {
31+
NSString *identifier = [NSString stringWithFormat:@"%@%@", kEventNotificationIdentifierPrefix, event._id];
32+
NSString *categoryIdentifier = [RadarEvent stringForType:event.type];
33+
UNMutableNotificationContent *content = [RadarNotificationHelper extractContentFromMetadata:event.metadata geofenceId:nil];
34+
if (content) {
35+
content.categoryIdentifier = categoryIdentifier;
36+
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:nil];
37+
[UNUserNotificationCenter.currentNotificationCenter addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
38+
if (error) {
39+
[[RadarLogger sharedInstance]
40+
logWithLevel:RadarLogLevelDebug
41+
message:[NSString stringWithFormat:@"Error adding local notification | identifier = %@; error = %@", request.identifier, error]];
42+
} else {
43+
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
44+
message:[NSString stringWithFormat:@"Added local notification | identifier = %@", request.identifier]];
45+
46+
}
47+
}];
48+
} else {
49+
continue;
50+
}
51+
52+
3153
NSString *notificationText;
3254
NSDictionary *metadata;
3355

@@ -52,8 +74,6 @@ + (void)showNotificationsForEvents:(NSArray<RadarEvent *> *)events {
5274
}
5375

5476
if (notificationText) {
55-
NSString *identifier = [NSString stringWithFormat:@"%@%@", kEventNotificationIdentifierPrefix, event._id];
56-
NSString *categoryIdentifier = [RadarEvent stringForType:event.type];
5777

5878
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
5979
content.body = [NSString localizedUserNotificationStringForKey:notificationText arguments:nil];
@@ -72,6 +92,53 @@ + (void)showNotificationsForEvents:(NSArray<RadarEvent *> *)events {
7292
}
7393
}];
7494
}
95+
96+
}
97+
}
98+
99+
100+
+ (UNMutableNotificationContent *)extractContentFromMetadata:(NSDictionary *)metadata geofenceId:(NSString *)geofenceId {
101+
102+
if (!metadata) {
103+
return nil;
104+
}
105+
106+
NSString *notificationText = [metadata objectForKey:@"radar:notificationText"];
107+
NSString *notificationTitle = [metadata objectForKey:@"radar:notificationTitle"];
108+
NSString *notificationSubtitle = [metadata objectForKey:@"radar:notificationSubtitle"];
109+
NSString *notificationURL = [metadata objectForKey:@"radar:notificationURL"];
110+
NSString *campaignId = [metadata objectForKey:@"radar:campaignId"];
111+
112+
if (notificationText && [RadarNotificationHelper isNotificationCampaign:metadata]) {
113+
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
114+
if (notificationTitle) {
115+
content.title = [NSString localizedUserNotificationStringForKey:notificationTitle arguments:nil];
116+
}
117+
if (notificationSubtitle) {
118+
content.subtitle = [NSString localizedUserNotificationStringForKey:notificationSubtitle arguments:nil];
119+
}
120+
content.body = [NSString localizedUserNotificationStringForKey:notificationText arguments:nil];
121+
122+
NSMutableDictionary *mutableUserInfo = [metadata mutableCopy];
123+
124+
NSDate *now = [NSDate new];
125+
NSTimeInterval lastSyncInterval = [now timeIntervalSince1970];
126+
mutableUserInfo[@"registeredAt"] = [NSString stringWithFormat:@"%f", lastSyncInterval];
127+
128+
if (notificationURL) {
129+
mutableUserInfo[@"url"] = notificationURL;
130+
}
131+
if (campaignId) {
132+
mutableUserInfo[@"campaignId"] = campaignId;
133+
}
134+
if (geofenceId) {
135+
mutableUserInfo[@"geofenceId"] = geofenceId;
136+
}
137+
138+
content.userInfo = [mutableUserInfo copy];
139+
return content;
140+
} else {
141+
return nil;
75142
}
76143
}
77144

@@ -223,7 +290,6 @@ + (void)getNotificationDiffWithCompletionHandler:(void (^)(NSArray *notification
223290

224291
NSMutableArray *notificationsDelivered = [NSMutableArray arrayWithArray:registeredNotifications];
225292
[notificationsDelivered removeObjectsInArray:currentNotifications];
226-
227293
if (completionHandler) {
228294
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:[NSString stringWithFormat:@"Setting %lu notifications remaining after re-registering", (unsigned long)notificationsDelivered.count]];
229295
completionHandler(notificationsDelivered, currentNotifications);
@@ -251,4 +317,8 @@ + (void)checkNotificationPermissionsWithCompletionHandler:(NotificationPermissio
251317
}
252318
}
253319

320+
+ (BOOL)isNotificationCampaign:(NSDictionary *)metadata {
321+
return [metadata objectForKey:@"radar:campaignType"] != nil && ([[metadata objectForKey:@"radar:campaignType"] isEqual:@"clientSide"] || [[metadata objectForKey:@"radar:campaignType"] isEqual:@"eventBased"]);
322+
}
323+
254324
@end

RadarSDK/RadarUtils.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ + (NSNumber *)timeZoneOffset {
4545
}
4646

4747
+ (NSString *)sdkVersion {
48-
return @"3.21.3";
48+
return @"3.21.4";
4949
}
5050

5151
+ (NSString *)deviceId {

RadarSDKMotion.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'RadarSDKMotion'
3-
s.version = '3.21.3'
3+
s.version = '3.21.4'
44
s.summary = 'Motion detection plugin for RadarSDK, the leading geofencing and location tracking platform'
55
s.homepage = 'https://radar.com'
66
s.author = { 'Radar Labs, Inc.' => '[email protected]' }

RadarSDKMotion/RadarSDKMotion.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
GCC_WARN_UNUSED_VARIABLE = YES;
290290
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
291291
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
292-
MARKETING_VERSION = 3.21.3;
292+
MARKETING_VERSION = 3.21.4;
293293
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
294294
MTL_FAST_MATH = YES;
295295
ONLY_ACTIVE_ARCH = YES;
@@ -348,7 +348,7 @@
348348
GCC_WARN_UNUSED_VARIABLE = YES;
349349
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
350350
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
351-
MARKETING_VERSION = 3.21.3;
351+
MARKETING_VERSION = 3.21.4;
352352
MTL_ENABLE_DEBUG_INFO = NO;
353353
MTL_FAST_MATH = YES;
354354
SDKROOT = iphoneos;

0 commit comments

Comments
 (0)