Skip to content

Commit

Permalink
[Feature] Added support for sending vents from background mode
Browse files Browse the repository at this point in the history
  • Loading branch information
desusai7 committed Dec 30, 2021
1 parent b129711 commit 6b75b28
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 6 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ All notable changes to this project will be documented in this file.
### Changed
- Added logic to filter out the property which are not set for Application Opened event.

##Version - 1.3.0 - 2021-12-20
### Changed
##Version - 1.3.0 - 2021-12-29
### Additions
- Added support for additional background run time through configuration.
- Added watchOS as a supported platform.

Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,4 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificatio
// [FIRMessaging messaging].APNSToken = deviceToken;
}

@end
@end
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ SPEC CHECKSUMS:
nanopb: 59317e09cf1f1a0af72f12af412d54edf52603fc
PromisesObjC: 3113f7f76903778cf4a0586bd1ab89329a0b7b97
Protobuf: 3724efa50cb2846d7ccebc8691c574e85fd74471
Rudder: f3f6b4a7c8d59b31a742bed98b8bdf4448ce19e1
Rudder: 9f813b7a85c6f7f945aa876d4cfd56e7f316361c

PODFILE CHECKSUM: 33b0e9f1b94a028aa8a3b4aecf718a44c9b6265e

Expand Down
1 change: 1 addition & 0 deletions Sources/Classes/Public/RSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) int configRefreshInterval;
@property (nonatomic) bool trackLifecycleEvents;
@property (nonatomic) bool recordScreenViews;
@property (nonatomic) bool enableBackgroundMode;
@property (nonatomic, nonnull) NSString *controlPlaneUrl;
@property (nonatomic, readwrite) NSMutableArray* factories;
@property (nonatomic, readwrite) NSMutableArray* customFactories;
Expand Down
1 change: 1 addition & 0 deletions Sources/Classes/Public/RSConfigBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype) withConfigRefreshInteval: (int) configRefreshInterval;
- (instancetype) withTrackLifecycleEvens: (BOOL) trackLifecycleEvents;
- (instancetype) withRecordScreenViews: (BOOL) recordScreenViews;
- (instancetype) withEnableBackgroundMode:(BOOL) enableBackgroundMode;
- (instancetype) withConfigPlaneUrl: (NSString*) configPlaneUrl __attribute((deprecated("Use withControlPlaneUrl instead.")));
- (instancetype) withControlPlaneUrl: (NSString*) controlPlaneUrl;
- (instancetype) withControlPlaneURL: (NSURL*) controlPlaneURL;
Expand Down
2 changes: 2 additions & 0 deletions Sources/Classes/Public/RSConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ extern NSString *const RSControlPlaneUrl;
extern bool const RSTrackLifeCycleEvents;
// whether we should record screen views automatically
extern bool const RSRecordScreenViews;
// whether we should add support for background run time
extern bool const RSEnableBackgroundMode;
// SDK Version
extern NSString *const RS_VERSION;

Expand Down
1 change: 1 addition & 0 deletions Sources/Classes/Public/RSEventRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
NSString* authToken;
NSString* anonymousIdToken;
RSConfig* config;
UIBackgroundTaskIdentifier backgroundTask;
RSDBPersistentManager* dbpersistenceManager;
RSServerConfigManager* configManager;
NSMutableDictionary<NSString*, NSObject*>* integrations;
Expand Down
7 changes: 5 additions & 2 deletions Sources/Classes/RSConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ - (instancetype)init
_configRefreshInterval = RSConfigRefreshInterval;
_trackLifecycleEvents = RSTrackLifeCycleEvents;
_recordScreenViews = RSRecordScreenViews;
_enableBackgroundMode = RSEnableBackgroundMode;
_controlPlaneUrl = RSControlPlaneUrl;
_factories = [[NSMutableArray alloc] init];
_customFactories = [[NSMutableArray alloc] init];
Expand All @@ -37,8 +38,9 @@ - (instancetype)init:(NSString *) dataPlaneUrl
logLevel: (int) logLevel
configRefreshInterval: (int) configRefreshInteval
trackLifecycleEvents: (BOOL) trackLifecycleEvents
recordScreenViews: (BOOL) recordScreenViews
controlPlaneUrl: (NSString *) controlPlaneUrl
enableBackgroundMode: (BOOL) enableBackgroundMode
recordScreenViews: (BOOL) recordScreenViews
controlPlaneUrl: (NSString *) controlPlaneUrl
{
self = [super init];
if (self) {
Expand All @@ -51,6 +53,7 @@ - (instancetype)init:(NSString *) dataPlaneUrl
_trackLifecycleEvents = trackLifecycleEvents;
_recordScreenViews = recordScreenViews;
_controlPlaneUrl = controlPlaneUrl;
_enableBackgroundMode = enableBackgroundMode;
_factories = [[NSMutableArray alloc] init];
_customFactories = [[NSMutableArray alloc] init];
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/Classes/RSConfigBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ - (instancetype) withRecordScreenViews:(BOOL)recordScreenViews {
return self;
}

- (instancetype) withEnableBackgroundMode:(BOOL)enableBackgroundMode {
if (config == nil) {
config = [[RSConfig alloc] init];
}
config.enableBackgroundMode = enableBackgroundMode;
return self;
}

-(instancetype)withConfigPlaneUrl:(NSString *) configPlaneUrl {
if (config == nil) {
config = [[RSConfig alloc] init];
Expand Down
1 change: 1 addition & 0 deletions Sources/Classes/RSConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ @implementation RSConstants
NSString *const RSControlPlaneUrl = @"https://api.rudderlabs.com";
bool const RSTrackLifeCycleEvents = YES;
bool const RSRecordScreenViews = NO;
bool const RSEnableBackgroundMode = NO;
NSString *const RS_VERSION = @"1.3.0";
@end
25 changes: 25 additions & 0 deletions Sources/Classes/RSEventRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ - (instancetype)init : (NSString*) _writeKey config:(RSConfig*) _config {

writeKey = _writeKey;
config = _config;
if(config.enableBackgroundMode)
{
[RSLogger logDebug:@"EventRepository: Enabling Background Mode"];
backgroundTask = UIBackgroundTaskInvalid;
[self registerBackGroundTask];
}

NSData *authData = [[[NSString alloc] initWithFormat:@"%@:", _writeKey] dataUsingEncoding:NSUTF8StringEncoding];
authToken = [authData base64EncodedStringWithOptions:0];
Expand Down Expand Up @@ -572,12 +578,17 @@ - (void)_applicationWillEnterForeground {
return;
}

if(config.enableBackgroundMode) {
[self registerBackGroundTask];
}

[[RSClient sharedInstance] track:@"Application Opened" properties:@{
@"from_background" : @YES
}];
}

- (void)_applicationDidEnterBackground {

if ([self getOptStatus]) {
return;
}
Expand All @@ -595,5 +606,19 @@ - (void) __prepareScreenRecorder {
#endif
}

- (void) registerBackGroundTask {
if(backgroundTask != UIBackgroundTaskInvalid) {
[self endBackGroundTask];
}
[RSLogger logDebug:@"EventRepository: registerBackGroundTask: Registering for Background Mode"];
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackGroundTask];
}];
}

- (void) endBackGroundTask {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}

@end

0 comments on commit 6b75b28

Please sign in to comment.