Urban Airship - Send Push with NSURLConnection

后端 未结 2 1349
無奈伤痛
無奈伤痛 2021-01-06 11:32

I\'m working on a simple prototype and need to test sending push notifications from one device to another.

I\'ve emailed Urban Airship to turn on the \"Allow Push Fr

相关标签:
2条回答
  • 2021-01-06 12:00

    Taking a look at Urban Airship's guide to troubleshooting HTTP status codes, and the documentation for the push API, my guess would be that you need to add a trailing slash to the URL:

    [NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]
    
    0 讨论(0)
  • 2021-01-06 12:02

    Example Using the V3 API...

    -(void)richPushNotification{
    
    NSDictionary *push = @{
    
                           @"audience" : @{
                                   @"device_token" : deviceToken
                                   },
                           @"device_types" : @[ @"ios" ],
                           @"notification" : @{
                                   @"ios" : @{
                                           @"alert":Message,
                                           @"sound":@"default",
                                           @"badge":@"auto",
                                           }
                                   },
                           @"message": @{
                                   @"title": Message,
                                   @"body": @"<html><body><h1>blah blah</h1> etc...</html>",
                                   @"content_type": @"text/html",
                                   @"extra": @{
                                           @"offer_id" : @"608f1f6c-8860-c617-a803-b187b491568e"
                                           }
                                   }
                           };
    
    
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/vnd.urbanairship+json; version=3;" forHTTPHeaderField:@"Accept"];
    
    NSString *authStr = [NSString stringWithFormat:@"%@:%@", appKey, appMasterSecret];
    NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];
    
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:push
                                                       options:0 // Pass 0 if you don't care about the readability of the generated string
                                                         error:NULL];
    
    request.HTTPBody = jsonData;
    
    [NSURLConnection connectionWithRequest:request delegate:self];
    

    }

    And The Response:

    - (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
    NSHTTPURLResponse * res = (NSHTTPURLResponse *) response;
    NSLog(@"response: %@",res);
    NSLog(@"res %li\n",(long)res.statusCode);
    
    if (res.statusCode == 202) {
    
        //Show Alert Message Sent
    
    }else{
    
        //Handle Error
    
    }
    

    }

    0 讨论(0)
提交回复
热议问题