Skip to content

Commit 064c46b

Browse files
authored
Merge pull request #564 from OneSignal/feature/outcomes-events
Feature/outcomes events
2 parents af9ac31 + 44d3815 commit 064c46b

File tree

85 files changed

+5362
-647
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+5362
-647
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/Base.lproj/Main.storyboard

Lines changed: 128 additions & 1 deletion
Large diffs are not rendered by default.

iOS_SDK/OneSignalDevApp/OneSignalDevApp/OneSignalDevApp.entitlements

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<string>development</string>
77
<key>com.apple.security.application-groups</key>
88
<array>
9-
<string>group.com.onesignal.example.testgroup</string>
9+
<string>group.com.onesignal.example.onesignal</string>
1010
</array>
1111
</dict>
1212
</plist>

iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.m

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ @interface ViewController ()
4343
@property (weak, nonatomic) IBOutlet UITextField *removeTriggerKey;
4444
@property (weak, nonatomic) IBOutlet UITextField *getTriggerKey;
4545
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
46+
@property (weak, nonatomic) IBOutlet UITextField *externalIdTextField;
47+
@property (weak, nonatomic) IBOutlet UITextField *outcomeName;
48+
@property (weak, nonatomic) IBOutlet UITextField *outcomeValueName;
49+
@property (weak, nonatomic) IBOutlet UITextField *outcomeValue;
50+
@property (weak, nonatomic) IBOutlet UITextField *outcomeUniqueName;
51+
@property (weak, nonatomic) IBOutlet UITextView *result;
4652

4753
@end
4854

@@ -118,7 +124,6 @@ - (IBAction)sendTagButton:(id)sender {
118124
[OneSignal IdsAvailable:^(NSString *userId, NSString *pushToken) {
119125
NSLog(@"IdsAvailable Fired");
120126
}];
121-
122127
}
123128

124129
- (IBAction)setEmailButtonPressed:(UIButton *)sender {
@@ -164,4 +169,36 @@ -(BOOL)textFieldShouldReturn:(UITextField *)textField {
164169
return false;
165170
}
166171

172+
- (IBAction)sendTestOutcomeEvent:(UIButton *)sender {
173+
[OneSignal sendOutcome:[_outcomeName text] onSuccess:^(OSOutcomeEvent *outcome) {
174+
dispatch_async(dispatch_get_main_queue(), ^{
175+
_result.text = [NSString stringWithFormat:@"sendTestOutcomeEvent success %@", outcome];
176+
[self.view endEditing:YES];
177+
});
178+
}];
179+
}
180+
- (IBAction)sendValueOutcomeEvent:(id)sender {
181+
if ([_outcomeValue text]) {
182+
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
183+
formatter.numberStyle = NSNumberFormatterDecimalStyle;
184+
NSNumber *value = [formatter numberFromString:[_outcomeValue text]];
185+
186+
[OneSignal sendOutcomeWithValue:[_outcomeValueName text] value:value onSuccess:^(OSOutcomeEvent *outcome) {
187+
dispatch_async(dispatch_get_main_queue(), ^{
188+
_result.text = [NSString stringWithFormat:@"sendValueOutcomeEvent success %@", outcome];
189+
[self.view endEditing:YES];
190+
});
191+
}];
192+
}
193+
}
194+
195+
- (IBAction)sendUniqueOutcomeEvent:(id)sender {
196+
[OneSignal sendUniqueOutcome:[_outcomeUniqueName text] onSuccess:^(OSOutcomeEvent *outcome) {
197+
dispatch_async(dispatch_get_main_queue(), ^{
198+
_result.text = [NSString stringWithFormat:@"sendUniqueOutcomeEvent success %@", outcome];
199+
[self.view endEditing:YES];
200+
});
201+
}];
202+
}
203+
167204
@end

iOS_SDK/OneSignalDevApp/OneSignalNotificationServiceExtension/OneSignalNotificationServiceExtension.entitlements

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<dict>
55
<key>com.apple.security.application-groups</key>
66
<array>
7-
<string>group.com.onesignal.example.testgroup</string>
7+
<string>group.com.onesignal.example.onesignal</string>
88
</array>
99
</dict>
1010
</plist>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>BuildSystemType</key>
6+
<string>Original</string>
7+
</dict>
8+
</plist>

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 234 additions & 18 deletions
Large diffs are not rendered by default.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
28+
#import <Foundation/Foundation.h>
29+
#import "OneSignalCommonDefines.h"
30+
#import "OSBaseFocusTimeProcessor.h"
31+
32+
@interface OSAttributedFocusTimeProcessor : OSBaseFocusTimeProcessor
33+
34+
@end
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
28+
#import <Foundation/Foundation.h>
29+
#import "OneSignalCommonDefines.h"
30+
#import "OSFocusCallParams.h"
31+
32+
// This is an abstract class
33+
@interface OSBaseFocusTimeProcessor : NSObject
34+
35+
@property (nonatomic, readonly) BOOL onFocusCallEnabled;
36+
37+
- (int)getMinSessionTime;
38+
- (NSString*)unsentActiveTimeUserDefaultsKey;
39+
- (BOOL)hasMinSyncTime:(NSTimeInterval)activeTime;
40+
41+
- (void)resetUnsentActiveTime;
42+
- (void)sendOnFocusCall:(OSFocusCallParams *)params;
43+
- (void)sendUnsentActiveTime:(OSFocusCallParams *)params;
44+
- (void)cancelDelayedJob;
45+
46+
- (NSTimeInterval)getUnsentActiveTime;
47+
- (void)saveUnsentActiveTime:(NSTimeInterval)time;
48+
49+
@end

0 commit comments

Comments
 (0)