Skip to content

Commit 8becfce

Browse files
committed
Added new public parseNSErrorAsJsonString method
* Designed to be used by other SDKs as a standard way to parse errors from postNotification.
1 parent cc7ae43 commit 8becfce

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

iOS_SDK/OneSignal/OneSignal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
265265
+ (void)postNotification:(NSDictionary*)jsonData;
266266
+ (void)postNotification:(NSDictionary*)jsonData onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock;
267267
+ (void)postNotificationWithJsonString:(NSString*)jsonData onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock;
268+
+ (NSString*)parseNSErrorAsJsonString:(NSError*)error;
268269

269270
// - Request and track user's location
270271
+ (void)promptLocation;

iOS_SDK/OneSignal/OneSignal.m

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ + (void)postNotification:(NSDictionary*)jsonData {
473473
}
474474

475475
+ (void)postNotification:(NSDictionary*)jsonData onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {
476-
NSMutableURLRequest* request = [httpClient requestWithMethod:@"POST" path:@"notifications"];
476+
NSMutableURLRequest* request = [httpClient requestWithMethod:@"GET" path:@"notifications"];
477477

478478
NSMutableDictionary* dataDic = [[NSMutableDictionary alloc] initWithDictionary:jsonData];
479479
dataDic[@"app_id"] = app_id;
@@ -506,11 +506,30 @@ + (void)postNotificationWithJsonString:(NSString*)jsonString onSuccess:(OSResult
506506
if (jsonError == nil && jsonData != nil)
507507
[self postNotification:jsonData onSuccess:successBlock onFailure:failureBlock];
508508
else {
509-
onesignal_Log(ONE_S_LL_WARN,[NSString stringWithFormat: @"postNotification JSON Parse Error: %@", jsonError]);
510-
onesignal_Log(ONE_S_LL_WARN,[NSString stringWithFormat: @"postNotification JSON Parse Error, JSON: %@", jsonString]);
509+
onesignal_Log(ONE_S_LL_WARN, [NSString stringWithFormat: @"postNotification JSON Parse Error: %@", jsonError]);
510+
onesignal_Log(ONE_S_LL_WARN, [NSString stringWithFormat: @"postNotification JSON Parse Error, JSON: %@", jsonString]);
511511
}
512512
}
513513

514+
+ (NSString*)parseNSErrorAsJsonString:(NSError*)error {
515+
NSString* jsonResponse;
516+
517+
if (error.userInfo && error.userInfo[@"returned"]) {
518+
@try {
519+
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:error.userInfo[@"returned"] options:0 error:nil];
520+
jsonResponse = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
521+
} @catch(NSException* e) {
522+
onesignal_Log(ONE_S_LL_ERROR, [NSString stringWithFormat:@"%@", e]);
523+
onesignal_Log(ONE_S_LL_ERROR, [NSString stringWithFormat:@"%@", [NSThread callStackSymbols]]);
524+
jsonResponse = @"{\"error\": \"Unkown error parsing error response.\"}";
525+
}
526+
}
527+
else
528+
jsonResponse = @"{\"error\": \"HTTP no response error\"}";
529+
530+
return jsonResponse;
531+
}
532+
514533
/* Option:0, 1 or 2 */
515534
+ (void)setNotificationDisplayOptions:(NSNumber*)option {
516535

0 commit comments

Comments
 (0)