Skip to content

Commit

Permalink
Merge branch 'master' into notification-recipt-with-silent-push
Browse files Browse the repository at this point in the history
  • Loading branch information
KennyHuRadar authored Sep 16, 2024
2 parents 46df72e + d2c2928 commit 6a916d7
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ docs/undocumented.json
*.xcarchive
IntegrationExamples
.build/
Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/
10 changes: 7 additions & 3 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIWindowSceneDelegate, UN
self.requestLocationPermissions()

// Replace with a valid test publishable key
Radar.initialize(publishableKey: "prj_test_pk_0000000000000000000000000000000000000000")
Radar.setUserId("pushTestUserId")
let radarInitializeOptions = RadarInitializeOptions()
// Uncomment to enable automatic setup for notification conversions
// radarInitializeOptions.autoSetupNotificationConversion = true
Radar.initialize(publishableKey: "prj_test_pk_0000000000000000000000000000000000000000", options: radarInitializeOptions )
Radar.setUserId("testUserId")
Radar.setMetadata([ "foo": "bar" ])
Radar.setDelegate(self)
Radar.setVerifiedDelegate(self)
Expand Down Expand Up @@ -332,7 +335,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIWindowSceneDelegate, UN
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
// Do nothing
// Uncomment for manual setup for notification conversions
// Radar.logConversion(response: response)
}

func notify(_ body: String) {
Expand Down
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.17.0'
s.version = '3.18.0'
s.summary = 'iOS SDK for Radar, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => '[email protected]' }
Expand Down
14 changes: 12 additions & 2 deletions RadarSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@
DD8E2F7D24018C54002D51AB /* CLVisitMock.m in Sources */ = {isa = PBXBuildFile; fileRef = DD8E2F7C24018C54002D51AB /* CLVisitMock.m */; };
DE1E7644239724FD006F34A1 /* search_geofences.json in Resources */ = {isa = PBXBuildFile; fileRef = DE1E7643239724FD006F34A1 /* search_geofences.json */; };
E698B6502C6112FA00084371 /* RadarMotionProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = E698B64F2C6112FA00084371 /* RadarMotionProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
E6B93B722C90E2B8003CB858 /* RadarInitializeOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = E6B93B712C90E2B8003CB858 /* RadarInitializeOptions.h */; settings = {ATTRIBUTES = (Public, ); }; };
E6B93B742C90E5B8003CB858 /* RadarInitializeOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E6B93B732C90E5B8003CB858 /* RadarInitializeOptions.m */; };
E6B93B752C90E5B8003CB858 /* RadarInitializeOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E6B93B732C90E5B8003CB858 /* RadarInitializeOptions.m */; };
E6EEC56E2B20F41A00DD096B /* RadarFileStorage.h in Headers */ = {isa = PBXBuildFile; fileRef = E6EEC56D2B20F41A00DD096B /* RadarFileStorage.h */; };
E6EEC5702B20F45D00DD096B /* RadarFileStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = E6EEC56F2B20F45D00DD096B /* RadarFileStorage.m */; };
F65AF72C2C10B242002BA009 /* get_config_response.json in Resources */ = {isa = PBXBuildFile; fileRef = F65AF72B2C10B242002BA009 /* get_config_response.json */; };
Expand Down Expand Up @@ -337,6 +340,8 @@
DDF1157C2524E18100D575C4 /* RadarTrip.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RadarTrip.m; sourceTree = "<group>"; };
DE1E7643239724FD006F34A1 /* search_geofences.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = search_geofences.json; sourceTree = "<group>"; };
E698B64F2C6112FA00084371 /* RadarMotionProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RadarMotionProtocol.h; sourceTree = "<group>"; };
E6B93B712C90E2B8003CB858 /* RadarInitializeOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RadarInitializeOptions.h; sourceTree = "<group>"; };
E6B93B732C90E5B8003CB858 /* RadarInitializeOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RadarInitializeOptions.m; sourceTree = "<group>"; };
E6EEC56D2B20F41A00DD096B /* RadarFileStorage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RadarFileStorage.h; sourceTree = "<group>"; };
E6EEC56F2B20F45D00DD096B /* RadarFileStorage.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RadarFileStorage.m; sourceTree = "<group>"; };
F65AF72B2C10B242002BA009 /* get_config_response.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = get_config_response.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -390,6 +395,7 @@
96A5A0DA27AD9F7F007B960B /* RadarEvent.h */,
96A5A0E927AD9F7F007B960B /* RadarFraud.h */,
96A5A0E627AD9F7F007B960B /* RadarGeofence.h */,
E6B93B712C90E2B8003CB858 /* RadarInitializeOptions.h */,
96A5A0E227AD9F7F007B960B /* RadarGeofenceGeometry.h */,
96A5A0F327AD9F7F007B960B /* RadarPlace.h */,
96A5A0F027AD9F7F007B960B /* RadarPolygonGeometry.h */,
Expand Down Expand Up @@ -474,6 +480,7 @@
532FC303277A783900989279 /* Radar+Internal.h */,
DD236C9923087F9200EB88F9 /* RadarAPIClient.m */,
DD633EC1237C5B800026C91A /* RadarAPIHelper.h */,
E6B93B732C90E5B8003CB858 /* RadarInitializeOptions.m */,
DD633EC2237C5B800026C91A /* RadarAPIHelper.m */,
DD6F4F662573174400AFA38B /* RadarBeaconManager.h */,
DD64416E25B398A7003C3E63 /* RadarBeaconManager.m */,
Expand Down Expand Up @@ -642,6 +649,7 @@
019514362BD078D90031ABA2 /* RadarVerifiedLocationToken.h in Headers */,
96A5A10B27AD9F7F007B960B /* RadarTripOptions.h in Headers */,
E698B6502C6112FA00084371 /* RadarMotionProtocol.h in Headers */,
E6B93B722C90E2B8003CB858 /* RadarInitializeOptions.h in Headers */,
82F7FAEE2A65FE030055AA4B /* RadarVerificationManager.h in Headers */,
96A5A0C727AD9F41007B960B /* RadarChain+Internal.h in Headers */,
96A5A0C827AD9F41007B960B /* RadarBeacon+Internal.h in Headers */,
Expand Down Expand Up @@ -853,6 +861,7 @@
E6EEC5702B20F45D00DD096B /* RadarFileStorage.m in Sources */,
0107AB29262201F4008AB52F /* RadarTrackingOptions.m in Sources */,
0107AB2F262201FB008AB52F /* RadarUtils.m in Sources */,
E6B93B752C90E5B8003CB858 /* RadarInitializeOptions.m in Sources */,
0107AA8926220140008AB52F /* RadarChain.m in Sources */,
F667F8272BFBF3C8001F2F67 /* RadarSdkConfiguration.m in Sources */,
F667F8272BFBF3C8001F2F67 /* RadarSdkConfiguration.m in Sources */,
Expand Down Expand Up @@ -896,6 +905,7 @@
DD8E2F7424018C17002D51AB /* RadarPermissionsHelperMock.m in Sources */,
DD8E2F7724018C25002D51AB /* RadarAPIHelperMock.m in Sources */,
96B465BC27D6732500D7119B /* CLLocation+RadarTests.m in Sources */,
E6B93B742C90E5B8003CB858 /* RadarInitializeOptions.m in Sources */,
DD103211237E0C47003DD408 /* RadarSDKTests.m in Sources */,
DD8E2F7124018BF9002D51AB /* RadarTestUtils.m in Sources */,
DD8E2F7D24018C54002D51AB /* CLVisitMock.m in Sources */,
Expand Down Expand Up @@ -1038,7 +1048,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MARKETING_VERSION = "3.17.0";
MARKETING_VERSION = 3.18.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -1096,7 +1106,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MARKETING_VERSION = "3.17.0";
MARKETING_VERSION = 3.18.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
OTHER_CFLAGS = "-fembed-bitcode";
Expand Down
35 changes: 28 additions & 7 deletions RadarSDK/Include/Radar.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "RadarTrackingOptions.h"
#import "RadarVerifiedLocationToken.h"
#import "RadarUser.h"
#import "RadarInitializeOptions.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -324,6 +325,18 @@ typedef void (^_Nullable RadarLogConversionCompletionHandler)(RadarStatus status
*/
+ (void)initializeWithPublishableKey:(NSString *_Nonnull)publishableKey NS_SWIFT_NAME(initialize(publishableKey:));

/**
Initializes the Radar SDK.
@warning Call this method from the main thread in your `AppDelegate` class before calling any other Radar methods.
@param publishableKey Your publishable API key.
@param options Radar SDK initialization options.
@see https://radar.com/documentation/sdk/ios#initialize-sdk
*/

+ (void)initializeWithPublishableKey:(NSString *)publishableKey options:(RadarInitializeOptions *)options NS_SWIFT_NAME(initialize(publishableKey:options:));
#pragma mark - Properties

/**
Expand Down Expand Up @@ -638,7 +651,7 @@ typedef void (^_Nullable RadarLogConversionCompletionHandler)(RadarStatus status
completionHandler:(RadarLogConversionCompletionHandler)completionHandler NS_SWIFT_NAME(logConversion(name:metadata:completionHandler:));

/**
Logs a conversion with revenue
Logs a conversion with revenue.
@param name The name of the conversion.
@param revenue The revenue generated by the conversion.
Expand All @@ -653,17 +666,21 @@ typedef void (^_Nullable RadarLogConversionCompletionHandler)(RadarStatus status
completionHandler:(RadarLogConversionCompletionHandler)completionHandler NS_SWIFT_NAME(logConversion(name:revenue:metadata:completionHandler:));

/**
logConversionWithNotification
Logs a conversion with a notification.
@param request The request associated with the notification
@see https://radar.com/documentation/api#send-a-custom-event
*/
+ (void)logConversionWithNotification:(UNNotificationRequest *_Nullable)request NS_SWIFT_NAME(logConversion(request:));

+ (void)logConversionWithNotification:(UNNotificationRequest *)request
eventName:(NSString *)eventName
conversionSource:(NSString *_Nullable)conversionSource
deliveredAfter:(NSDate *_Nullable)deliveredAfter NS_SWIFT_NAME(logConversion(request:eventName:conversionSource:deliveredAfter:));
/**
Logs a conversion with a notification. This should only be used to manually setup logging of notification conversions.
@param response The response associated with user interaction with the notification.
@see https://radar.com/documentation/api#send-a-custom-event
*/

+ (void)logConversionWithNotificationResponse:(UNNotificationResponse *)response NS_SWIFT_NAME(logConversion(response:));

#pragma mark - Trips

Expand Down Expand Up @@ -1213,7 +1230,11 @@ logConversionWithNotification
+ (NSDictionary *)dictionaryForLocation:(CLLocation *)location NS_SWIFT_NAME(dictionaryForLocation(_:));


+ (void) nativeSetup NS_SWIFT_NAME(nativeSetup());
/**
Performs optional setup for Radar SDK within the AppDelegate. This method only needs to be called if Radar is initalized in cross-platform code.
*/
+ (void)nativeSetup NS_SWIFT_NAME(nativeSetup());

+ (void) testNotificationChecker NS_SWIFT_NAME(testNotificationChecker());

Expand Down
17 changes: 17 additions & 0 deletions RadarSDK/Include/RadarInitializeOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// RadarInitializeOptions.h
// RadarSDK
//
// Created by Kenny Hu on 9/10/24.
// Copyright © 2024 Radar Labs, Inc. All rights reserved.
//


#import <Foundation/Foundation.h>

@interface RadarInitializeOptions : NSObject

@property (assign, nonatomic) BOOL autoLogNotificationConversions;

@end

8 changes: 8 additions & 0 deletions RadarSDK/Radar+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@

+ (void)logOpenedAppConversion;

+ (void)logConversionWithNotification:(UNNotificationRequest *_Nonnull)request
eventName:(NSString *_Nonnull)eventName
conversionSource:(NSString *_Nullable)conversionSource
deliveredAfter:(NSDate *_Nullable)deliveredAfter;

+ (void)logOpenedAppConversionWithNotification:(UNNotificationRequest *_Nonnull)request
conversionSource:(NSString *_Nullable)conversionSource;

@end
94 changes: 60 additions & 34 deletions RadarSDK/Radar.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ + (void) nativeSetup {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[RadarNotificationHelper swizzleNotificationCenterDelegate];
[RadarNotificationHelper registerBackgroundNotificationChecks];
[RadarNotificationHelper scheduleBackgroundNotificationChecks];
//[RadarNotificationHelper registerBackgroundNotificationChecks];
//[RadarNotificationHelper scheduleBackgroundNotificationChecks];
});
}

+ (void)initializeWithPublishableKey:(NSString *)publishableKey {
+ (void)initializeWithPublishableKey:(NSString *)publishableKey options:(RadarInitializeOptions *)options {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo type:RadarLogTypeSDKCall message:@"initialize()"];

Class RadarSDKMotion = NSClassFromString(@"RadarSDKMotion");
Expand All @@ -74,10 +74,12 @@ + (void)initializeWithPublishableKey:(NSString *)publishableKey {

RadarSdkConfiguration *sdkConfiguration = [RadarSettings sdkConfiguration];

if (NSClassFromString(@"XCTestCase") == nil) {
[Radar nativeSetup];
// just setting the notification permission status into RadarState for now

if (NSClassFromString(@"XCTestCase") == nil && options.autoLogNotificationConversions) {
[RadarNotificationHelper checkNotificationPermissionsWithCompletion:nil];
if (options.autoLogNotificationConversions) {
[Radar nativeSetup];
}
}

if (sdkConfiguration.usePersistence) {
Expand All @@ -89,27 +91,33 @@ + (void)initializeWithPublishableKey:(NSString *)publishableKey {
}

[[RadarLocationManager sharedInstance] updateTrackingFromInitialize];

[[RadarAPIClient sharedInstance] getConfigForUsage:@"initialize"
verified:NO
completionHandler:^(RadarStatus status, RadarConfig *config) {
if (status == RadarStatusSuccess && config) {
[[RadarLocationManager sharedInstance] updateTrackingFromMeta:config.meta];
[RadarSettings setSdkConfiguration:config.meta.sdkConfiguration];
}

[RadarNotificationHelper checkNotificationPermissionsWithCompletionHandler:^(BOOL granted) {
[[RadarAPIClient sharedInstance] getConfigForUsage:@"initialize"
verified:NO
completionHandler:^(RadarStatus status, RadarConfig *config) {
if (status == RadarStatusSuccess && config) {
[[RadarLocationManager sharedInstance] updateTrackingFromMeta:config.meta];
[RadarSettings setSdkConfiguration:config.meta.sdkConfiguration];
}

RadarSdkConfiguration *sdkConfiguration = [RadarSettings sdkConfiguration];
if (sdkConfiguration.startTrackingOnInitialize && ![RadarSettings tracking]) {
[Radar startTrackingWithOptions:[RadarSettings trackingOptions]];
}
if (sdkConfiguration.trackOnceOnAppOpen) {
[Radar trackOnceWithCompletionHandler:nil];
}
RadarSdkConfiguration *sdkConfiguration = [RadarSettings sdkConfiguration];
if (sdkConfiguration.startTrackingOnInitialize && ![RadarSettings tracking]) {
[Radar startTrackingWithOptions:[RadarSettings trackingOptions]];
}
if (sdkConfiguration.trackOnceOnAppOpen) {
[Radar trackOnceWithCompletionHandler:nil];
}

[self flushLogs];
}];
}];

[self flushLogs];
}];
}

+ (void)initializeWithPublishableKey:(NSString *)publishableKey {
[self initializeWithPublishableKey:publishableKey options:[RadarInitializeOptions new]];
}

#pragma mark - Properties

Expand Down Expand Up @@ -508,15 +516,29 @@ + (void)sendLogConversionRequestWithName:(NSString * _Nonnull) name
}

+ (void)logOpenedAppConversion {
// if opened_app has been logged within the last second, don't log it again
NSTimeInterval lastAppOpenTimeInterval = [[NSDate date] timeIntervalSinceDate:[RadarSettings lastAppOpenTime]];
if (lastAppOpenTimeInterval > 1) {
[RadarSettings updateLastAppOpenTime];
[self sendLogConversionRequestWithName:@"opened_app" metadata:nil completionHandler:^(RadarStatus status, RadarEvent * _Nullable event) {
NSString *message = [NSString stringWithFormat:@"Conversion name = %@: status = %@; event = %@", event.conversionName, [Radar stringForStatus:status], event];
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:message];
}];
if (![RadarSettings useOpenedAppConversion]) {
return;
}

// Perform a non-blocking sleep for 1 second before starting, this is to address the fact that swizzled notification method may be called at a different relative live as compared to this method depending on framework.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// if opened_app has been logged within the last second, don't log it again
NSTimeInterval lastAppOpenTimeInterval = [[NSDate date] timeIntervalSinceDate:[RadarSettings lastAppOpenTime]];

if (lastAppOpenTimeInterval > 2) {
[RadarSettings updateLastAppOpenTime];
// metadata not needed as app is not opened by notification.
[self sendLogConversionRequestWithName:@"opened_app" metadata:nil completionHandler:^(RadarStatus status, RadarEvent * _Nullable event) {
NSString *message = [NSString stringWithFormat:@"Conversion name = %@: status = %@; event = %@", event.conversionName, [Radar stringForStatus:status], event];
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:message];
}];
}
});
}

+ (void)logOpenedAppConversionWithNotification:(UNNotificationRequest *)request
conversionSource:(NSString *_Nullable)conversionSource {
[self logConversionWithNotification:request eventName:@"opened_app" conversionSource:conversionSource deliveredAfter:nil];
}

+ (void)logConversionWithName:(NSString *)name
Expand Down Expand Up @@ -582,6 +604,10 @@ + (void)logConversionWithNotification:(UNNotificationRequest *)request
}];
}

+ (void)logConversionWithNotificationResponse:(UNNotificationResponse *)response {
[RadarNotificationHelper logConversionWithNotificationResponse:response];
}

#pragma mark - Trips

+ (RadarTripOptions *)getTripOptions {
Expand Down Expand Up @@ -1383,9 +1409,9 @@ - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

+ (BOOL)isTestKey {
+ (BOOL)canFlushLogs {
NSString *publishableKey = [RadarSettings publishableKey];
if ([publishableKey hasPrefix:@"prj_test_pk"] || [publishableKey hasPrefix:@"org_test_pk"] || [RadarSettings userDebug]) {
if ([publishableKey hasPrefix:@"prj_test_pk"] || [publishableKey hasPrefix:@"org_test_pk"] || [RadarSettings userDebug] || ([RadarSettings sdkConfiguration].logLevel && [RadarSettings sdkConfiguration].logLevel != RadarLogLevelNone)) {
return YES;
}
return NO;
Expand All @@ -1396,7 +1422,7 @@ + (void)sendLog:(RadarLogLevel)level type:(RadarLogType)type message:(NSString *
}

+ (void)flushLogs {
if (![self isTestKey]) {
if (![self canFlushLogs]) {
return;
}

Expand Down
Loading

0 comments on commit 6a916d7

Please sign in to comment.