Skip to content

Commit d446ea4

Browse files
authored
Action manager push fix (#508)
1 parent faf65b0 commit d446ea4

File tree

12 files changed

+227
-155
lines changed

12 files changed

+227
-155
lines changed

LeanplumSDK/LeanplumSDK.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@
438438
6A714BFE26F8B505004A34A9 /* Leanplum_AsyncSocket.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 075AADAA26847EC3007CA1BD /* Leanplum_AsyncSocket.h */; };
439439
6A714C0126F8B88A004A34A9 /* NotificationsManager+Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A714C0026F8B88A004A34A9 /* NotificationsManager+Utilities.swift */; };
440440
6A714C0226F8B88A004A34A9 /* NotificationsManager+Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A714C0026F8B88A004A34A9 /* NotificationsManager+Utilities.swift */; };
441+
6A7B2F52289BC8FC00F73EC7 /* LPActionContextNotification.h in Headers */ = {isa = PBXBuildFile; fileRef = 6A7B2F50289BC8FC00F73EC7 /* LPActionContextNotification.h */; settings = {ATTRIBUTES = (Public, ); }; };
442+
6A7B2F53289BC8FC00F73EC7 /* LPActionContextNotification.h in Headers */ = {isa = PBXBuildFile; fileRef = 6A7B2F50289BC8FC00F73EC7 /* LPActionContextNotification.h */; settings = {ATTRIBUTES = (Public, ); }; };
443+
6A7B2F54289BC8FC00F73EC7 /* LPActionContextNotification.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A7B2F51289BC8FC00F73EC7 /* LPActionContextNotification.m */; };
444+
6A7B2F55289BC8FC00F73EC7 /* LPActionContextNotification.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A7B2F51289BC8FC00F73EC7 /* LPActionContextNotification.m */; };
441445
6A96032A27C69F3C00F34BA0 /* WeakTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A96032927C69F3C00F34BA0 /* WeakTimer.swift */; };
442446
6A96032B27C69F3C00F34BA0 /* WeakTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A96032927C69F3C00F34BA0 /* WeakTimer.swift */; };
443447
6A96032D27C8DD1300F34BA0 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A96032C27C8DD1200F34BA0 /* User.swift */; };
@@ -775,6 +779,8 @@
775779
6A265E2627187EBB0074354F /* NSObject+Notifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSObject+Notifications.swift"; sourceTree = "<group>"; };
776780
6A714BA326F8B317004A34A9 /* Leanplum.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Leanplum.framework; sourceTree = BUILT_PRODUCTS_DIR; };
777781
6A714C0026F8B88A004A34A9 /* NotificationsManager+Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NotificationsManager+Utilities.swift"; sourceTree = "<group>"; };
782+
6A7B2F50289BC8FC00F73EC7 /* LPActionContextNotification.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LPActionContextNotification.h; sourceTree = "<group>"; };
783+
6A7B2F51289BC8FC00F73EC7 /* LPActionContextNotification.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPActionContextNotification.m; sourceTree = "<group>"; };
778784
6A96032927C69F3C00F34BA0 /* WeakTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakTimer.swift; sourceTree = "<group>"; };
779785
6A96032C27C8DD1200F34BA0 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
780786
6A98A9BB272A941D00D8753A /* NotificationsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsManager.swift; sourceTree = "<group>"; };
@@ -1156,6 +1162,8 @@
11561162
075AADCB26847EC3007CA1BD /* LPActionContext.m */,
11571163
075AADCC26847EC3007CA1BD /* LPActionContext-Internal.h */,
11581164
075AADCD26847EC3007CA1BD /* LPActionArg.m */,
1165+
6A7B2F50289BC8FC00F73EC7 /* LPActionContextNotification.h */,
1166+
6A7B2F51289BC8FC00F73EC7 /* LPActionContextNotification.m */,
11591167
);
11601168
path = Actions;
11611169
sourceTree = "<group>";
@@ -1352,6 +1360,7 @@
13521360
buildActionMask = 2147483647;
13531361
files = (
13541362
075AAECC26847EC4007CA1BD /* LPConstants.h in Headers */,
1363+
6A7B2F52289BC8FC00F73EC7 /* LPActionContextNotification.h in Headers */,
13551364
075AAEB326847EC4007CA1BD /* LPActionTriggerManager.h in Headers */,
13561365
075AAE5226847EC4007CA1BD /* LPWebInterstitialViewController.h in Headers */,
13571366
075AAE1426847EC4007CA1BD /* LPRequestFactory.h in Headers */,
@@ -1440,6 +1449,7 @@
14401449
buildActionMask = 2147483647;
14411450
files = (
14421451
6A714AF326F8B317004A34A9 /* LPConstants.h in Headers */,
1452+
6A7B2F53289BC8FC00F73EC7 /* LPActionContextNotification.h in Headers */,
14431453
6A714AF426F8B317004A34A9 /* LPActionTriggerManager.h in Headers */,
14441454
6A714AF526F8B317004A34A9 /* LPWebInterstitialViewController.h in Headers */,
14451455
6A714AF626F8B317004A34A9 /* LPRequestFactory.h in Headers */,
@@ -1661,6 +1671,7 @@
16611671
C9374A39272C36B200F4915A /* Utilities.swift in Sources */,
16621672
39C081952781D34700C1DBD6 /* ActionManager+ActionDefinition.swift in Sources */,
16631673
075AAEC426847EC4007CA1BD /* LPEventDataManager.m in Sources */,
1674+
6A7B2F54289BC8FC00F73EC7 /* LPActionContextNotification.m in Sources */,
16641675
075AAE5326847EC4007CA1BD /* LPInterstitialViewController.m in Sources */,
16651676
6A07FDAA28352DCE00995BE3 /* ContentMerger.swift in Sources */,
16661677
075AAEA226847EC4007CA1BD /* UIDevice+IdentifierAddition.m in Sources */,
@@ -1778,6 +1789,7 @@
17781789
C9374A3A272C36B200F4915A /* Utilities.swift in Sources */,
17791790
6A714B4F26F8B317004A34A9 /* LPEventDataManager.m in Sources */,
17801791
6A714B5026F8B317004A34A9 /* LPInterstitialViewController.m in Sources */,
1792+
6A7B2F55289BC8FC00F73EC7 /* LPActionContextNotification.m in Sources */,
17811793
6A714B5126F8B317004A34A9 /* UIDevice+IdentifierAddition.m in Sources */,
17821794
6A07FDAB28352DCE00995BE3 /* ContentMerger.swift in Sources */,
17831795
6A714B5226F8B317004A34A9 /* LPEventCallbackManager.m in Sources */,

LeanplumSDK/LeanplumSDK/Classes/Features/Actions/LPActionContext-Internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ NS_ASSUME_NONNULL_BEGIN
1414

1515
@interface LPActionContext ()
1616

17+
- (instancetype)init:(NSString *)name
18+
args:(NSDictionary *)args
19+
messageId:(NSString *)messageId
20+
preventRealtimeUpdating:(BOOL)preventRealtimeUpdating;
21+
1722
+ (LPActionContext *)actionContextWithName:(NSString *)name
1823
args:(nullable NSDictionary *)args
1924
messageId:(nullable NSString *)messageId;

LeanplumSDK/LeanplumSDK/Classes/Features/Actions/LPActionContext.m

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,45 @@ @interface LPActionContext()
3838

3939
@implementation LPActionContext
4040

41-
@synthesize name=_name;
42-
@synthesize messageId=_messageId;
43-
@synthesize originalMessageId=_originalMessageId;
44-
@synthesize priority=_priority;
4541
@synthesize args=_args;
46-
@synthesize parentContext=_parentContext;
47-
@synthesize contentVersion=_contentVersion;
48-
@synthesize key=_key;
49-
@synthesize preventRealtimeUpdating=_preventRealtimeUpdating;
50-
@synthesize contextualValues=_contextualValues;
51-
@synthesize countAggregator=_countAggregator;
42+
43+
- (instancetype)initWithName:(NSString *)name
44+
args:(NSDictionary *)args
45+
messageId:(NSString *)messageId
46+
originalMessageId:(NSString *)originalMessageId
47+
priority:(NSNumber *)priority
48+
{
49+
self = [super init];
50+
if (self) {
51+
_name = name;
52+
_args = args;
53+
_messageId = messageId;
54+
_originalMessageId = originalMessageId;
55+
_contentVersion = [[LPVarCache sharedCache] contentVersion];
56+
_preventRealtimeUpdating = NO;
57+
_isRooted = YES;
58+
_isPreview = NO;
59+
_priority = priority;
60+
_countAggregator = [LPCountAggregator sharedAggregator];
61+
}
62+
63+
return self;
64+
}
65+
66+
- (instancetype)init:(NSString *)name
67+
args:(NSDictionary *)args
68+
messageId:(NSString *)messageId
69+
preventRealtimeUpdating:(BOOL)preventRealtimeUpdating;
70+
{
71+
self = [self initWithName:name
72+
args:args
73+
messageId:messageId
74+
originalMessageId:nil
75+
priority:[NSNumber numberWithInt:DEFAULT_PRIORITY]];
76+
_preventRealtimeUpdating = preventRealtimeUpdating;
77+
78+
return self;
79+
}
5280

5381
+ (LPActionContext *)actionContextWithName:(NSString *)name
5482
args:(NSDictionary *)args
@@ -68,18 +96,11 @@ + (LPActionContext *)actionContextWithName:(NSString *)name
6896
priority:(NSNumber *)priority
6997

7098
{
71-
LPActionContext *context = [[LPActionContext alloc] init];
72-
context->_name = name;
73-
context->_args = args;
74-
context->_messageId = messageId;
75-
context->_originalMessageId = originalMessageId;
76-
context->_contentVersion = [[LPVarCache sharedCache] contentVersion];
77-
context->_preventRealtimeUpdating = NO;
78-
context->_isRooted = YES;
79-
context->_isPreview = NO;
80-
context->_priority = priority;
81-
context->_countAggregator = [LPCountAggregator sharedAggregator];
82-
return context;
99+
return [[LPActionContext alloc] initWithName:name
100+
args:args
101+
messageId:messageId
102+
originalMessageId:originalMessageId
103+
priority:priority];
83104
}
84105

85106
- (NSDictionary *)defaultValues
@@ -133,7 +154,7 @@ - (id)objectNamed:(NSString *)name
133154
LP_TRY
134155
[self setProperArgs];
135156
return [[LPVarCache sharedCache] getValueFromComponentArray:[[LPVarCache sharedCache] getNameComponents:name]
136-
fromDict:_args];
157+
fromDict:_args];
137158
LP_END_TRY
138159
return nil;
139160
}
@@ -152,9 +173,9 @@ - (NSString *)fillTemplate:(NSString *)value
152173
if (!_contextualValues || !value || [value rangeOfString:@"##"].location == NSNotFound) {
153174
return value;
154175
}
155-
176+
156177
NSDictionary *parameters = _contextualValues.parameters;
157-
178+
158179
for (NSString *parameterName in [parameters keyEnumerator]) {
159180
NSString *placeholder = [NSString stringWithFormat:@"##Parameter %@##", parameterName];
160181
value = [value stringByReplacingOccurrencesOfString:placeholder
@@ -165,7 +186,7 @@ - (NSString *)fillTemplate:(NSString *)value
165186
value = [value
166187
stringByReplacingOccurrencesOfString:@"##Previous Value##"
167188
withString:[_contextualValues
168-
.previousAttributeValue description]];
189+
.previousAttributeValue description]];
169190
}
170191
if (_contextualValues.attributeValue) {
171192
value = [value stringByReplacingOccurrencesOfString:@"##Value##"
@@ -204,30 +225,30 @@ - (NSURL *)htmlWithTemplateNamed:(NSString *)templateName
204225
"Empty name parameter provided."];
205226
return nil;
206227
}
207-
228+
208229
LP_TRY
209230
[self setProperArgs];
210-
231+
211232
NSMutableDictionary *htmlVars = [self replaceFileNameToLocalFilePath:[_args mutableCopy]
212233
preserveFileNamed:templateName];
213234
htmlVars[@"messageId"] = self.messageId;
214-
235+
215236
// Triggering Event.
216237
if (self.contextualValues && self.contextualValues.arguments) {
217238
htmlVars[@"displayEvent"] = self.contextualValues.arguments;
218239
}
219-
240+
220241
// Add HTML Vars.
221242
NSString *jsonString = [LPJSON stringFromJSON:htmlVars];
222243
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
223-
244+
224245
// Template.
225246
NSString *htmlString = [self htmlStringContentsOfFile:[self fileNamed:templateName]];
226247
if (!htmlString) {
227248
LPLog(LPError, @"Fail to get HTML template.");
228249
return nil;
229250
}
230-
251+
231252
if ([[htmlVars valueForKey:@"Height"] isEqualToString:@"100%"] && [[htmlVars valueForKey:@"Width"] isEqualToString:@"100%"]) {
232253
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"/*##MEDIAQUERY##" withString:@""];
233254
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"##MEDIAQUERY##*/" withString:@""];
@@ -239,16 +260,16 @@ - (NSURL *)htmlWithTemplateNamed:(NSString *)templateName
239260

240261
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"##Vars##"
241262
withString:jsonString];
242-
263+
243264
htmlString = [self fillTemplate:htmlString];
244-
265+
245266
// Save filled template to temp file which will be used by WebKit
246267
NSString *randomUUID = [[[NSUUID UUID] UUIDString] lowercaseString];
247268
NSString *tmpPath = [LPFileManager fileRelativeToDocuments:randomUUID createMissingDirectories:YES];
248269
NSURL *tmpURL = [[NSURL fileURLWithPath:tmpPath] URLByAppendingPathExtension:@"html"];
249-
270+
250271
[htmlString writeToURL:tmpURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
251-
272+
252273
return tmpURL;
253274
LP_END_TRY
254275
return nil;
@@ -344,7 +365,7 @@ - (NSDictionary *)dictionaryNamed:(NSString *)name
344365
if ([object isKindOfClass:[NSDictionary class]]) {
345366
return (NSDictionary *) object;
346367
}
347-
368+
348369
if ([object isKindOfClass:[NSString class]]) {
349370
return [LPJSON JSONFromString:object];
350371
}
@@ -377,7 +398,7 @@ - (NSDictionary *)getChildArgs:(NSString *)name
377398
if (![actionArgs isKindOfClass:[NSDictionary class]]) {
378399
return nil;
379400
}
380-
401+
381402
NSDictionary *defaultArgs = [[[LPActionManager shared] definitionWithName:actionArgs[LP_VALUE_ACTION_ARG]] values];
382403
actionArgs = [ContentMerger mergeWithVars:defaultArgs diff:actionArgs];
383404

@@ -415,14 +436,14 @@ - (NSString *)eventWithParentEventNamesFromEvent:(NSString *)event
415436
options:NSCaseInsensitiveSearch
416437
range:NSMakeRange(0,
417438
actionName.length)
418-
];
419-
439+
];
440+
420441
if (fullEventName.length) {
421442
[fullEventName appendString:@" "];
422443
}
423444
[fullEventName appendString:actionName];
424445
}
425-
446+
426447
return fullEventName;
427448
LP_END_TRY
428449
}
@@ -435,12 +456,12 @@ - (void)runActionNamed:(NSString *)name
435456
return;
436457
}
437458
NSDictionary *args = [self getChildArgs:name];
438-
459+
439460
__weak LPActionContext *weakSelf = self;
440461
if ([self actionDidExecute]) {
441462
LPActionContext *actionNamedContext = [LPActionContext
442-
actionContextWithName:name
443-
args:args messageId:_messageId];
463+
actionContextWithName:name
464+
args:args messageId:_messageId];
444465
actionNamedContext->_parentContext = weakSelf;
445466
// notifies our LPActionManager that the action was executed
446467
self.actionDidExecute(actionNamedContext);
@@ -449,11 +470,11 @@ - (void)runActionNamed:(NSString *)name
449470
if (!args) {
450471
return;
451472
}
452-
473+
453474
// Chain to existing message.
454475
NSString *messageId = args[LP_VALUE_CHAIN_MESSAGE_ARG];
455476
NSString *actionType = args[LP_VALUE_ACTION_ARG];
456-
477+
457478
void (^executeChainedMessage)(void) = ^void(void) {
458479
LPActionContext *chainedActionContext = [Leanplum createActionContextForMessageId:messageId];
459480
chainedActionContext.contextualValues = self.contextualValues;
@@ -465,12 +486,13 @@ - (void)runActionNamed:(NSString *)name
465486
[[LPActionManager shared] triggerWithContexts:@[chainedActionContext] priority:PriorityHigh trigger:nil];
466487
});
467488
};
468-
489+
469490
if (messageId && [actionType isEqualToString:LP_VALUE_CHAIN_MESSAGE_ACTION_NAME]) {
470491
NSDictionary *message = [[LPActionManager shared] messages][messageId];
471492
if (message) {
472493
executeChainedMessage();
473494
} else {
495+
BOOL previousPauseState = LPActionManager.shared.isPaused;
474496
LPActionManager.shared.isPaused = YES;
475497
// Message doesn't seem to be on the device,
476498
// so let's forceContentUpdate and retry showing it.
@@ -479,7 +501,7 @@ - (void)runActionNamed:(NSString *)name
479501
if (message) {
480502
executeChainedMessage();
481503
}
482-
LPActionManager.shared.isPaused = NO;
504+
LPActionManager.shared.isPaused = previousPauseState;
483505
}];
484506
}
485507
} else {
@@ -496,7 +518,7 @@ - (void)runActionNamed:(NSString *)name
496518
[[LPActionManager shared] triggerWithContexts:@[childContext] priority:PriorityHigh trigger:nil];
497519
});
498520
}
499-
521+
500522
LP_END_TRY
501523

502524
[self.countAggregator incrementCount:@"run_action_named"];
@@ -510,6 +532,7 @@ - (void)runTrackedActionNamed:(NSString *)name
510532
@"provided."];
511533
return;
512534
}
535+
513536
[self trackMessageEvent:name withValue:0.0 andInfo:nil andParameters:nil];
514537
}
515538
[self runActionNamed:name];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// LPActionContextNotification.h
3+
// LeanplumSDK
4+
//
5+
// Created by Nikola Zagorchev on 4.08.22.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#import "LPActionContext.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
/**
14+
* LPActionContext subclass used for presenting Push Notification as a Confirm In-app message
15+
* when Notification is received when App is in foreground.
16+
* Do NOT use except for the above-mentioned use case.
17+
*/
18+
NS_SWIFT_NAME(ActionContextNotification)
19+
@interface LPActionContextNotification : LPActionContext {
20+
}
21+
22+
- (void)runTrackedActionNamed:(NSString *)name;
23+
@end
24+
25+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)