Skip to content

Commit efb2e2b

Browse files
authored
Added jsonRepresentation method to OSInAppMessageAction class (#704)
* Added `jsonRepresentation` method to `OSInAppMessageAction` class * `OSJSONEncodable` now opens up possibility to have the `jsonRepresentation` method * Modified how we construct the json for the `OSInAppMessageAction` class * Fixed demo app example IAM click block and now print out the constructed json rep * Did not need `OSJSONEncodable` * Fixed comment format to `//` instead of `/* */` * Changed how jsonRepresentation method internals work * Added `null` check for `clickUrl` because we have to call `absoluteString` method `NSURL` class * Added `jsonRepresentation` method to `tag` and `outcome` class * `OSInAppMessageTag` now has `jsonRepresentation` * Made `tagsToAdd` and `tagsToRemove` public access * `OSInAppMessageOutcome` now has `jsonRepresentation` * Made `name`, `weight`, and `unique` public access * Wihtin `OSInAppMessageAction` now, the `tags` and `outcomes` are also public access * This is now consistently with Android IAM click action class and what is given to the developers from the IAM click handler
1 parent a0aa0e6 commit efb2e2b

File tree

8 files changed

+75
-25
lines changed

8 files changed

+75
-25
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5656

5757
// Example block for IAM action click handler
5858
id inAppMessagingActionClickBlock = ^(OSInAppMessageAction *action) {
59-
NSString *message = [NSString stringWithFormat:@"Click Action Occurred: clickName:%@ clickUrl:%@ firstClick:%i closesMessage:%i",
60-
action.clickName,
61-
action.clickUrl,
62-
action.firstClick,
63-
action.closesMessage];
59+
NSString *message = [NSString stringWithFormat:@"Click Action Occurred: %@", [action jsonRepresentation]];
6460
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:message];
6561
};
6662

iOS_SDK/OneSignalSDK/Source/OSInAppMessageAction.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ typedef NS_ENUM(NSUInteger, OSInAppMessageActionUrlType) {
5050
// The unique identifier for this click
5151
@property (strong, nonatomic, nonnull) NSString *clickId;
5252

53-
// The outcome to send for this action
54-
@property (strong, nonatomic, nullable) NSArray<OSInAppMessageOutcome *> *outcomes;
55-
56-
// The tags to send for this action
57-
@property (strong, nonatomic, nullable) OSInAppMessageTag *tags;
58-
5953
// The prompt action available
6054
@property (nonatomic, nullable) NSArray<NSObject<OSInAppMessagePrompt>*> *promptActions;
6155

iOS_SDK/OneSignalSDK/Source/OSInAppMessageAction.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#import "OneSignalHelper.h"
2829
#import "OSInAppMessageAction.h"
2930
#import "OSInAppMessagePushPrompt.h"
3031
#import "OSInAppMessageLocationPrompt.h"
@@ -109,6 +110,31 @@ + (instancetype)instanceWithJson:(NSDictionary *)json {
109110
return action;
110111
}
111112

113+
- (NSDictionary *)jsonRepresentation {
114+
let json = [NSMutableDictionary new];
115+
116+
json[@"click_name"] = self.clickName;
117+
json[@"first_click"] = @(self.firstClick);
118+
json[@"closes_message"] = @(self.closesMessage);
119+
120+
if (self.clickUrl)
121+
json[@"click_url"] = self.clickUrl.absoluteString;
122+
123+
if (self.outcomes && self.outcomes.count > 0) {
124+
let *jsonOutcomes = [NSMutableArray new];
125+
for (OSInAppMessageOutcome *outcome in self.outcomes) {
126+
[jsonOutcomes addObject:[outcome jsonRepresentation]];
127+
}
128+
129+
json[@"outcomes"] = jsonOutcomes;
130+
}
131+
132+
if (self.tags)
133+
json[@"tags"] = [self.tags jsonRepresentation];
134+
135+
return json;
136+
}
137+
112138
- (NSString *)description {
113139
return [NSString stringWithFormat:@"OSInAppMessageAction outcome: %@ \ntag: %@ promptAction: %@", _outcomes, _tags, [_promptActions description]];
114140
}

iOS_SDK/OneSignalSDK/Source/OSInAppMessageOutcome.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@
3232
#import "OSJSONHandling.h"
3333
#import "OneSignal.h"
3434

35-
@interface OSInAppMessageOutcome : NSObject <OSJSONDecodable>
36-
37-
@property (strong, nonatomic, nonnull) NSString *name;
38-
@property (strong, nonatomic, nonnull) NSNumber *weight;
39-
@property (nonatomic) BOOL unique;
35+
@interface OSInAppMessageOutcome () <OSJSONDecodable>
4036

4137
@end
4238

iOS_SDK/OneSignalSDK/Source/OSInAppMessageOutcome.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#import "OneSignalHelper.h"
2829
#import "OSInAppMessageOutcome.h"
2930

3031
@implementation OSInAppMessageOutcome
@@ -65,6 +66,16 @@ + (instancetype _Nullable)instancePreviewFromPayload:(OSNotificationPayload * _N
6566
return nil;
6667
}
6768

69+
- (NSDictionary *)jsonRepresentation {
70+
let json = [NSMutableDictionary new];
71+
72+
json[@"name"] = self.name;
73+
json[@"weight"] = self.weight;
74+
json[@"unique"] = @(self.unique);
75+
76+
return json;
77+
}
78+
6879
- (NSString *)description {
6980
return [NSString stringWithFormat:@"OSInAppMessageOutcome name: %@ weight: %@ unique: %s\n", _name, _weight, _unique ? "YES" : "NO"];
7081
}

iOS_SDK/OneSignalSDK/Source/OSInAppMessageTag.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232
#import "OSJSONHandling.h"
3333
#import "OneSignal.h"
3434

35-
@interface OSInAppMessageTag : NSObject <OSJSONEncodable, OSJSONDecodable>
36-
37-
@property (strong, nonatomic, nullable) NSDictionary *tagsToAdd;
38-
@property (strong, nonatomic, nullable) NSArray *tagsToRemove;
35+
@interface OSInAppMessageTag () <OSJSONEncodable, OSJSONDecodable>
3936

4037
@end
4138

iOS_SDK/OneSignalSDK/Source/OneSignal.h

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,50 @@ typedef NS_ENUM(NSUInteger, OSNotificationDisplayType) {
189189

190190
@end;
191191

192+
@interface OSInAppMessageOutcome : NSObject
193+
194+
@property (strong, nonatomic, nonnull) NSString *name;
195+
@property (strong, nonatomic, nonnull) NSNumber *weight;
196+
@property (nonatomic) BOOL unique;
197+
198+
// Convert the class into a NSDictionary
199+
- (NSDictionary *_Nonnull)jsonRepresentation;
200+
201+
@end
202+
203+
@interface OSInAppMessageTag : NSObject
204+
205+
@property (strong, nonatomic, nullable) NSDictionary *tagsToAdd;
206+
@property (strong, nonatomic, nullable) NSArray *tagsToRemove;
207+
208+
// Convert the class into a NSDictionary
209+
- (NSDictionary *_Nonnull)jsonRepresentation;
210+
211+
@end
212+
192213
@interface OSInAppMessageAction : NSObject
193214

194-
/* The action name attached to the IAM action */
215+
// The action name attached to the IAM action
195216
@property (strong, nonatomic, nullable) NSString *clickName;
196217

197-
/* The URL (if any) that should be opened when the action occurs */
218+
// The URL (if any) that should be opened when the action occurs
198219
@property (strong, nonatomic, nullable) NSURL *clickUrl;
199220

200-
/* Whether or not the click action is first click on the IAM */
221+
// Whether or not the click action is first click on the IAM
201222
@property (nonatomic) BOOL firstClick;
202223

203-
/* Whether or not the click action dismisses the message */
224+
// Whether or not the click action dismisses the message
204225
@property (nonatomic) BOOL closesMessage;
205226

227+
// The outcome to send for this action
228+
@property (strong, nonatomic, nullable) NSArray<OSInAppMessageOutcome *> *outcomes;
229+
230+
// The tags to send for this action
231+
@property (strong, nonatomic, nullable) OSInAppMessageTag *tags;
232+
233+
// Convert the class into a NSDictionary
234+
- (NSDictionary *_Nonnull)jsonRepresentation;
235+
206236
@end
207237

208238
@protocol OSInAppMessageDelegate <NSObject>

iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ - (void)testIAMClickedLaunchesUniqueOutcomeAPIV2Request {
11231123
XCTAssertFalse(OneSignalClientOverrider.lastHTTPRequestType);
11241124
}
11251125

1126-
- (void)testIAMClickedLaunchesTagSendPIRequest {
1126+
- (void)testIAMClickedLaunchesTagSendAPIRequest {
11271127
let message = [OSInAppMessageTestHelper testMessageJsonWithTriggerPropertyName:OS_DYNAMIC_TRIGGER_KIND_SESSION_TIME withId:@"test_id1" withOperator:OSTriggerOperatorTypeLessThan withValue:@10.0];
11281128
let registrationResponse = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message]];
11291129

0 commit comments

Comments
 (0)