Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification diff #425

Merged
merged 18 commits into from
Dec 21, 2024
2 changes: 1 addition & 1 deletion RadarSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RadarSDK'
s.version = '3.19.5'
s.version = '3.19.6'
s.summary = 'iOS SDK for Radar, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => 'support@radar.com' }
Expand Down
4 changes: 2 additions & 2 deletions RadarSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 3.19.5;
MARKETING_VERSION = 3.19.6;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -1122,7 +1122,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 3.19.5;
MARKETING_VERSION = 3.19.6;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
OTHER_CFLAGS = "-fembed-bitcode";
Expand Down
47 changes: 47 additions & 0 deletions RadarSDK/RadarAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#import "RadarUtils.h"
#import "RadarVerificationManager.h"
#import "RadarVerifiedLocationToken+Internal.h"
#import "RadarNotificationHelper.h"
#import <os/log.h>

@implementation RadarAPIClient
Expand Down Expand Up @@ -376,6 +377,47 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
}];
}

if (sdkConfiguration.useNotificationDiff) {
[RadarNotificationHelper getNotificationDiffWithCompletionHandler:^(NSArray *notificationsDelivered, NSArray *notificationsRemaining) {
if (notificationsDelivered) {
params[@"notificationDiff"] = notificationsDelivered;
}

[[RadarAPIClient sharedInstance] makeTrackRequestWithParams:params
options:options
stopped:stopped
location:location
source:source
verified:verified
publishableKey:publishableKey
notificationsRemaining:notificationsDelivered
completionHandler:completionHandler];
}];
} else {
[[RadarAPIClient sharedInstance] makeTrackRequestWithParams:params
options:options
stopped:stopped
location:location
source:source
verified:verified
publishableKey:publishableKey
notificationsRemaining:@[]
completionHandler:completionHandler];
}


}

- (void)makeTrackRequestWithParams:(NSDictionary *)params
options:(RadarTrackingOptions *)options
stopped:(BOOL)stopped
location:(CLLocation *)location
source:(RadarLocationSource)source
verified:(BOOL)verified
publishableKey:(NSString *)publishableKey
notificationsRemaining:(NSArray *)notificationsRemaining
completionHandler:(RadarTrackAPICompletionHandler)completionHandler {

NSString *host = verified ? [RadarSettings verifiedHost] : [RadarSettings host];
NSString *url = [NSString stringWithFormat:@"%@/v1/track", host];
url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Expand Down Expand Up @@ -416,6 +458,8 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
NSMutableDictionary *bufferParams = [params mutableCopy];
bufferParams[@"replayed"] = @(YES);

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:[NSString stringWithFormat:@"Setting %lu notifications remaining", (unsigned long)notificationsRemaining.count]];
[RadarState setRegisteredNotifications:notificationsRemaining];
[[RadarReplayBuffer sharedInstance] writeNewReplayToBuffer:bufferParams];
} else if (options.replay == RadarTrackingOptionsReplayStops && stopped &&
!(source == RadarLocationSourceForegroundLocation || source == RadarLocationSourceManualLocation)) {
Expand Down Expand Up @@ -510,6 +554,9 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
}

return completionHandler(RadarStatusSuccess, res, events, user, nearbyGeofences, config, token);
} else {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:[NSString stringWithFormat:@"Setting %lu notifications remaining", (unsigned long)notificationsRemaining.count]];
[RadarState setRegisteredNotifications:notificationsRemaining];
}

[[RadarDelegateHolder sharedInstance] didFailWithStatus:status];
Expand Down
2 changes: 2 additions & 0 deletions RadarSDK/RadarNotificationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef void (^NotificationPermissionCheckCompletion)(BOOL granted);

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

#
+ (void)getNotificationDiffWithCompletionHandler:(void (^)(NSArray *notificationsDelivered, NSArray *notificationsRemaining))completionHandler;
@end

NS_ASSUME_NONNULL_END
36 changes: 35 additions & 1 deletion RadarSDK/RadarNotificationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,23 @@ + (void)removePendingNotificationsWithCompletionHandler:(void (^)(void))completi
}

+ (void)addOnPremiseNotificationRequests:(NSArray<UNNotificationRequest *> *)requests {

[RadarNotificationHelper checkNotificationPermissionsWithCompletionHandler:^(BOOL granted) {
if (granted) {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
[RadarState setRegisteredNotifications: [NSArray new]];
for (UNNotificationRequest *request in requests) {
[notificationCenter addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
if (error) {
[[RadarLogger sharedInstance]
logWithLevel:RadarLogLevelError
message:[NSString stringWithFormat:@"Error adding local notification | identifier = %@; error = %@", request.identifier, error]];
} else {
NSDictionary *userInfo = request.content.userInfo;
if (userInfo) {
[RadarState addRegisteredNotification:userInfo];
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:[NSString stringWithFormat:@"Added local notification to registered notifications | userInfo = %@", userInfo]];
}

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
message:[NSString stringWithFormat:@"Added local notification | identifier = %@", request.identifier]];
}
Expand All @@ -196,6 +202,34 @@ + (void)addOnPremiseNotificationRequests:(NSArray<UNNotificationRequest *> *)req
}];
}

+ (void)getNotificationDiffWithCompletionHandler:(void (^)(NSArray *notificationsDelivered, NSArray *notificationsRemaining))completionHandler {
if (NSClassFromString(@"XCTestCase") != nil) {
if (completionHandler) {
completionHandler(@[], @[]);
}
return;
}
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
NSArray *registeredNotifications = [RadarState registeredNotifications];

[notificationCenter getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> *requests) {
NSMutableArray *currentNotifications = [NSMutableArray new];

for (UNNotificationRequest *request in requests) {
if (request.content.userInfo) {
[currentNotifications addObject:request.content.userInfo];
}
}

NSMutableArray *notificationsDelivered = [NSMutableArray arrayWithArray:registeredNotifications];
[notificationsDelivered removeObjectsInArray:currentNotifications];

if (completionHandler) {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:[NSString stringWithFormat:@"Setting %lu notifications remaining after re-registering", (unsigned long)notificationsDelivered.count]];
completionHandler(notificationsDelivered, currentNotifications);
}
}];
}

+ (void)checkNotificationPermissionsWithCompletionHandler:(NotificationPermissionCheckCompletion)completionHandler {
if (NSClassFromString(@"XCTestCase") == nil) {
Expand Down
2 changes: 2 additions & 0 deletions RadarSDK/RadarSdkConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, assign) BOOL useForegroundLocationUpdatedAtMsDiff;

@property (nonatomic, assign) BOOL useNotificationDiff;

/**
Initializes a new RadarSdkConfiguration object with given value.
*/
Expand Down
7 changes: 7 additions & 0 deletions RadarSDK/RadarSdkConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
_useForegroundLocationUpdatedAtMsDiff = [(NSNumber *)useForegroundLocationUpdatedAtMsDiffObj boolValue];
}

NSObject *useNotificationDiffObj = dict[@"useNotificationDiff"];
_useNotificationDiff = NO;
if (useNotificationDiffObj && [useNotificationDiffObj isKindOfClass:[NSNumber class]]) {
_useNotificationDiff = [(NSNumber *)useNotificationDiffObj boolValue];
}

return self;
}

Expand All @@ -97,6 +103,7 @@ - (NSDictionary *)dictionaryValue {
dict[@"useLocationMetadata"] = @(_useLocationMetadata);
dict[@"useOpenedAppConversion"] = @(_useOpenedAppConversion);
dict[@"useForegroundLocationUpdatedAtMsDiff"] = @(_useForegroundLocationUpdatedAtMsDiff);
dict[@"useNotificationDiff"] = @(_useNotificationDiff);

return dict;
}
Expand Down
1 change: 1 addition & 0 deletions RadarSDK/RadarSettings.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ @implementation RadarSettings
static NSString *const kXPlatformSDKVersion = @"radar-xPlatformSDKVersion";
static NSString *const kInitializeOptions = @"radar-initializeOptions";


+ (NSString *)publishableKey {
return [[NSUserDefaults standardUserDefaults] stringForKey:kPublishableKey];
}
Expand Down
3 changes: 3 additions & 0 deletions RadarSDK/RadarState.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)setLastMotionActivityData:(NSDictionary *_Nullable)lastMotionActivityData;
+ (void)setNotificationPermissionGranted:(BOOL)granted;
+ (BOOL)notificationPermissionGranted;
+ (NSArray<NSDictionary *> *_Nullable)registeredNotifications;
+ (void)setRegisteredNotifications:(NSArray<NSDictionary *> *_Nullable)registeredNotifications;
+ (void)addRegisteredNotification:(NSDictionary *)registeredNotification;

@end

Expand Down
20 changes: 20 additions & 0 deletions RadarSDK/RadarState.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ @implementation RadarState
static NSString *const kLastHeadingData = @"radar-lastHeadingData";
static NSString *const kLastMotionActivityData = @"radar-lastMotionActivityData";
static NSString *const kNotificationPermissionGranted = @"radar-notificationPermissionGranted";
static NSString *const kRegisteredNotifications = @"radar-registeredNotifications";

+ (CLLocation *)lastLocation {
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kLastLocation];
Expand Down Expand Up @@ -190,4 +191,23 @@ + (BOOL)notificationPermissionGranted {
return [[NSUserDefaults standardUserDefaults] boolForKey:kNotificationPermissionGranted];
}

+ (NSArray<NSDictionary *> *_Nullable)registeredNotifications {
NSArray<NSDictionary *> *registeredNotifications = [[NSUserDefaults standardUserDefaults] valueForKey:kRegisteredNotifications];
return registeredNotifications;
}

+ (void)setRegisteredNotifications:(NSArray<NSDictionary *> *_Nullable)registeredNotifications {
[[NSUserDefaults standardUserDefaults] setValue:registeredNotifications forKey:kRegisteredNotifications];
}


+ (void)addRegisteredNotification:(NSDictionary *)notification {
NSMutableArray *registeredNotifications = [NSMutableArray new];
NSArray *notifications = [RadarState registeredNotifications];
if (notifications) {
[registeredNotifications addObjectsFromArray:notifications];
}
[registeredNotifications addObject:notification];
[RadarState setRegisteredNotifications:registeredNotifications];
}
@end
2 changes: 1 addition & 1 deletion RadarSDK/RadarUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ + (NSNumber *)timeZoneOffset {
}

+ (NSString *)sdkVersion {
return @"3.19.5-beta.1";
return @"3.19.6";
}

+ (NSString *)deviceId {
Expand Down
2 changes: 1 addition & 1 deletion RadarSDKMotion.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RadarSDKMotion'
s.version = '3.19.5'
s.version = '3.19.6'
s.summary = 'Motion detection plugin for RadarSDK, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => 'support@radar.com' }
Expand Down
4 changes: 2 additions & 2 deletions RadarSDKMotion/RadarSDKMotion.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 3.19.5;
MARKETING_VERSION = 3.19.6;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -348,7 +348,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 3.19.5;
MARKETING_VERSION = 3.19.6;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand Down