2828#import " OneSignalDialogController.h"
2929#import " OneSignalHelper.h"
3030#import " OneSignalDialogRequest.h"
31+ #import " OneSignalAlertViewDelegate.h"
3132
3233/*
3334 This class handles displaying all dialogs (alerts) for the SDK
@@ -41,6 +42,16 @@ @interface OneSignalDialogController ()
4142
4243@end
4344
45+ @interface OneSignal ()
46+
47+ + (void )handleNotificationOpened : (NSDictionary *)messageDict
48+ foreground : (BOOL )foreground
49+ isActive : (BOOL )isActive
50+ actionType : (OSNotificationActionType)actionType
51+ displayType : (OSNotificationDisplayType)displayType ;
52+
53+ @end
54+
4455@implementation OneSignalDialogController
4556
4657+ (instancetype _Nonnull)sharedInstance {
@@ -55,11 +66,75 @@ + (instancetype _Nonnull)sharedInstance {
5566 return sharedInstance;
5667}
5768
58- - (void )presentDialogWithTitle : (NSString * _Nonnull)title withMessage : (NSString * _Nonnull)message withAction : (NSString * _Nullable)actionTitle cancelTitle : (NSString * _Nonnull)cancelTitle withActionCompletion : (OSDialogActionCompletion _Nullable)completion {
69+ - (NSArray <NSString *> *)getActionTitlesFromPayload : (OSNotificationPayload *)payload {
70+ NSMutableArray <NSString *> *actionTitles = [NSMutableArray <NSString *> new];
71+ if (payload.actionButtons ) {
72+ for (id button in payload.actionButtons ) {
73+ [actionTitles addObject: button[@" text" ]];
74+ }
75+ }
76+ return actionTitles;
77+ }
78+
79+ - (void )presentDialogWithMessageDict : (NSDictionary *)messageDict {
80+ if ([OneSignalHelper isIOSVersionLessThan: @" 8.0" ]) {
81+ [OneSignalAlertView showInAppAlert: messageDict];
82+ }
83+ let payload = [OSNotificationPayload parseWithApns: messageDict];
84+ // Add action buttons to payload
85+ NSArray <NSString *> *actionTitles = [self getActionTitlesFromPayload: payload];
86+
87+ [self presentDialogWithTitle: payload.title withMessage: payload.body withActions: actionTitles cancelTitle: @" Close" withActionCompletion: ^(int tappedActionIndex) {
88+ OSNotificationActionType actionType = OSNotificationActionTypeOpened;
89+
90+ NSDictionary *finalDict = messageDict;
91+
92+ if (tappedActionIndex > -1 ) {
93+
94+ actionType = OSNotificationActionTypeActionTaken;
95+
96+ NSMutableDictionary * userInfo = [messageDict mutableCopy ];
97+
98+ // Fixed for iOS 7, which has 'actionbuttons' as a root property of the dict, not in 'os_data'
99+ if (messageDict[@" os_data" ] && !messageDict[@" actionbuttons" ]) {
100+ if ([messageDict[@" os_data" ][@" buttons" ] isKindOfClass: [NSDictionary class ]])
101+ userInfo[@" actionSelected" ] = messageDict[@" os_data" ][@" buttons" ][@" o" ][tappedActionIndex - 1 ][@" i" ];
102+ else
103+ userInfo[@" actionSelected" ] = messageDict[@" os_data" ][@" buttons" ][tappedActionIndex][@" i" ];
104+ } else if (messageDict[@" buttons" ]) {
105+ userInfo[@" actionSelected" ] = messageDict[@" buttons" ][tappedActionIndex][@" i" ];
106+ } else {
107+ NSMutableDictionary * customDict = userInfo[@" custom" ] ? [userInfo[@" custom" ] mutableCopy ] : [NSMutableDictionary new ];
108+ NSMutableDictionary * additionalData = customDict[@" a" ] ? [[NSMutableDictionary alloc ] initWithDictionary: customDict[@" a" ]] : [NSMutableDictionary new ];
109+
110+ if ([additionalData[@" actionButtons" ] isKindOfClass: [NSArray class ]]) {
111+ additionalData[@" actionSelected" ] = additionalData[@" actionButtons" ][tappedActionIndex - 1 ][@" id" ];
112+ } else if ([messageDict[@" o" ] isKindOfClass: [NSArray class ]]) {
113+ additionalData[@" actionSelected" ] = messageDict[@" o" ][tappedActionIndex][@" i" ];
114+ } else if ([messageDict[@" actionbuttons" ] isKindOfClass: [NSArray class ]]) {
115+ additionalData[@" actionSelected" ] = messageDict[@" actionbuttons" ][tappedActionIndex][@" i" ];
116+ }
117+
118+ customDict[@" a" ] = additionalData;
119+ userInfo[@" custom" ] = customDict;
120+ }
121+
122+ finalDict = userInfo;
123+ }
124+
125+ [OneSignal handleNotificationOpened: finalDict foreground: YES isActive: YES actionType: actionType displayType: OSNotificationDisplayTypeInAppAlert];
126+ }];
127+
128+ // Message received that was displayed (Foreground + InAppAlert is true)
129+ // Call received callback
130+ [OneSignalHelper handleNotificationReceived: OSNotificationDisplayTypeInAppAlert fromBackground: NO ];
131+ }
132+
133+ - (void )presentDialogWithTitle : (NSString * _Nonnull)title withMessage : (NSString * _Nonnull)message withActions : (NSArray <NSString *> * _Nullable)actionTitles cancelTitle : (NSString * _Nonnull)cancelTitle withActionCompletion : (OSDialogActionCompletion _Nullable)completion {
59134
60135 // ensure this UI code executes on the main thread
61136 dispatch_async (dispatch_get_main_queue (), ^{
62- let request = [[OSDialogRequest alloc ] initWithTitle: title withMessage: message withActionTitle: actionTitle withCancelTitle: cancelTitle withCompletion: completion];
137+ let request = [[OSDialogRequest alloc ] initWithTitle: title withMessage: message withActionTitles: actionTitles withCancelTitle: cancelTitle withCompletion: completion];
63138
64139 [self .queue addObject: request];
65140
@@ -75,8 +150,12 @@ - (void)presentDialogWithTitle:(NSString * _Nonnull)title withMessage:(NSString
75150- (void )displayDialog : (OSDialogRequest * _Nonnull)request {
76151 // iOS 7
77152 if ([OneSignalHelper isIOSVersionLessThan: @" 8.0" ]) {
78-
79- let alertView = [[UIAlertView alloc ] initWithTitle: request.title message: request.message delegate: self cancelButtonTitle: request.cancelTitle otherButtonTitles: request.actionTitle, nil ];
153+ let alertView = [[UIAlertView alloc ] initWithTitle: request.title message: request.message delegate: self cancelButtonTitle: request.cancelTitle otherButtonTitles: nil , nil ];
154+ if (request.actionTitles != nil ) {
155+ for (NSString *actionTitle in request.actionTitles ) {
156+ [alertView addButtonWithTitle: actionTitle];
157+ }
158+ }
80159
81160 [alertView show ];
82161
@@ -89,19 +168,26 @@ - (void)displayDialog:(OSDialogRequest * _Nonnull)request {
89168 let controller = [UIAlertController alertControllerWithTitle: request.title message: request.message preferredStyle: UIAlertControllerStyleAlert];
90169
91170 [controller addAction: [UIAlertAction actionWithTitle: request.cancelTitle style: UIAlertActionStyleCancel handler: ^(UIAlertAction * _Nonnull action) {
92- [self delayResult: false ];
171+ [self delayResult: - 1 ];
93172 }]];
94173
95- if (request.actionTitle != nil ) {
96- [controller addAction: [UIAlertAction actionWithTitle: request.actionTitle style: UIAlertActionStyleDefault handler: ^(UIAlertAction * _Nonnull action) {
97- [self delayResult: true ];
98- }]];
174+ if (request.actionTitles != nil ) {
175+ for (int i = 0 ; i < request.actionTitles .count ; i++) {
176+ NSString *actionTitle = request.actionTitles [i];
177+ [controller addAction: [UIAlertAction actionWithTitle: actionTitle style: UIAlertActionStyleDefault handler: ^(UIAlertAction * _Nonnull action) {
178+ [self delayResult: i];
179+ }]];
180+ }
99181 }
100182
101183 [rootViewController presentViewController: controller animated: true completion: nil ];
102184}
103185
104- - (void )delayResult : (BOOL )result {
186+ - (void )alertView : (UIAlertView *)alertView clickedButtonAtIndex : (NSInteger )buttonIndex {
187+ [self delayResult: (int )buttonIndex-1 ];
188+ }
189+
190+ - (void )delayResult : (int )result {
105191 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
106192 let currentDialog = self.queue .firstObject ;
107193
@@ -120,8 +206,8 @@ - (void)delayResult:(BOOL)result {
120206 });
121207}
122208
123- - (void )alertView : (UIAlertView *) alertView didDismissWithButtonIndex : ( NSInteger ) buttonIndex {
124- [ self delayResult: buttonIndex > 0 ];
209+ - (void )clearQueue {
210+ self. queue = [ NSMutableArray new ];
125211}
126212
127213@end
0 commit comments