Skip to content

Commit c6cfcda

Browse files
committed
Update background sending session_time to user
* When app is backgrounded, create a background task and send the session_time to the user endpoint * Have callbacks, bypassing the operation repo * This is done both for unattributed sessions that send the request right away and for attributed sessions that wait 30 seconds before sending
1 parent 5a309b1 commit c6cfcda

File tree

4 files changed

+67
-27
lines changed

4 files changed

+67
-27
lines changed

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSPropertyOperationExecutor.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class OSPropertyOperationExecutor: OSOperationExecutor {
180180

181181
extension OSPropertyOperationExecutor {
182182
// TODO: We can make this go through the operation repo
183-
func updateProperties(propertiesDeltas: OSPropertiesDeltas, refreshDeviceMetadata: Bool?, propertiesModel: OSPropertiesModel, identityModel: OSIdentityModel) {
183+
func updateProperties(propertiesDeltas: OSPropertiesDeltas, refreshDeviceMetadata: Bool, propertiesModel: OSPropertiesModel, identityModel: OSIdentityModel, sendImmediately: Bool = false, onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) {
184184

185185
let request = OSRequestUpdateProperties(
186186
properties: [:],
@@ -189,7 +189,20 @@ extension OSPropertyOperationExecutor {
189189
modelToUpdate: propertiesModel,
190190
identityModel: identityModel)
191191

192-
updateRequestQueue.append(request)
193-
OneSignalUserDefaults.initShared().saveCodeableData(forKey: OS_PROPERTIES_EXECUTOR_UPDATE_REQUEST_QUEUE_KEY, withValue: self.updateRequestQueue)
192+
if (sendImmediately) {
193+
// Bypass the request queues
194+
OneSignalClient.shared().execute(request) { _ in
195+
if let onSuccess = onSuccess {
196+
onSuccess()
197+
}
198+
} onFailure: { _ in
199+
if let onFailure = onFailure {
200+
onFailure()
201+
}
202+
}
203+
} else {
204+
updateRequestQueue.append(request)
205+
OneSignalUserDefaults.initShared().saveCodeableData(forKey: OS_PROPERTIES_EXECUTOR_UPDATE_REQUEST_QUEUE_KEY, withValue: self.updateRequestQueue)
206+
}
194207
}
195208
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,11 @@ extension OneSignalUserManagerImpl {
548548
}
549549

550550
@objc
551-
public func updateSession(sessionCount: NSNumber?, sessionTime: NSNumber?, refreshDeviceMetadata: Bool) {
551+
public func updateSession(sessionCount: NSNumber?, sessionTime: NSNumber?, refreshDeviceMetadata: Bool, sendImmediately: Bool = false, onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) {
552552
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
553+
if let onFailure = onFailure {
554+
onFailure()
555+
}
553556
return
554557
}
555558

@@ -564,10 +567,16 @@ extension OneSignalUserManagerImpl {
564567
propertiesDeltas: propertiesDeltas,
565568
refreshDeviceMetadata: refreshDeviceMetadata,
566569
propertiesModel: propertiesModel,
567-
identityModel: identityModel
570+
identityModel: identityModel,
571+
sendImmediately: sendImmediately,
572+
onSuccess: onSuccess,
573+
onFailure: onFailure
568574
)
569575
} else {
570576
OneSignalLog.onesignalLog(.LL_ERROR, message: "OneSignalUserManagerImpl.updateSession with sessionCount: \(String(describing: sessionCount)) sessionTime: \(String(describing: sessionTime)) cannot be executed due to missing property executor.")
577+
if let onFailure = onFailure {
578+
onFailure()
579+
}
571580
}
572581
}
573582

iOS_SDK/OneSignalSDK/Source/OSAttributedFocusTimeProcessor.m

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import <OneSignalCore/OneSignalCore.h>
2929
#import "OneSignalFramework.h"
3030
#import "OSAttributedFocusTimeProcessor.h"
31+
#import <OneSignalUser/OneSignalUser.h>
3132

3233
@interface OneSignal ()
3334
+ (void)sendSessionEndOutcomes:(NSNumber*)totalTimeActive params:(OSFocusCallParams *)params onSuccess:(OSResultSuccessBlock _Nonnull)successBlock onFailure:(OSFailureBlock _Nonnull)failureBlock;
@@ -43,6 +44,8 @@ @implementation OSAttributedFocusTimeProcessor {
4344
- (instancetype)init {
4445
self = [super init];
4546
[OSBackgroundTaskManager setTaskInvalid:ATTRIBUTED_FOCUS_TASK];
47+
[OSBackgroundTaskManager setTaskInvalid:SEND_SESSION_TIME_TO_USER_TASK];
48+
4649
return self;
4750
}
4851

@@ -80,37 +83,51 @@ - (void)sendOnFocusCallWithParams:(OSFocusCallParams *)params totalTimeActive:(N
8083
}
8184

8285
[OSBackgroundTaskManager beginBackgroundTask:ATTRIBUTED_FOCUS_TASK];
86+
[OSBackgroundTaskManager beginBackgroundTask:SEND_SESSION_TIME_TO_USER_TASK];
8387

8488
if (params.onSessionEnded) {
85-
[self sendBackgroundAttributedFocusPingWithParams:params withTotalTimeActive:@(totalTimeActive)];
89+
[self sendBackgroundAttributedSessionTimeWithParams:params withTotalTimeActive:@(totalTimeActive)];
8690
return;
8791
}
8892

8993
restCallTimer = [NSTimer
9094
scheduledTimerWithTimeInterval:DELAY_TIME
9195
target:self
92-
selector:@selector(sendBackgroundAttributedFocusPingWithNSTimer:)
96+
selector:@selector(sendBackgroundAttributedSessionTimeWithNSTimer:)
9397
userInfo:@{@"params": params, @"time": @(totalTimeActive)}
9498
repeats:false];
9599
}
96100

97-
- (void)sendBackgroundAttributedFocusPingWithNSTimer:(NSTimer*)timer {
101+
- (void)sendBackgroundAttributedSessionTimeWithNSTimer:(NSTimer*)timer {
98102
let userInfo = (NSDictionary<NSString*, id>*)timer.userInfo;
99103
let params = (OSFocusCallParams*)userInfo[@"params"];
100104
let totalTimeActive = (NSNumber*)userInfo[@"time"];
101-
[self sendBackgroundAttributedFocusPingWithParams:params withTotalTimeActive:totalTimeActive];
105+
[self sendBackgroundAttributedSessionTimeWithParams:params withTotalTimeActive:totalTimeActive];
102106
}
103107

104-
- (void)sendBackgroundAttributedFocusPingWithParams:(OSFocusCallParams *)params withTotalTimeActive:(NSNumber*)totalTimeActive {
105-
106-
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"OSAttributedFocusTimeProcessor:sendBackgroundAttributedFocusPingWithParams start"];
107-
// TODO: Can we get wait for onSuccess to call [super saveUnsentActiveTime:0]
108-
// Need on failure an success to end background task
109-
if ([OneSignal sendSessionEndOutcomes:totalTimeActive params:params]) {
110-
[super saveUnsentActiveTime:0];
111-
}
108+
- (void)sendBackgroundAttributedSessionTimeWithParams:(OSFocusCallParams *)params withTotalTimeActive:(NSNumber*)totalTimeActive {
109+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"OSAttributedFocusTimeProcessor:sendBackgroundAttributedSessionTimeWithParams start"];
112110

113-
[OSBackgroundTaskManager endBackgroundTask:ATTRIBUTED_FOCUS_TASK];
111+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
112+
[OneSignalUserManagerImpl.sharedInstance updateSessionWithSessionCount:nil sessionTime:totalTimeActive refreshDeviceMetadata:false sendImmediately:true onSuccess:^{
113+
[super saveUnsentActiveTime:0];
114+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"sendBackgroundAttributed session time succeed, saveUnsentActiveTime with 0"];
115+
[OSBackgroundTaskManager endBackgroundTask:SEND_SESSION_TIME_TO_USER_TASK];
116+
} onFailure:^{
117+
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:@"sendBackgroundAttributed session time failed, will retry on next open"];
118+
[OSBackgroundTaskManager endBackgroundTask:SEND_SESSION_TIME_TO_USER_TASK];
119+
}];
120+
});
121+
122+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
123+
[OneSignal sendSessionEndOutcomes:totalTimeActive params:params onSuccess:^(NSDictionary *result) {
124+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"sendBackgroundAttributed succeed"];
125+
[OSBackgroundTaskManager endBackgroundTask:ATTRIBUTED_FOCUS_TASK];
126+
} onFailure:^(NSError *error) {
127+
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:@"sendBackgroundAttributed failed, will retry on next open"];
128+
[OSBackgroundTaskManager endBackgroundTask:ATTRIBUTED_FOCUS_TASK];
129+
}];
130+
});
114131
}
115132

116133
- (void)cancelDelayedJob {
@@ -120,6 +137,8 @@ - (void)cancelDelayedJob {
120137
[restCallTimer invalidate];
121138
restCallTimer = nil;
122139
[OSBackgroundTaskManager endBackgroundTask:ATTRIBUTED_FOCUS_TASK];
140+
[OSBackgroundTaskManager endBackgroundTask:SEND_SESSION_TIME_TO_USER_TASK];
141+
123142
}
124143

125144
@end

iOS_SDK/OneSignalSDK/Source/OSUnattributedFocusTimeProcessor.m

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,14 @@ - (void)sendOnFocusCallWithParams:(OSFocusCallParams *)params totalTimeActive:(N
7777

7878
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"OSUnattributedFocusTimeProcessor:sendOnFocusCallWithParams start"];
7979

80-
// updateSession can have a success fail block
81-
[OneSignalUserManagerImpl.sharedInstance updateSessionWithSessionCount:nil sessionTime:@(totalTimeActive) refreshDeviceMetadata:false];
82-
83-
// TODO: Can we get wait for onSuccess to call [super saveUnsentActiveTime:0]
84-
// TODO: Revisit when we also test op repo flushing on backgrounding
85-
// We could have callbacks from user module updateSessionTime and set to 0 when we get that callback
86-
[super saveUnsentActiveTime:0];
87-
88-
[OSBackgroundTaskManager endBackgroundTask:UNATTRIBUTED_FOCUS_TASK];
80+
[OneSignalUserManagerImpl.sharedInstance updateSessionWithSessionCount:nil sessionTime:@(totalTimeActive) refreshDeviceMetadata:false sendImmediately:true onSuccess:^{
81+
[super saveUnsentActiveTime:0];
82+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"sendOnFocusCallWithParams unattributed succeed, saveUnsentActiveTime with 0"];
83+
[OSBackgroundTaskManager endBackgroundTask:UNATTRIBUTED_FOCUS_TASK];
84+
} onFailure:^{
85+
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:@"sendOnFocusCallWithParams unattributed failed, will retry on next open"];
86+
[OSBackgroundTaskManager endBackgroundTask:UNATTRIBUTED_FOCUS_TASK];
87+
}];
8988
});
9089
}
9190

0 commit comments

Comments
 (0)