|
| 1 | +/** |
| 2 | + * Modified MIT License |
| 3 | + * |
| 4 | + * Copyright 2019 OneSignal |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * 1. The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + * with services provided by OneSignal. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | +#import <UIKit/UIKit.h> |
| 28 | +#import "Requests.h" |
| 29 | +#import "OneSignalClient.h" |
| 30 | +#import "OSAttributedFocusTimeProcessor.h" |
| 31 | + |
| 32 | +@implementation OSAttributedFocusTimeProcessor { |
| 33 | + UIBackgroundTaskIdentifier delayBackgroundTask; |
| 34 | + NSTimer* restCallTimer; |
| 35 | +} |
| 36 | + |
| 37 | +static let ATTRIBUTED_MIN_SESSION_TIME_SEC = 1; |
| 38 | +static let DELAY_TIME = 30; |
| 39 | + |
| 40 | +- (instancetype)init { |
| 41 | + self = [super init]; |
| 42 | + delayBackgroundTask = UIBackgroundTaskInvalid; |
| 43 | + return self; |
| 44 | +} |
| 45 | + |
| 46 | +- (void)beginDelayBackgroundTask { |
| 47 | + delayBackgroundTask = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler:^{ |
| 48 | + [self endDelayBackgroundTask]; |
| 49 | + }]; |
| 50 | +} |
| 51 | + |
| 52 | +- (void)endDelayBackgroundTask { |
| 53 | + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE |
| 54 | + message:[NSString stringWithFormat:@"OSAttributedFocusTimeProcessor:endDelayBackgroundTask:%d", delayBackgroundTask]]; |
| 55 | + [UIApplication.sharedApplication endBackgroundTask:delayBackgroundTask]; |
| 56 | + delayBackgroundTask = UIBackgroundTaskInvalid; |
| 57 | +} |
| 58 | + |
| 59 | +- (int)getMinSessionTime { |
| 60 | + return ATTRIBUTED_MIN_SESSION_TIME_SEC; |
| 61 | +} |
| 62 | + |
| 63 | +- (NSString*)unsentActiveTimeUserDefaultsKey { |
| 64 | + return UNSENT_ACTIVE_TIME_ATTRIBUTED; |
| 65 | +} |
| 66 | + |
| 67 | +- (void)sendOnFocusCall:(OSFocusCallParams *)params { |
| 68 | + let unsentActive = [super getUnsentActiveTime]; |
| 69 | + let totalTimeActive = unsentActive + params.timeElapsed; |
| 70 | + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE |
| 71 | + message:[NSString stringWithFormat:@"sendOnFocusCall attributed with totalTimeActive %f", totalTimeActive]]; |
| 72 | + |
| 73 | + [super saveUnsentActiveTime:totalTimeActive]; |
| 74 | + [self sendOnFocusCallWithParams:params totalTimeActive:totalTimeActive]; |
| 75 | +} |
| 76 | + |
| 77 | +- (void)sendUnsentActiveTime:(OSFocusCallParams *)params { |
| 78 | + let unsentActive = [super getUnsentActiveTime]; |
| 79 | + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE |
| 80 | + message:[NSString stringWithFormat:@"sendUnsentActiveTime attributed with unsentActive %f", unsentActive]]; |
| 81 | + |
| 82 | + [self sendOnFocusCallWithParams:params totalTimeActive:unsentActive]; |
| 83 | +} |
| 84 | + |
| 85 | +- (void)sendOnFocusCallWithParams:(OSFocusCallParams *)params totalTimeActive:(NSTimeInterval)totalTimeActive { |
| 86 | + if (!params.userId) |
| 87 | + return; |
| 88 | + |
| 89 | + [self beginDelayBackgroundTask]; |
| 90 | + if (params.onSessionEnded) { |
| 91 | + [self sendBackgroundAttributedFocusPingWithParams:params withTotalTimeActive:@(totalTimeActive)]; |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + restCallTimer = [NSTimer |
| 96 | + scheduledTimerWithTimeInterval:DELAY_TIME |
| 97 | + target:self |
| 98 | + selector:@selector(sendBackgroundAttributedFocusPingWithNSTimer:) |
| 99 | + userInfo:@{@"params": params, @"time": @(totalTimeActive)} |
| 100 | + repeats:false]; |
| 101 | +} |
| 102 | + |
| 103 | +- (void)sendBackgroundAttributedFocusPingWithNSTimer:(NSTimer*)timer { |
| 104 | + let userInfo = (NSDictionary<NSString*, id>*)timer.userInfo; |
| 105 | + let params = (OSFocusCallParams*)userInfo[@"params"]; |
| 106 | + let totalTimeActive = (NSNumber*)userInfo[@"time"]; |
| 107 | + [self sendBackgroundAttributedFocusPingWithParams:params withTotalTimeActive:totalTimeActive]; |
| 108 | +} |
| 109 | + |
| 110 | +- (void)sendBackgroundAttributedFocusPingWithParams:(OSFocusCallParams*)params withTotalTimeActive:(NSNumber*)totalTimeActive { |
| 111 | + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 112 | + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"beginBackgroundAttributedFocusTask start"]; |
| 113 | + |
| 114 | + let requests = [NSMutableDictionary new]; |
| 115 | + |
| 116 | + requests[@"push"] = [OSRequestOnFocus withUserId:params.userId appId:params.appId activeTime:totalTimeActive netType:params.netType emailAuthToken:nil deviceType:@(DEVICE_TYPE_PUSH) directSession:params.direct notificationIds:params.notificationIds]; |
| 117 | + |
| 118 | + // For email we omit additionalFieldsToAddToOnFocusPayload as we don't want to add |
| 119 | + // outcome fields which would double report the session time |
| 120 | + if (params.emailUserId) |
| 121 | + requests[@"email"] = [OSRequestOnFocus withUserId:params.emailUserId appId:params.appId activeTime:totalTimeActive netType:params.netType emailAuthToken:params.emailAuthToken deviceType:@(DEVICE_TYPE_EMAIL)]; |
| 122 | + |
| 123 | + [OneSignalClient.sharedClient executeSimultaneousRequests:requests withSuccess:^(NSDictionary *result) { |
| 124 | + [super saveUnsentActiveTime:0]; |
| 125 | + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"sendOnFocusCallWithParams attributed succeed, saveUnsentActiveTime with 0"]; |
| 126 | + [self endDelayBackgroundTask]; |
| 127 | + } onFailure:^(NSDictionary<NSString *, NSError *> *errors) { |
| 128 | + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"sendOnFocusCallWithParams attributed failed, will retry on next open"]; |
| 129 | + [self endDelayBackgroundTask]; |
| 130 | + }]; |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +- (void)cancelDelayedJob { |
| 135 | + if (!restCallTimer) |
| 136 | + return; |
| 137 | + |
| 138 | + [restCallTimer invalidate]; |
| 139 | + restCallTimer = nil; |
| 140 | + [self endDelayBackgroundTask]; |
| 141 | +} |
| 142 | + |
| 143 | +@end |
0 commit comments