Skip to content

Commit 6823096

Browse files
committed
Fix session logic from a notification clicked
* This was a remaining TODO * Moved where `appEntryState` lives away from `OneSignal` to the `OneSignalOutcomes` module, and lives on `OSSessionManager` * Also server expects session_time as an int, not float
1 parent 9ef9c26 commit 6823096

File tree

7 files changed

+37
-39
lines changed

7 files changed

+37
-39
lines changed

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ of this software and associated documentation files (the "Software"), to deal
3434
#import "OneSignalWebViewManager.h"
3535
#import "UNUserNotificationCenter+OneSignalNotifications.h"
3636
#import "UIApplicationDelegate+OneSignalNotifications.h"
37+
#import <OneSignalOutcomes/OSSessionManager.h>
3738

3839
@implementation OSNotificationClickEvent
3940
@synthesize notification = _notification, result = _result;
@@ -696,10 +697,10 @@ + (void)handleNotificationOpened:(NSDictionary*)messageDict
696697

697698
// Call Action Block
698699
[self lastMessageReceived:messageDict];
699-
// if (!isActive) { TODO: Figure out session stuff from notif opened
700-
// OneSignal.appEntryState = NOTIFICATION_CLICK;
701-
// [[OSSessionManager sharedSessionManager] onDirectInfluenceFromNotificationOpen:_appEntryState withNotificationId:messageId];
702-
// }
700+
if (!isActive) {
701+
OSSessionManager.sharedSessionManager.appEntryState = NOTIFICATION_CLICK;
702+
[[OSSessionManager sharedSessionManager] onDirectInfluenceFromNotificationOpen:NOTIFICATION_CLICK withNotificationId:messageId];
703+
}
703704

704705
[self handleNotificationActionWithUrl:notification.launchURL actionID:actionID];
705706
}

iOS_SDK/OneSignalSDK/OneSignalOutcomes/Source/OutcomeEvents/OSOutcomesRequests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ + (instancetype _Nonnull)withActiveTime:(NSNumber * _Nonnull)activeTime
138138
let params = [NSMutableDictionary new];
139139
params[@"app_id"] = appId;
140140
params[@"id"] = @"os__session_duration";
141-
params[@"session_time"] = activeTime;
141+
params[@"session_time"] = @([activeTime intValue]);
142142
params[@"subscription"] = @{@"id": pushSubscriptionId, @"type": @"iOSPush"};
143143
params[@"onesignal_id"] = onesignalId;
144144

iOS_SDK/OneSignalSDK/OneSignalOutcomes/Source/OutcomeEvents/OSSessionManager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,20 @@
4141
+ (void)resetSharedSessionManager;
4242

4343
@property (nonatomic) id<SessionStatusDelegate> _Nullable delegate;
44+
@property AppEntryAction appEntryState;
4445

4546
- (instancetype _Nonnull)init:(Class<SessionStatusDelegate> _Nullable)delegate withTrackerFactory:(OSTrackerFactory *_Nonnull)trackerFactory;
4647

4748
- (NSArray<OSInfluence *> *_Nonnull)getInfluences;
4849
- (NSArray<OSInfluence *> *_Nonnull)getSessionInfluences;
4950
- (void)initSessionFromCache;
50-
- (void)restartSessionIfNeeded:(AppEntryAction)entryAction;
51+
- (void)restartSessionIfNeeded;
5152
- (void)onInAppMessageReceived:(NSString * _Nonnull)messageId;
5253
- (void)onDirectInfluenceFromIAMClick:(NSString * _Nonnull)directIAMId;
5354
- (void)onDirectInfluenceFromIAMClickFinished;
5455
- (void)onNotificationReceived:(NSString * _Nonnull)notificationId;
5556
- (void)onDirectInfluenceFromNotificationOpen:(AppEntryAction)entryAction withNotificationId:(NSString * _Nonnull)directNotificationId;
56-
- (void)attemptSessionUpgrade:(AppEntryAction)entryAction;
57-
- (NSDate *)sessionLaunchTime;
57+
- (void)attemptSessionUpgrade;
58+
- (NSDate *_Nullable)sessionLaunchTime;
5859

5960
@end

iOS_SDK/OneSignalSDK/OneSignalOutcomes/Source/OutcomeEvents/OSSessionManager.m

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ @implementation OSSessionManager
4141
static OSSessionManager *_sessionManager;
4242

4343
NSDate *_sessionLaunchTime;
44+
AppEntryAction _appEntryState = APP_CLOSE;
4445

4546
+ (OSSessionManager*)sharedSessionManager {
4647
if (!_sessionManager)
@@ -52,6 +53,14 @@ + (void)resetSharedSessionManager {
5253
_sessionManager = nil;
5354
}
5455

56+
- (AppEntryAction)appEntryState {
57+
return _appEntryState;
58+
}
59+
60+
- (void)setAppEntryState:(AppEntryAction)appEntryState {
61+
_appEntryState = appEntryState;
62+
}
63+
5564
- (instancetype _Nonnull)init:(id<SessionStatusDelegate>)delegate withTrackerFactory:(OSTrackerFactory *)trackerFactory {
5665
if (self = [super init]) {
5766
_delegate = delegate;
@@ -75,11 +84,11 @@ - (void)initSessionFromCache {
7584
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"SessionManager restored from cache with influences: %@", [self getInfluences].description]];
7685
}
7786

78-
- (void)restartSessionIfNeeded:(AppEntryAction)entryAction {
79-
NSArray<OSChannelTracker *> *channelTrackers = [_trackerFactory channelsToResetByEntryAction:entryAction];
87+
- (void)restartSessionIfNeeded {
88+
NSArray<OSChannelTracker *> *channelTrackers = [_trackerFactory channelsToResetByEntryAction:_appEntryState];
8089
NSMutableArray<OSInfluence *> *updatedInfluences = [NSMutableArray new];
8190

82-
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OneSignal SessionManager restartSessionIfNeeded with entryAction:: %u channelTrackers: %@", entryAction, channelTrackers.description]];
91+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OneSignal SessionManager restartSessionIfNeeded with entryAction:: %u channelTrackers: %@", _appEntryState, channelTrackers.description]];
8392

8493
for (OSChannelTracker *channelTracker in channelTrackers) {
8594
NSArray *lastIds = [channelTracker lastReceivedIds];
@@ -134,12 +143,13 @@ - (void)onNotificationReceived:(NSString *)notificationId {
134143
}
135144

136145
- (void)onDirectInfluenceFromNotificationOpen:(AppEntryAction)entryAction withNotificationId:(NSString *)directNotificationId {
146+
_appEntryState = entryAction;
137147
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OneSignal SessionManager onDirectInfluenceFromNotificationOpen notificationId: %@", directNotificationId]];
138148

139149
if (!directNotificationId || directNotificationId.length == 0)
140150
return;
141151

142-
[self attemptSessionUpgrade:entryAction withDirectId:directNotificationId];
152+
[self attemptSessionUpgradeWithDirectId:directNotificationId];
143153
}
144154

145155
/*
@@ -150,15 +160,15 @@ - (void)onDirectInfluenceFromNotificationOpen:(AppEntryAction)entryAction withNo
150160
* INDIRECT -> DIRECT
151161
* DIRECT -> DIRECT
152162
*/
153-
- (void)attemptSessionUpgrade:(AppEntryAction)entryAction {
154-
[self attemptSessionUpgrade:entryAction withDirectId:nil];
163+
- (void)attemptSessionUpgrade {
164+
[self attemptSessionUpgradeWithDirectId:nil];
155165
}
156166

157-
- (void)attemptSessionUpgrade:(AppEntryAction)entryAction withDirectId:(NSString *)directId {
158-
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OneSignal SessionManager attemptSessionUpgrade with entryAction: %u", entryAction]];
167+
- (void)attemptSessionUpgradeWithDirectId:(NSString *)directId {
168+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OneSignal SessionManager attemptSessionUpgrade with entryAction: %u", _appEntryState]];
159169

160-
OSChannelTracker *channelTrackerByAction = [_trackerFactory channelByEntryAction:entryAction];
161-
NSArray<OSChannelTracker *> *channelTrackersToReset = [_trackerFactory channelsToResetByEntryAction:entryAction];
170+
OSChannelTracker *channelTrackerByAction = [_trackerFactory channelByEntryAction:_appEntryState];
171+
NSArray<OSChannelTracker *> *channelTrackersToReset = [_trackerFactory channelsToResetByEntryAction:_appEntryState];
162172
NSMutableArray<OSInfluence *> *influencesToEnd = [NSMutableArray new];
163173
OSInfluence *lastInfluence = nil;
164174

@@ -190,7 +200,7 @@ - (void)attemptSessionUpgrade:(AppEntryAction)entryAction withDirectId:(NSString
190200
if (channelTracker.influenceType == UNATTRIBUTED) {
191201
NSArray *lastIds = [channelTracker lastReceivedIds];
192202
// There are new ids for attribution and the application was open again without resetting session
193-
if (lastIds.count > 0 && entryAction != APP_CLOSE) {
203+
if (lastIds.count > 0 && _appEntryState != APP_CLOSE) {
194204
// Save influence to ended it later if needed
195205
// This influence will be unattributed
196206
OSInfluence *influence = [channelTracker currentSessionInfluence];

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,6 @@ + (OneSignalReceiveReceiptsController*)receiveReceiptsController {
137137
return _receiveReceiptsController;
138138
}
139139

140-
static AppEntryAction _appEntryState = APP_CLOSE;
141-
+ (AppEntryAction)appEntryState {
142-
return _appEntryState;
143-
}
144-
145-
+ (void)setAppEntryState:(AppEntryAction)appEntryState {
146-
_appEntryState = appEntryState;
147-
}
148-
149140
+ (NSString*)appId {
150141
return appId;
151142
}
@@ -391,7 +382,7 @@ + (void)startNewSessionInternal {
391382

392383
[OSOutcomes.sharedController clearOutcomes];
393384

394-
[[OSSessionManager sharedSessionManager] restartSessionIfNeeded:_appEntryState];
385+
[[OSSessionManager sharedSessionManager] restartSessionIfNeeded];
395386

396387
[OneSignalTrackFirebaseAnalytics trackInfluenceOpenEvent];
397388

@@ -797,6 +788,7 @@ + (void)load {
797788

798789
[[OSMigrationController new] migrate];
799790
// sessionLaunchTime = [NSDate date];
791+
// TODO: sessionLaunchTime used to always be set in load
800792

801793
[OSDialogInstanceManager setSharedInstance:[OneSignalDialogController sharedInstance]];
802794
}

iOS_SDK/OneSignalSDK/Source/OneSignalInternal.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,9 @@
3939

4040
@interface OneSignal (OneSignalInternal)
4141

42-
+ (NSDate *_Nonnull)sessionLaunchTime;
43-
44-
4542
@property (class, readonly) BOOL didCallDownloadParameters;
4643
@property (class, readonly) BOOL downloadedParameters;
4744

48-
@property (class) AppEntryAction appEntryState;
49-
@property (class) OSSessionManager* _Nonnull sessionManager;
50-
5145
@end
5246

5347
#endif /* OneSignalInternal_h */

iOS_SDK/OneSignalSDK/Source/OneSignalTracker.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ + (void)applicationForegrounded {
9898
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Foregrounded started"];
9999
[OSFocusTimeProcessorFactory cancelFocusCall];
100100

101-
if (OneSignal.appEntryState != NOTIFICATION_CLICK)
102-
OneSignal.appEntryState = APP_OPEN;
101+
if (OSSessionManager.sharedSessionManager.appEntryState != NOTIFICATION_CLICK)
102+
OSSessionManager.sharedSessionManager.appEntryState = APP_OPEN;
103103

104104
lastOpenedTime = [NSDate date].timeIntervalSince1970;
105105

@@ -109,7 +109,7 @@ + (void)applicationForegrounded {
109109
else {
110110
// This checks if notification permissions changed when app was backgrounded
111111
[OSNotificationsManager sendNotificationTypesUpdateToDelegate];
112-
[[OSSessionManager sharedSessionManager] attemptSessionUpgrade:OneSignal.appEntryState];
112+
[[OSSessionManager sharedSessionManager] attemptSessionUpgrade];
113113
// TODO: Here it used to call receivedInAppMessageJson with nil, this method no longer exists
114114
// [OneSignal receivedInAppMessageJson:nil];
115115
}
@@ -125,7 +125,7 @@ + (void)applicationBackgrounded {
125125
if (timeElapsed < -1)
126126
return;
127127

128-
OneSignal.appEntryState = APP_CLOSE;
128+
OSSessionManager.sharedSessionManager.appEntryState = APP_CLOSE;
129129

130130
let influences = [[OSSessionManager sharedSessionManager] getSessionInfluences];
131131
let focusCallParams = [self createFocusCallParams:influences onSessionEnded:false];

0 commit comments

Comments
 (0)