Skip to content

Commit 84dc315

Browse files
authored
add callback on message displayed (#225)
1 parent b9acece commit 84dc315

File tree

7 files changed

+175
-1
lines changed

7 files changed

+175
-1
lines changed

Example/Tests/Classes/LeanplumTest.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#import <XCTest/XCTest.h>
2727
#import <UIKit/UIKit.h>
28+
#import <OCMock/OCMock.h>
2829
#import <OHHTTPStubs/OHHTTPStubs.h>
2930
#import <OHHTTPStubs/OHPathHelpers.h>
3031
#import "LeanplumHelper.h"
@@ -46,6 +47,7 @@ @interface Leanplum (Test)
4647

4748
+ (NSSet<NSString *> *)parseEnabledCountersFromResponse:(NSDictionary *)response;
4849
+ (NSSet<NSString *> *)parseEnabledFeatureFlagsFromResponse:(NSDictionary *)response;
50+
+ (void)triggerMessageDisplayed:(LPActionContext *)context;
4951

5052
@end
5153

@@ -1781,4 +1783,40 @@ - (void)on_start_response:(BOOL) success
17811783
XCTAssertTrue(success);
17821784
}
17831785

1786+
/**
1787+
* Test that method triggerMessageDisplayed calls user defined callback
1788+
*/
1789+
-(void)test_triggerMessageDisplayedCallsCallback
1790+
{
1791+
__block BOOL blockCalled = NO;
1792+
1793+
NSString *messageID = @"testMessageID";
1794+
NSString *messageBody = @"testMessageBody";
1795+
NSString *recipientUserID = @"recipientUserID";
1796+
1797+
LPActionContext *actionContext = [[LPActionContext alloc] init];
1798+
id actionContextMock = OCMPartialMock(actionContext);
1799+
1800+
OCMStub([actionContextMock messageId]).andReturn(messageID);
1801+
OCMStub([actionContextMock args]).andReturn(@{@"Message":messageBody});
1802+
1803+
id leanplumMock = OCMClassMock([Leanplum class]);
1804+
OCMStub([leanplumMock userId]).andReturn(recipientUserID);
1805+
1806+
LeanplumMessageDisplayedCallbackBlock block =
1807+
^void(LPMessageArchiveData *messageArchiveData) {
1808+
blockCalled = YES;
1809+
XCTAssertEqual(messageArchiveData.messageID, messageID);
1810+
XCTAssertEqual(messageArchiveData.messageBody, messageBody);
1811+
XCTAssertEqual(messageArchiveData.recipientUserID, recipientUserID);
1812+
NSDate *now = [NSDate date];
1813+
NSTimeInterval interval = [now timeIntervalSinceDate:messageArchiveData.deliveryDateTime];
1814+
XCTAssertTrue(interval < 1000);
1815+
};
1816+
[Leanplum onMessageDisplayed:block];
1817+
[Leanplum triggerMessageDisplayed:actionContext];
1818+
1819+
XCTAssertTrue(blockCalled);
1820+
}
1821+
17841822
@end

Leanplum-SDK/Classes/Internal/LPInternalState.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@property(strong, nonatomic) NSMutableArray *startBlocks, *variablesChangedBlocks,
1515
*interfaceChangedBlocks, *eventsChangedBlocks, *noDownloadsBlocks, *onceNoDownloadsBlocks,
16-
*startIssuedBlocks;
16+
*startIssuedBlocks, *messageDisplayedBlocks;
1717
@property(strong, nonatomic) NSMutableDictionary *actionBlocks, *actionResponders;
1818
@property(strong, nonatomic) NSMutableSet *startResponders, *variablesChangedResponders,
1919
*interfaceChangedResponders, *eventsChangedResponders, *noDownloadsResponders;

Leanplum-SDK/Classes/Internal/LPInternalState.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ - (id)init {
2626
_eventsChangedBlocks = nil;
2727
_noDownloadsBlocks = nil;
2828
_onceNoDownloadsBlocks = nil;
29+
_messageDisplayedBlocks = nil;
2930
_actionBlocks = nil;
3031
_actionResponders = nil;
3132
_startResponders = nil;

Leanplum-SDK/Classes/Internal/Leanplum.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,29 @@ + (void)triggerVariablesChangedAndNoDownloadsPending
507507
LP_END_USER_CODE
508508
}
509509

510+
+ (void)triggerMessageDisplayed:(LPActionContext *)context
511+
{
512+
LP_BEGIN_USER_CODE
513+
NSString *messageID = context.messageId;
514+
NSString *messageKey = @"Message";
515+
NSString *messageBody = @"";
516+
if ([context.args valueForKey:messageKey]) {
517+
messageBody = [context.args valueForKey:messageKey];
518+
}
519+
NSString *recipientUserID = [Leanplum userId];
520+
NSDate *deliveryDateTime = [NSDate date];
521+
for (LeanplumMessageDisplayedCallbackBlock block in [LPInternalState sharedState]
522+
.messageDisplayedBlocks.copy) {
523+
LPMessageArchiveData *messageArchiveData = [[LPMessageArchiveData alloc]
524+
initWithMessageID: messageID
525+
messageBody:messageBody
526+
recipientUserID:recipientUserID
527+
deliveryDateTime:deliveryDateTime];
528+
block(messageArchiveData);
529+
}
530+
LP_END_USER_CODE
531+
}
532+
510533
+ (void)triggerAction:(LPActionContext *)context
511534
{
512535
[self triggerAction:context handledBlock:nil];
@@ -534,6 +557,9 @@ + (void)triggerAction:(LPActionContext *)context handledBlock:(LeanplumHandledBl
534557

535558
if (handledBlock) {
536559
handledBlock(handled);
560+
if (handled) {
561+
[Leanplum triggerMessageDisplayed:context];
562+
}
537563
}
538564
};
539565

@@ -1477,6 +1503,21 @@ + (void)onceVariablesChangedAndNoDownloadsPending:(LeanplumVariablesChangedBlock
14771503
}
14781504
}
14791505

1506+
1507+
+ (void)onMessageDisplayed:(LeanplumMessageDisplayedCallbackBlock)block {
1508+
if (!block) {
1509+
[self throwError:@"[Leanplum onMessageDisplayed:] Nil block "
1510+
@"parameter provided."];
1511+
return;
1512+
}
1513+
LP_TRY
1514+
if (![LPInternalState sharedState].messageDisplayedBlocks) {
1515+
[LPInternalState sharedState].messageDisplayedBlocks = [NSMutableArray array];
1516+
}
1517+
[[LPInternalState sharedState].messageDisplayedBlocks addObject:[block copy]];
1518+
LP_END_TRY
1519+
}
1520+
14801521
+ (void)clearUserContent {
14811522
[[LPVarCache sharedCache] clearUserContent];
14821523
[[LPCountAggregator sharedAggregator] incrementCount:@"clear_user_content"];

Leanplum-SDK/Classes/Leanplum.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#import "LPActionArg.h"
2828
#import "LPActionContext.h"
2929
#import "LPVar.h"
30+
#import "LPMessageArchiveData.h"
3031

3132
#ifndef LP_NOT_TV
3233
#if (!defined(TARGET_OS_TV) || !TARGET_OS_TV)
@@ -371,6 +372,13 @@ typedef enum {
371372
*/
372373
+ (void)onceVariablesChangedAndNoDownloadsPending:(LeanplumVariablesChangedBlock)block;
373374

375+
typedef void (^LeanplumMessageDisplayedCallbackBlock)(LPMessageArchiveData *messageArchiveData);
376+
377+
/**
378+
* Block to call when a message is displayed to the user.
379+
*/
380+
+ (void)onMessageDisplayed:(LeanplumMessageDisplayedCallbackBlock)block;
381+
374382
/**
375383
* Clears cached values for messages, variables and test assignments.
376384
* Use sparingly as if the app is updated, you'll have to deal with potentially
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// LPMessageArchiveData.h
3+
// Leanplum iOS SDK Version 2.0.6
4+
//
5+
// Copyright (c) 2012 Leanplum, Inc. All rights reserved.
6+
//
7+
// Licensed to the Apache Software Foundation (ASF) under one
8+
// or more contributor license agreements. See the NOTICE file
9+
// distributed with this work for additional information
10+
// regarding copyright ownership. The ASF licenses this file
11+
// to you under the Apache License, Version 2.0 (the "License");
12+
// you may not use this file except in compliance with the License.
13+
// You may obtain a copy of the License at
14+
//
15+
// http://www.apache.org/licenses/LICENSE-2.0
16+
//
17+
// Unless required by applicable law or agreed to in writing,
18+
// software distributed under the License is distributed on an
19+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
// KIND, either express or implied. See the License for the
21+
// specific language governing permissions and limitations
22+
// under the License.
23+
24+
#import <Foundation/Foundation.h>
25+
26+
NS_ASSUME_NONNULL_BEGIN
27+
28+
@interface LPMessageArchiveData : NSObject
29+
30+
-(instancetype)init NS_UNAVAILABLE;
31+
-(instancetype)initWithMessageID:(NSString *)messageID
32+
messageBody:(NSString *)messageBody
33+
recipientUserID:(NSString *)recipientUserID
34+
deliveryDateTime:(NSDate *)deliveryDateTime;
35+
36+
@property (nonatomic, copy) NSString *messageID;
37+
@property (nonatomic, copy) NSString *messageBody;
38+
@property (nonatomic, copy) NSString *recipientUserID;
39+
@property (nonatomic, copy) NSDate *deliveryDateTime;
40+
41+
@end
42+
43+
NS_ASSUME_NONNULL_END
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// LPMessageArchiveData.m
3+
// Leanplum iOS SDK Version 2.0.6
4+
//
5+
// Copyright (c) 2012 Leanplum, Inc. All rights reserved.
6+
//
7+
// Licensed to the Apache Software Foundation (ASF) under one
8+
// or more contributor license agreements. See the NOTICE file
9+
// distributed with this work for additional information
10+
// regarding copyright ownership. The ASF licenses this file
11+
// to you under the Apache License, Version 2.0 (the "License");
12+
// you may not use this file except in compliance with the License.
13+
// You may obtain a copy of the License at
14+
//
15+
// http://www.apache.org/licenses/LICENSE-2.0
16+
//
17+
// Unless required by applicable law or agreed to in writing,
18+
// software distributed under the License is distributed on an
19+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
// KIND, either express or implied. See the License for the
21+
// specific language governing permissions and limitations
22+
// under the License.
23+
24+
#import "LPMessageArchiveData.h"
25+
26+
@implementation LPMessageArchiveData
27+
28+
-(instancetype)initWithMessageID:(NSString *)messageID
29+
messageBody:(NSString *)messageBody
30+
recipientUserID:(NSString *)recipientUserID
31+
deliveryDateTime:(NSDate *)deliveryDateTime {
32+
self = [super init];
33+
if (self) {
34+
_messageID = messageID;
35+
_messageBody = messageBody;
36+
_recipientUserID = recipientUserID;
37+
_deliveryDateTime = deliveryDateTime;
38+
}
39+
return self;
40+
}
41+
42+
43+
@end

0 commit comments

Comments
 (0)