Skip to content

Commit

Permalink
Added jsonRepresentation method to OSInAppMessageAction class (#704)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mikechoch committed Jun 30, 2020
1 parent a0aa0e6 commit efb2e2b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 25 deletions.
6 changes: 1 addition & 5 deletions iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

// Example block for IAM action click handler
id inAppMessagingActionClickBlock = ^(OSInAppMessageAction *action) {
NSString *message = [NSString stringWithFormat:@"Click Action Occurred: clickName:%@ clickUrl:%@ firstClick:%i closesMessage:%i",
action.clickName,
action.clickUrl,
action.firstClick,
action.closesMessage];
NSString *message = [NSString stringWithFormat:@"Click Action Occurred: %@", [action jsonRepresentation]];
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:message];
};

Expand Down
6 changes: 0 additions & 6 deletions iOS_SDK/OneSignalSDK/Source/OSInAppMessageAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ typedef NS_ENUM(NSUInteger, OSInAppMessageActionUrlType) {
// The unique identifier for this click
@property (strong, nonatomic, nonnull) NSString *clickId;

// The outcome to send for this action
@property (strong, nonatomic, nullable) NSArray<OSInAppMessageOutcome *> *outcomes;

// The tags to send for this action
@property (strong, nonatomic, nullable) OSInAppMessageTag *tags;

// The prompt action available
@property (nonatomic, nullable) NSArray<NSObject<OSInAppMessagePrompt>*> *promptActions;

Expand Down
26 changes: 26 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/OSInAppMessageAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* THE SOFTWARE.
*/

#import "OneSignalHelper.h"
#import "OSInAppMessageAction.h"
#import "OSInAppMessagePushPrompt.h"
#import "OSInAppMessageLocationPrompt.h"
Expand Down Expand Up @@ -109,6 +110,31 @@ + (instancetype)instanceWithJson:(NSDictionary *)json {
return action;
}

- (NSDictionary *)jsonRepresentation {
let json = [NSMutableDictionary new];

json[@"click_name"] = self.clickName;
json[@"first_click"] = @(self.firstClick);
json[@"closes_message"] = @(self.closesMessage);

if (self.clickUrl)
json[@"click_url"] = self.clickUrl.absoluteString;

if (self.outcomes && self.outcomes.count > 0) {
let *jsonOutcomes = [NSMutableArray new];
for (OSInAppMessageOutcome *outcome in self.outcomes) {
[jsonOutcomes addObject:[outcome jsonRepresentation]];
}

json[@"outcomes"] = jsonOutcomes;
}

if (self.tags)
json[@"tags"] = [self.tags jsonRepresentation];

return json;
}

- (NSString *)description {
return [NSString stringWithFormat:@"OSInAppMessageAction outcome: %@ \ntag: %@ promptAction: %@", _outcomes, _tags, [_promptActions description]];
}
Expand Down
6 changes: 1 addition & 5 deletions iOS_SDK/OneSignalSDK/Source/OSInAppMessageOutcome.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
#import "OSJSONHandling.h"
#import "OneSignal.h"

@interface OSInAppMessageOutcome : NSObject <OSJSONDecodable>

@property (strong, nonatomic, nonnull) NSString *name;
@property (strong, nonatomic, nonnull) NSNumber *weight;
@property (nonatomic) BOOL unique;
@interface OSInAppMessageOutcome () <OSJSONDecodable>

@end

Expand Down
11 changes: 11 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/OSInAppMessageOutcome.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* THE SOFTWARE.
*/

#import "OneSignalHelper.h"
#import "OSInAppMessageOutcome.h"

@implementation OSInAppMessageOutcome
Expand Down Expand Up @@ -65,6 +66,16 @@ + (instancetype _Nullable)instancePreviewFromPayload:(OSNotificationPayload * _N
return nil;
}

- (NSDictionary *)jsonRepresentation {
let json = [NSMutableDictionary new];

json[@"name"] = self.name;
json[@"weight"] = self.weight;
json[@"unique"] = @(self.unique);

return json;
}

- (NSString *)description {
return [NSString stringWithFormat:@"OSInAppMessageOutcome name: %@ weight: %@ unique: %s\n", _name, _weight, _unique ? "YES" : "NO"];
}
Expand Down
5 changes: 1 addition & 4 deletions iOS_SDK/OneSignalSDK/Source/OSInAppMessageTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
#import "OSJSONHandling.h"
#import "OneSignal.h"

@interface OSInAppMessageTag : NSObject <OSJSONEncodable, OSJSONDecodable>

@property (strong, nonatomic, nullable) NSDictionary *tagsToAdd;
@property (strong, nonatomic, nullable) NSArray *tagsToRemove;
@interface OSInAppMessageTag () <OSJSONEncodable, OSJSONDecodable>

@end

Expand Down
38 changes: 34 additions & 4 deletions iOS_SDK/OneSignalSDK/Source/OneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,50 @@ typedef NS_ENUM(NSUInteger, OSNotificationDisplayType) {

@end;

@interface OSInAppMessageOutcome : NSObject

@property (strong, nonatomic, nonnull) NSString *name;
@property (strong, nonatomic, nonnull) NSNumber *weight;
@property (nonatomic) BOOL unique;

// Convert the class into a NSDictionary
- (NSDictionary *_Nonnull)jsonRepresentation;

@end

@interface OSInAppMessageTag : NSObject

@property (strong, nonatomic, nullable) NSDictionary *tagsToAdd;
@property (strong, nonatomic, nullable) NSArray *tagsToRemove;

// Convert the class into a NSDictionary
- (NSDictionary *_Nonnull)jsonRepresentation;

@end

@interface OSInAppMessageAction : NSObject

/* The action name attached to the IAM action */
// The action name attached to the IAM action
@property (strong, nonatomic, nullable) NSString *clickName;

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

/* Whether or not the click action is first click on the IAM */
// Whether or not the click action is first click on the IAM
@property (nonatomic) BOOL firstClick;

/* Whether or not the click action dismisses the message */
// Whether or not the click action dismisses the message
@property (nonatomic) BOOL closesMessage;

// The outcome to send for this action
@property (strong, nonatomic, nullable) NSArray<OSInAppMessageOutcome *> *outcomes;

// The tags to send for this action
@property (strong, nonatomic, nullable) OSInAppMessageTag *tags;

// Convert the class into a NSDictionary
- (NSDictionary *_Nonnull)jsonRepresentation;

@end

@protocol OSInAppMessageDelegate <NSObject>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ - (void)testIAMClickedLaunchesUniqueOutcomeAPIV2Request {
XCTAssertFalse(OneSignalClientOverrider.lastHTTPRequestType);
}

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

Expand Down

0 comments on commit efb2e2b

Please sign in to comment.