-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOneSignalTracker.m
More file actions
157 lines (130 loc) · 6.62 KB
/
OneSignalTracker.m
File metadata and controls
157 lines (130 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Modified MIT License
*
* Copyright 2016 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import <UIKit/UIKit.h>
#import "OneSignalInternal.h"
#import "OneSignalTracker.h"
#import "OneSignalWebView.h"
#import <OneSignalCore/OneSignalCore.h>
#import "OSMacros.h"
#import <OneSignalOutcomes/OneSignalOutcomes.h>
#import "OSFocusTimeProcessorFactory.h"
#import "OSFocusCallParams.h"
#import "OSFocusInfluenceParam.h"
@interface OneSignal ()
+ (BOOL)shouldStartNewSession;
+ (void)startNewSession:(BOOL)fromInit;
+ (BOOL)sendNotificationTypesUpdate;
+ (NSString*)mUserId;
+ (NSString *)mEmailUserId;
+ (NSString *)mEmailAuthToken;
+ (NSString *)mExternalIdAuthToken;
@end
@implementation OneSignalTracker
static BOOL lastOnFocusWasToBackground = YES;
+ (void)onFocus:(BOOL)toBackground {
// return if the user has not granted privacy permissions
if ([OSPrivacyConsentController requiresUserPrivacyConsent])
return;
// Prevent the onFocus to be called twice when app being terminated
// - Both WillResignActive and willTerminate
if (lastOnFocusWasToBackground == toBackground)
return;
lastOnFocusWasToBackground = toBackground;
if (toBackground) {
[self applicationBackgrounded];
} else {
[self applicationBecameActive];
}
}
+ (void)applicationBecameActive {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Active started"];
[OSFocusTimeProcessorFactory cancelFocusCall];
if (OSSessionManager.sharedSessionManager.appEntryState != NOTIFICATION_CLICK)
OSSessionManager.sharedSessionManager.appEntryState = APP_OPEN;
[OSSessionManager.sharedSessionManager setLastOpenedTime:[[NSDate date] timeIntervalSince1970]];
// on_session tracking when resumming app.
if ([OneSignal shouldStartNewSession])
[OneSignal startNewSession:NO];
else {
[[OSSessionManager sharedSessionManager] attemptSessionUpgrade];
// TODO: Here it used to call receivedInAppMessageJson with nil, this method no longer exists
// [OneSignal receivedInAppMessageJson:nil];
}
}
+ (void)applicationBackgrounded {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Backgrounded started"];
[self updateLastClosedTime];
let timeElapsed = [OSSessionManager.sharedSessionManager getTimeFocusedElapsed];
if (timeElapsed < -1)
return;
OSSessionManager.sharedSessionManager.appEntryState = APP_CLOSE;
let influences = [[OSSessionManager sharedSessionManager] getSessionInfluences];
let focusCallParams = [self createFocusCallParams:influences onSessionEnded:false];
let timeProcessor = [OSFocusTimeProcessorFactory createTimeProcessorWithInfluences:influences focusEventType:BACKGROUND];
if (timeProcessor)
[timeProcessor sendOnFocusCall:focusCallParams];
// user module let them know app is backgrounded
[OneSignalUserManagerImpl.sharedInstance runBackgroundTasks];
}
// Note: This is not from app backgrounding
// The on_focus call is made right away.
+ (void)onSessionEnded:(NSArray<OSInfluence *> *)lastInfluences {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"onSessionEnded started"];
let timeElapsed = [OSSessionManager.sharedSessionManager getTimeFocusedElapsed];
let focusCallParams = [self createFocusCallParams:lastInfluences onSessionEnded:true];
let timeProcessor = [OSFocusTimeProcessorFactory createTimeProcessorWithInfluences:lastInfluences focusEventType:END_SESSION];
if (!timeProcessor) {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"onSessionEnded no time processor to end"];
return;
}
if (timeElapsed < -1)
// If there is no in focus time to be added we just need to send the time from the last session that just ended.
[timeProcessor sendUnsentActiveTime:focusCallParams];
else
[timeProcessor sendOnFocusCall:focusCallParams];
}
+ (OSFocusCallParams *)createFocusCallParams:(NSArray<OSInfluence *> *)lastInfluences onSessionEnded:(BOOL)onSessionEnded {
let timeElapsed = [OSSessionManager.sharedSessionManager getTimeFocusedElapsed];
NSMutableArray<OSFocusInfluenceParam *> *focusInfluenceParams = [NSMutableArray new];
for (OSInfluence *influence in lastInfluences) {
NSString *channelString = [OS_INFLUENCE_CHANNEL_TO_STRING(influence.influenceChannel) lowercaseString];
OSFocusInfluenceParam * focusInfluenceParam = [[OSFocusInfluenceParam alloc] initWithParamsInfluenceIds:influence.ids
influenceKey:[NSString stringWithFormat:@"%@_%@", channelString, @"ids"]
directInfluence:influence.influenceType == DIRECT
influenceDirectKey:@"direct"];
[focusInfluenceParams addObject:focusInfluenceParam];
}
return [[OSFocusCallParams alloc] initWithParamsAppId:[OneSignalConfigManager getAppId]
timeElapsed:timeElapsed
influenceParams:focusInfluenceParams
onSessionEnded:onSessionEnded];
}
+ (void)updateLastClosedTime {
let now = [NSDate date].timeIntervalSince1970;
[OneSignalUserDefaults.initStandard saveDoubleForKey:OSUD_APP_LAST_CLOSED_TIME withValue:now];
}
@end