-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOneSignalCoreHelper.m
More file actions
337 lines (270 loc) · 11.3 KB
/
Copy pathOneSignalCoreHelper.m
File metadata and controls
337 lines (270 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* Modified MIT License
*
* Copyright 2021 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import <Foundation/Foundation.h>
#import "OneSignalCoreHelper.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef uint32_t CC_LONG; /* 32 bit unsigned integer */
typedef uint64_t CC_LONG64; /* 64 bit unsigned integer */
/*** SHA224 ***/
#define CC_SHA224_DIGEST_LENGTH 28 /* digest length in bytes */
#define CC_SHA224_BLOCK_BYTES 64 /* block size in bytes */
/* same context struct is used for SHA224 and SHA256 */
typedef struct CC_SHA256state_st
{ CC_LONG count[2];
CC_LONG hash[8];
CC_LONG wbuf[16];
} CC_SHA256_CTX;
extern int CC_SHA224_Init(CC_SHA256_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA224_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA224_Final(unsigned char *md, CC_SHA256_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern unsigned char *CC_SHA224(const void *data, CC_LONG len, unsigned char *md)
API_AVAILABLE(macos(10.4), ios(2.0));
/*** SHA256 ***/
#define CC_SHA256_DIGEST_LENGTH 32 /* digest length in bytes */
#define CC_SHA256_BLOCK_BYTES 64 /* block size in bytes */
extern int CC_SHA256_Init(CC_SHA256_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern unsigned char *CC_SHA256(const void *data, CC_LONG len, unsigned char *md)
API_AVAILABLE(macos(10.4), ios(2.0));
/*** SHA384 ***/
#define CC_SHA384_DIGEST_LENGTH 48 /* digest length in bytes */
#define CC_SHA384_BLOCK_BYTES 128 /* block size in bytes */
/* same context struct is used for SHA384 and SHA512 */
typedef struct CC_SHA512state_st
{ CC_LONG64 count[2];
CC_LONG64 hash[8];
CC_LONG64 wbuf[16];
} CC_SHA512_CTX;
extern int CC_SHA384_Init(CC_SHA512_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA384_Update(CC_SHA512_CTX *c, const void *data, CC_LONG len)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA384_Final(unsigned char *md, CC_SHA512_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern unsigned char *CC_SHA384(const void *data, CC_LONG len, unsigned char *md)
API_AVAILABLE(macos(10.4), ios(2.0));
/*** SHA512 ***/
#define CC_SHA512_DIGEST_LENGTH 64 /* digest length in bytes */
#define CC_SHA512_BLOCK_BYTES 128 /* block size in bytes */
extern int CC_SHA512_Init(CC_SHA512_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA512_Update(CC_SHA512_CTX *c, const void *data, CC_LONG len)
API_AVAILABLE(macos(10.4), ios(2.0));
extern int CC_SHA512_Final(unsigned char *md, CC_SHA512_CTX *c)
API_AVAILABLE(macos(10.4), ios(2.0));
extern unsigned char *CC_SHA512(const void *data, CC_LONG len, unsigned char *md)
API_AVAILABLE(macos(10.4), ios(2.0));
@implementation OneSignalCoreHelper
+ (void)runOnMainThread:(void(^)())block {
if ([NSThread isMainThread])
block();
else
dispatch_sync(dispatch_get_main_queue(), block);
}
+ (void)dispatch_async_on_main_queue:(void(^)())block {
dispatch_async(dispatch_get_main_queue(), block);
}
+ (void)performSelector:(SEL)aSelector onMainThreadOnObject:(nullable id)targetObj withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay {
[self dispatch_async_on_main_queue:^{
[targetObj performSelector:aSelector withObject:anArgument afterDelay:delay];
}];
}
+ (NSString*)trimURLSpacing:(NSString*)url {
if (!url || [url isEqual:[NSNull null]]) {
return nil;
}
return [url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
+ (NSString*)parseNSErrorAsJsonString:(NSError*)error {
NSString* jsonResponse;
if (error.userInfo && error.userInfo[@"returned"]) {
@try {
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:error.userInfo[@"returned"] options:0 error:nil];
jsonResponse = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
} @catch(NSException* e) {
jsonResponse = @"{\"error\": \"Unknown error parsing error response.\"}";
}
}
else
jsonResponse = @"{\"error\": \"HTTP no response error\"}";
return jsonResponse;
}
// Prevent the OSNotification blocks from firing if we receive a Non-OneSignal remote push
+ (BOOL)isOneSignalPayload:(NSDictionary *)payload {
if (!payload)
return NO;
return payload[@"custom"][@"i"] || payload[@"os_data"][@"i"];
}
+ (NSMutableDictionary*)formatApsPayloadIntoStandard:(NSDictionary*)remoteUserInfo identifier:(NSString*)identifier {
NSMutableDictionary* userInfo, *customDict, *additionalData, *optionsDict;
BOOL is2dot4Format = false;
if (remoteUserInfo[@"os_data"]) {
userInfo = [remoteUserInfo mutableCopy];
additionalData = [NSMutableDictionary dictionary];
if (remoteUserInfo[@"os_data"][@"buttons"]) {
is2dot4Format = [userInfo[@"os_data"][@"buttons"] isKindOfClass:[NSArray class]];
if (is2dot4Format)
optionsDict = userInfo[@"os_data"][@"buttons"];
else
optionsDict = userInfo[@"os_data"][@"buttons"][@"o"];
}
}
else if (remoteUserInfo[@"custom"]) {
userInfo = [remoteUserInfo mutableCopy];
customDict = [userInfo[@"custom"] mutableCopy];
if (customDict[@"a"])
additionalData = [[NSMutableDictionary alloc] initWithDictionary:customDict[@"a"]];
else
additionalData = [[NSMutableDictionary alloc] init];
optionsDict = userInfo[@"o"];
}
else {
return nil;
}
if (optionsDict) {
NSMutableArray* buttonArray = [[NSMutableArray alloc] init];
for (NSDictionary* button in optionsDict) {
[buttonArray addObject: @{@"text" : button[@"n"],
@"id" : (button[@"i"] ? button[@"i"] : button[@"n"])}];
}
additionalData[@"actionButtons"] = buttonArray;
}
if (![@"com.apple.UNNotificationDefaultActionIdentifier" isEqualToString:identifier])
additionalData[@"actionSelected"] = identifier;
if ([additionalData count] == 0)
additionalData = nil;
else if (remoteUserInfo[@"os_data"]) {
[userInfo addEntriesFromDictionary:additionalData];
if (!is2dot4Format && userInfo[@"os_data"][@"buttons"])
userInfo[@"aps"] = @{@"alert" : userInfo[@"os_data"][@"buttons"][@"m"]};
}
else {
customDict[@"a"] = additionalData;
userInfo[@"custom"] = customDict;
if (userInfo[@"m"])
userInfo[@"aps"] = @{@"alert" : userInfo[@"m"]};
}
return userInfo;
}
+ (BOOL)isDisplayableNotification:(NSDictionary*)msg {
if ([self isRemoteSilentNotification:msg]) {
return false;
}
return msg[@"aps"][@"alert"] != nil;
}
+ (BOOL)isRemoteSilentNotification:(NSDictionary*)msg {
//no alert, sound, or badge payload
if(msg[@"badge"] || msg[@"aps"][@"badge"] || msg[@"m"] || msg[@"o"] || msg[@"s"] || (msg[@"title"] && [msg[@"title"] length] > 0) || msg[@"sound"] || msg[@"aps"][@"sound"] || msg[@"aps"][@"alert"] || msg[@"os_data"][@"buttons"])
return false;
return true;
}
+ (NSString *)randomStringWithLength:(int)length {
NSString * letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [[NSMutableString alloc] initWithCapacity:length];
for(int i = 0; i < length; i++) {
uint32_t ln = (uint32_t)letters.length;
uint32_t rand = arc4random_uniform(ln);
[randomString appendFormat:@"%C", [letters characterAtIndex:rand]];
}
return randomString;
}
+ (BOOL)verifyURL:(NSString *)urlString {
if (urlString) {
NSURL* url = [NSURL URLWithString:urlString];
if (url)
return YES;
}
return NO;
}
+ (BOOL)isWWWScheme:(NSURL*)url {
NSString* urlScheme = [url.scheme lowercaseString];
return [urlScheme isEqualToString:@"http"] || [urlScheme isEqualToString:@"https"];
}
+(void)callSelector:(SEL)selector onObject:(id)object withArg:(BOOL)arg {
NSMethodSignature *methodSignature = [object methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setSelector:selector];
[invocation setTarget:object];
[invocation setArgument:&arg atIndex:2];
[invocation invoke];
}
+(void)callSelector:(SEL)selector
onObject:(id)object
withArgs:(NSArray*)args {
NSMethodSignature *methodSignature = [object methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setSelector:selector];
[invocation setTarget:object];
for(int i = 0; i < methodSignature.numberOfArguments - 2; i++) {
id argv = [args objectAtIndex:i];
[invocation setArgument:&argv atIndex:i + 2];
}
[invocation invoke];
}
/*
Given a UIDeviceOrientation return the custom enum ViewOrientation
*/
+ (ViewOrientation)validateOrientation:(UIDeviceOrientation)orientation {
if (UIDeviceOrientationIsPortrait(orientation))
return OrientationPortrait;
else if (UIDeviceOrientationIsLandscape(orientation))
return OrientationLandscape;
return OrientationInvalid;
}
/*
Given a float convert it to the phone scaled CGFloat
Multiplies float by iPhones scale value
*/
+ (CGFloat)sizeToScale:(float)size {
float scale = [self getScreenScale];
return (CGFloat) (size * scale);
}
/*
Get currentDevice screen bounds
Contains point of origin (x, y) and the size (height, width) of the screen
Changes in regards to the phones current orientation
*/
+ (CGRect)getScreenBounds {
return UIScreen.mainScreen.bounds;
}
/*
Get currentDevice screen scale
Important for specific constraints so that phones of different resolution scale properly
*/
+ (float)getScreenScale {
return UIScreen.mainScreen.scale;
}
@end