|
| 1 | +/** |
| 2 | + * Modified MIT License |
| 3 | + * |
| 4 | + * Copyright 2017 OneSignal |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * 1. The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * 2. All copies of substantial portions of the Software may only be used in |
| 17 | + * connection with services provided by OneSignal. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#import "./include/onesignal_flutter/OSFlutterCategories.h" |
| 29 | + |
| 30 | +/* |
| 31 | + The OneSignal iOS SDK implements similar methods (`toDictionary`) |
| 32 | + However we decided to implement custom `toJson` methods for several |
| 33 | + of these objects to add more properties. |
| 34 | +
|
| 35 | + TODO: Update the native iOS SDK to add these details |
| 36 | + (ie. `templateId` is missing from OSNotificationPayload's `toDictionary` |
| 37 | + method in the native SDK) and remove them from here. |
| 38 | +*/ |
| 39 | + |
| 40 | +@implementation OSNotification (Flutter) |
| 41 | +- (NSDictionary *)toJson { |
| 42 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 43 | + |
| 44 | + json[@"contentAvailable"] = @(self.contentAvailable); |
| 45 | + json[@"mutableContent"] = @(self.mutableContent); |
| 46 | + |
| 47 | + if (self.rawPayload) { |
| 48 | + NSError *jsonError; |
| 49 | + NSData *data = |
| 50 | + [NSJSONSerialization dataWithJSONObject:self.rawPayload |
| 51 | + options:NSJSONWritingPrettyPrinted |
| 52 | + error:&jsonError]; |
| 53 | + |
| 54 | + if (!jsonError) { |
| 55 | + NSString *rawPayloadString = |
| 56 | + [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
| 57 | + json[@"rawPayload"] = rawPayloadString; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (self.notificationId) |
| 62 | + json[@"notificationId"] = self.notificationId; |
| 63 | + if (self.templateName) |
| 64 | + json[@"templateName"] = self.templateName; |
| 65 | + if (self.templateId) |
| 66 | + json[@"templateId"] = self.templateId; |
| 67 | + if (self.badge) |
| 68 | + json[@"badge"] = @(self.badge); |
| 69 | + if (self.badgeIncrement) |
| 70 | + json[@"badgeIncrement"] = @(self.badgeIncrement); |
| 71 | + if (self.sound) |
| 72 | + json[@"sound"] = self.sound; |
| 73 | + if (self.title) |
| 74 | + json[@"title"] = self.title; |
| 75 | + if (self.subtitle) |
| 76 | + json[@"subtitle"] = self.subtitle; |
| 77 | + if (self.body) |
| 78 | + json[@"body"] = self.body; |
| 79 | + if (self.launchURL) |
| 80 | + json[@"launchUrl"] = self.launchURL; |
| 81 | + if (self.additionalData) |
| 82 | + json[@"additionalData"] = self.additionalData; |
| 83 | + if (self.attachments) |
| 84 | + json[@"attachments"] = self.attachments; |
| 85 | + if (self.actionButtons) |
| 86 | + json[@"buttons"] = self.actionButtons; |
| 87 | + if (self.category) |
| 88 | + json[@"category"] = self.category; |
| 89 | + |
| 90 | + return json; |
| 91 | +} |
| 92 | +@end |
| 93 | + |
| 94 | +@implementation OSNotificationClickEvent (Flutter) |
| 95 | +- (NSDictionary *)toJson { |
| 96 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 97 | + |
| 98 | + json[@"notification"] = self.notification.toJson; |
| 99 | + json[@"result"] = self.result.toJson; |
| 100 | + |
| 101 | + return json; |
| 102 | +} |
| 103 | +@end |
| 104 | + |
| 105 | +@implementation OSNotificationClickResult (Flutter) |
| 106 | +- (NSDictionary *)toJson { |
| 107 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 108 | + |
| 109 | + json[@"action_id"] = self.actionId; |
| 110 | + json[@"url"] = self.url; |
| 111 | + |
| 112 | + return json; |
| 113 | +} |
| 114 | +@end |
| 115 | + |
| 116 | +@implementation OSNotificationWillDisplayEvent (Flutter) |
| 117 | +- (NSDictionary *)toJson { |
| 118 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 119 | + |
| 120 | + json[@"notification"] = self.notification.toJson; |
| 121 | + |
| 122 | + return json; |
| 123 | +} |
| 124 | +@end |
| 125 | + |
| 126 | +@implementation OSInAppMessageWillDisplayEvent (Flutter) |
| 127 | +- (NSDictionary *)toJson { |
| 128 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 129 | + |
| 130 | + json[@"message"] = self.message.toJson; |
| 131 | + |
| 132 | + return json; |
| 133 | +} |
| 134 | +@end |
| 135 | + |
| 136 | +@implementation OSInAppMessageDidDisplayEvent (Flutter) |
| 137 | +- (NSDictionary *)toJson { |
| 138 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 139 | + |
| 140 | + json[@"message"] = self.message.toJson; |
| 141 | + |
| 142 | + return json; |
| 143 | +} |
| 144 | +@end |
| 145 | + |
| 146 | +@implementation OSInAppMessageWillDismissEvent (Flutter) |
| 147 | +- (NSDictionary *)toJson { |
| 148 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 149 | + |
| 150 | + json[@"message"] = self.message.toJson; |
| 151 | + |
| 152 | + return json; |
| 153 | +} |
| 154 | +@end |
| 155 | + |
| 156 | +@implementation OSInAppMessageDidDismissEvent (Flutter) |
| 157 | +- (NSDictionary *)toJson { |
| 158 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 159 | + |
| 160 | + json[@"message"] = self.message.toJson; |
| 161 | + |
| 162 | + return json; |
| 163 | +} |
| 164 | +@end |
| 165 | + |
| 166 | +@implementation OSInAppMessageClickEvent (Flutter) |
| 167 | +- (NSDictionary *)toJson { |
| 168 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 169 | + |
| 170 | + json[@"message"] = self.message.toJson; |
| 171 | + json[@"result"] = self.result.toJson; |
| 172 | + |
| 173 | + return json; |
| 174 | +} |
| 175 | +@end |
| 176 | + |
| 177 | +@implementation OSInAppMessageClickResult (Flutter) |
| 178 | +- (NSDictionary *)toJson { |
| 179 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 180 | + |
| 181 | + json[@"action_id"] = self.actionId; |
| 182 | + json[@"url"] = self.url; |
| 183 | + json[@"closing_message"] = @(self.closingMessage); |
| 184 | + |
| 185 | + return json; |
| 186 | +} |
| 187 | +@end |
| 188 | + |
| 189 | +@implementation OSInAppMessage (Flutter) |
| 190 | +- (NSDictionary *)toJson { |
| 191 | + NSMutableDictionary *json = [NSMutableDictionary new]; |
| 192 | + |
| 193 | + json[@"message_id"] = self.messageId; |
| 194 | + |
| 195 | + return json; |
| 196 | +} |
| 197 | +@end |
| 198 | + |
| 199 | +@implementation NSError (Flutter) |
| 200 | +- (FlutterError *)flutterError { |
| 201 | + return [FlutterError |
| 202 | + errorWithCode:[NSString stringWithFormat:@"%i", (int)self.code] |
| 203 | + message:self.localizedDescription |
| 204 | + details:nil]; |
| 205 | +} |
| 206 | +@end |
0 commit comments