Skip to content

Instantly share code, notes, and snippets.

@pierrevalade
Created July 11, 2010 23:06
Show Gist options
  • Save pierrevalade/471927 to your computer and use it in GitHub Desktop.
Save pierrevalade/471927 to your computer and use it in GitHub Desktop.
//
// UAPushNotification.m
// MyEarth
//
// Created by Pierre Valade on 08/07/10.
// Copyright 2010 Pierre Valade. All rights reserved.
//
#import "UAPushNotification.h"
#define kApplicationKey @""
#define kApplicationSecret @""
@implementation UAPushNotification
// Set default options here
+ (void)initialize {
[self setDelegate:self];
[self setBaseURL:[NSURL URLWithString:@"https://go.urbanairship.com"]];
[self setBasicAuthWithUsername:kApplicationKey password:kApplicationSecret];
NSDictionary *hdrs = [NSDictionary dictionaryWithObject:@"application/json" forKey:@"Content-Type"];
[self setHeaders:hdrs];
}
+ (void)registerWithDeviceToken:(NSData *)theDeviceToken {
// Get a hex string from the device token with no spaces or < >
NSString *deviceToken = [[[[theDeviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"* Device Token: %@", deviceToken);
NSString *registrationUrl = [NSString stringWithFormat:@"/api/device_tokens/%@", deviceToken];
NSDictionary *opts = [NSDictionary dictionaryWithObject:@"{}" forKey:@"body"];
[self putPath:registrationUrl withOptions:opts object:nil];
}
+ (void)restConnection:(NSURLConnection *)connection
didReturnResource:(id)resource object:(id)object {
NSLog(@"* Called when the resource was succeffully fetched and encoded");
}
+ (void)restConnection:(NSURLConnection *)connection
didFailWithError:(NSError *)error object:(id)object {
NSLog(@"* Called when the connection fails in situations where the server is not available, etc.");
}
+ (void)restConnection:(NSURLConnection *)connection
didReceiveResponse:(NSHTTPURLResponse *)response object:(id)object {
// NSLog(@"* Called when the connection receieves any type of response");
if ([response statusCode] == 200) {
NSLog(@"* Device registered at urbanairship");
}
}
- (void)restConnection:(NSURLConnection *)connection
didReceiveError:(NSError *)error response:(NSHTTPURLResponse *)response object:(id)object {
NSLog(@"* Called when the connection receieves a statusCode that isn't a success code.");
}
- (void)restConnection:(NSURLConnection *)connection didReceiveParseError:(NSError *)error responseBody:(NSString *)body object:(id)object {
NSLog(@"* Called when the HRFormatter recieved an error parsing the response data.");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment