|
32 | 32 | #import "OneSignal.h" |
33 | 33 |
|
34 | 34 | @implementation OSNotificationPayload |
35 | | -@synthesize actionButtons = _actionButtons, additionalData = _additionalData, badge = _badge, body = _body, contentAvailable = _contentAvailable, notificationID = _notificationID, launchURL = _launchURL, rawPayload = _rawPayload, sound = _sound, subtitle = _subtitle, title = _title, attachments = _attachments; |
36 | 35 |
|
37 | 36 | - (id)initWithRawMessage:(NSDictionary*)message { |
| 37 | + if (!message) |
| 38 | + return nil; |
| 39 | + |
38 | 40 | self = [super init]; |
39 | | - if (self && message) { |
40 | | - _rawPayload = [NSDictionary dictionaryWithDictionary:message]; |
41 | | - |
42 | | - BOOL is2dot4Format = [_rawPayload[@"os_data"][@"buttons"] isKindOfClass:[NSArray class]]; |
43 | | - |
44 | | - if (_rawPayload[@"aps"][@"content-available"]) |
45 | | - _contentAvailable = (BOOL)_rawPayload[@"aps"][@"content-available"]; |
46 | | - else |
47 | | - _contentAvailable = NO; |
48 | | - |
49 | | - if (_rawPayload[@"aps"][@"badge"]) |
50 | | - _badge = [_rawPayload[@"aps"][@"badge"] intValue]; |
51 | | - else |
52 | | - _badge = [_rawPayload[@"badge"] intValue]; |
53 | | - |
54 | | - _actionButtons = _rawPayload[@"o"]; |
55 | | - if (!_actionButtons) { |
56 | | - if (is2dot4Format) |
57 | | - _actionButtons = _rawPayload[@"os_data"][@"buttons"]; |
58 | | - else |
59 | | - _actionButtons = _rawPayload[@"os_data"][@"buttons"][@"o"]; |
60 | | - } |
61 | | - |
62 | | - if(_rawPayload[@"aps"][@"sound"]) |
63 | | - _sound = _rawPayload[@"aps"][@"sound"]; |
64 | | - else if(_rawPayload[@"s"]) |
65 | | - _sound = _rawPayload[@"s"]; |
66 | | - else if (!is2dot4Format) |
67 | | - _sound = _rawPayload[@"os_data"][@"buttons"][@"s"]; |
68 | | - |
69 | | - if(_rawPayload[@"custom"]) { |
70 | | - NSDictionary* custom = _rawPayload[@"custom"]; |
71 | | - if (custom[@"a"]) |
72 | | - _additionalData = [custom[@"a"] copy]; |
73 | | - _notificationID = custom[@"i"]; |
74 | | - _launchURL = custom[@"u"]; |
75 | | - |
76 | | - _attachments = [_rawPayload[@"at"] copy]; |
77 | | - } |
78 | | - else if(_rawPayload[@"os_data"]) { |
79 | | - NSDictionary * os_data = _rawPayload[@"os_data"]; |
80 | | - |
81 | | - NSMutableDictionary *additional = [_rawPayload mutableCopy]; |
82 | | - [additional removeObjectForKey:@"aps"]; |
83 | | - [additional removeObjectForKey:@"os_data"]; |
84 | | - _additionalData = [[NSDictionary alloc] initWithDictionary:additional]; |
85 | | - |
86 | | - _notificationID = os_data[@"i"]; |
87 | | - _launchURL = os_data[@"u"]; |
88 | | - |
89 | | - if (is2dot4Format) { |
90 | | - if (os_data[@"att"]) |
91 | | - _attachments = [os_data[@"att"] copy]; |
92 | | - } |
93 | | - else { |
94 | | - if (os_data[@"buttons"][@"at"]) |
95 | | - _attachments = [os_data[@"buttons"][@"at"] copy]; |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - if(_rawPayload[@"m"]) { |
100 | | - id m = _rawPayload[@"m"]; |
101 | | - if ([m isKindOfClass:[NSDictionary class]]) { |
102 | | - _body = m[@"body"]; |
103 | | - _title = m[@"title"]; |
104 | | - _subtitle = m[@"subtitle"]; |
105 | | - } |
106 | | - else |
107 | | - _body = m; |
108 | | - } |
109 | | - else if(_rawPayload[@"aps"][@"alert"]) { |
110 | | - id a = message[@"aps"][@"alert"]; |
111 | | - if ([a isKindOfClass:[NSDictionary class]]) { |
112 | | - _body = a[@"body"]; |
113 | | - _title = a[@"title"]; |
114 | | - _subtitle = a[@"subtitle"]; |
115 | | - } |
116 | | - else |
117 | | - _body = a; |
118 | | - } |
119 | | - else if(_rawPayload[@"os_data"][@"buttons"][@"m"]) { |
120 | | - id m = _rawPayload[@"os_data"][@"buttons"][@"m"]; |
121 | | - if ([m isKindOfClass:[NSDictionary class]]) { |
122 | | - _body = m[@"body"]; |
123 | | - _title = m[@"title"]; |
124 | | - _subtitle = m[@"subtitle"]; |
125 | | - } |
126 | | - else |
127 | | - _body = m; |
128 | | - } |
129 | | - } |
| 41 | + if (!self) |
| 42 | + return nil; |
| 43 | + |
| 44 | + _rawPayload = [NSDictionary dictionaryWithDictionary:message]; |
| 45 | + |
| 46 | + if ([_rawPayload[@"os_data"] isKindOfClass:[NSDictionary class]]) |
| 47 | + [self parseOSDataPayload]; |
| 48 | + else |
| 49 | + [self parseOriginalPayload]; |
| 50 | + |
| 51 | + [self parseOtherApnsFields]; |
130 | 52 |
|
131 | 53 | return self; |
132 | 54 | } |
| 55 | + |
| 56 | +// Original OneSignal payload format. |
| 57 | +-(void)parseOriginalPayload { |
| 58 | + BOOL remoteSlient = _rawPayload[@"m"] && !_rawPayload[@"aps"][@"alert"]; |
| 59 | + if (remoteSlient) |
| 60 | + [self parseRemoteSlient:_rawPayload]; |
| 61 | + else { |
| 62 | + [self parseApnsFields]; |
| 63 | + _attachments = _rawPayload[@"at"]; |
| 64 | + [self parseActionButtons:_rawPayload[@"o"]]; |
| 65 | + } |
| 66 | + |
| 67 | + [self parseCommonOneSignalFields:_rawPayload[@"custom"]]; |
| 68 | + _additionalData = _rawPayload[@"custom"][@"a"]; |
| 69 | +} |
| 70 | + |
| 71 | +// New OneSignal playload format. |
| 72 | +// OneSignal specific features are under "os_data". |
| 73 | +-(void)parseOSDataPayload { |
| 74 | + NSDictionary *os_data = _rawPayload[@"os_data"]; |
| 75 | + BOOL remoteSlient = os_data[@"buttons"] && !_rawPayload[@"aps"][@"alert"]; |
| 76 | + if (remoteSlient) |
| 77 | + [self parseRemoteSlient:os_data[@"buttons"]]; |
| 78 | + else { |
| 79 | + [self parseApnsFields]; |
| 80 | + _attachments = os_data[@"att"]; |
| 81 | + [self parseActionButtons:os_data[@"buttons"]]; |
| 82 | + } |
| 83 | + |
| 84 | + [self parseCommonOneSignalFields:_rawPayload[@"os_data"]]; |
| 85 | + [self parseOSDataAdditionalData]; |
| 86 | +} |
| 87 | + |
| 88 | +-(void)parseOSDataAdditionalData { |
| 89 | + NSMutableDictionary *additional = [_rawPayload mutableCopy]; |
| 90 | + [additional removeObjectForKey:@"aps"]; |
| 91 | + [additional removeObjectForKey:@"os_data"]; |
| 92 | + _additionalData = [[NSDictionary alloc] initWithDictionary:additional]; |
| 93 | +} |
| 94 | + |
| 95 | +// Fields that share the same format for all OneSignal payload types. |
| 96 | +-(void)parseCommonOneSignalFields:(NSDictionary*)payload { |
| 97 | + _notificationID = payload[@"i"]; |
| 98 | + _launchURL = payload[@"u"]; |
| 99 | + _templateID = payload[@"ti"]; |
| 100 | + _templateName = payload[@"tn"]; |
| 101 | +} |
| 102 | + |
| 103 | +-(void)parseApnsFields { |
| 104 | + [self parseAlertField:_rawPayload[@"aps"][@"alert"]]; |
| 105 | + _badge = [_rawPayload[@"aps"][@"badge"] intValue]; |
| 106 | + _sound = _rawPayload[@"aps"][@"sound"]; |
| 107 | +} |
| 108 | + |
| 109 | +// Pasrse the APNs alert field, can be a NSString or a NSDictionary |
| 110 | +-(void)parseAlertField:(NSObject*)alert { |
| 111 | + if ([alert isKindOfClass:[NSDictionary class]]) { |
| 112 | + NSDictionary *alertDictionary = (NSDictionary*)alert; |
| 113 | + _body = alertDictionary[@"body"]; |
| 114 | + _title = alertDictionary[@"title"]; |
| 115 | + _subtitle = alertDictionary[@"subtitle"]; |
| 116 | + } |
| 117 | + else |
| 118 | + _body = (NSString*)alert; |
| 119 | +} |
| 120 | + |
| 121 | +// Only used on iOS 9 and older. |
| 122 | +// - Or if the OneSignal server hasn't received the iOS version update. |
| 123 | +// May also be used if OneSignal server hasn't received the SDK version 2.4.0+ update event |
| 124 | +-(void)parseRemoteSlient:(NSDictionary*)payload { |
| 125 | + [self parseAlertField:payload[@"m"]]; |
| 126 | + _badge = [payload[@"b"] intValue]; |
| 127 | + _sound = payload[@"s"]; |
| 128 | + _attachments = payload[@"at"]; |
| 129 | + [self parseActionButtons:_rawPayload[@"o"]]; |
| 130 | +} |
| 131 | + |
| 132 | +// Parse and convert minified keys for action buttons |
| 133 | +-(void)parseActionButtons:(NSArray<NSDictionary*>*)buttons { |
| 134 | + NSMutableArray *buttonArray = [NSMutableArray new]; |
| 135 | + for (NSDictionary *button in buttons) { |
| 136 | + [buttonArray addObject: @{@"text" : button[@"n"], |
| 137 | + @"id" : (button[@"i"] ?: button[@"n"]) |
| 138 | + }]; |
| 139 | + } |
| 140 | + |
| 141 | + _actionButtons = buttonArray; |
| 142 | +} |
| 143 | + |
| 144 | +-(void)parseOtherApnsFields { |
| 145 | + NSDictionary *aps = _rawPayload[@"aps"]; |
| 146 | + if (aps[@"content-available"]) |
| 147 | + _contentAvailable = (BOOL)aps[@"content-available"]; |
| 148 | + |
| 149 | + if (aps[@"mutable-content"]) |
| 150 | + _mutableContent = (BOOL)aps[@"mutable-content"]; |
| 151 | + |
| 152 | + _category = aps[@"category"]; |
| 153 | +} |
| 154 | + |
133 | 155 | @end |
0 commit comments