Thursday 10 May 2012

Push Notification


extern NSString            *deviceToken;

#define  kApplicationKey @"d702hrNiSvWSG7fGjWNMyw"
#define  kApplicationSecret @"bo8vP8vOSdO2akt_ySXolw"

 [[UIApplication sharedApplication]
     registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                         UIRemoteNotificationTypeSound |
                                         UIRemoteNotificationTypeAlert)];

#pragma mark -

//*******************************for push notifications**************************//

#pragma mark -
#pragma mark Push Notifications

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken {
    // Get a hex string from the device token with no spaces or < >
   
    deviceToken = @"";
    deviceToken = [[NSString alloc] init];
   
    deviceToken = [[[[_deviceToken description]
                     stringByReplacingOccurrencesOfString: @"<" withString: @""]
                    stringByReplacingOccurrencesOfString: @">" withString: @""]
                   stringByReplacingOccurrencesOfString: @" " withString: @""];
   
    NSLog(@"deviceToken = %@",deviceToken);

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
   
    NSString *UAServer = @"https://go.urbanairship.com";
    NSString *urlString = [NSString stringWithFormat:@"%@%@%@/", UAServer, @"/api/device_tokens/", deviceToken];
    NSURL *url = [NSURL URLWithString:urlString];
   
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"PUT"];
   
    // Authenticate to the server
    [request addValue:[NSString stringWithFormat:@"Basic %@",
                       [AppDelegate base64forData:[[NSString stringWithFormat:@"%@:%@",
                                                    kApplicationKey,
                                                    kApplicationSecret] dataUsingEncoding: NSUTF8StringEncoding]]] forHTTPHeaderField:@"Authorization"];
   
    [[NSURLConnection connectionWithRequest:request delegate:self] start];
   
   
    [NSThread detachNewThreadSelector:@selector(registerForRemoteNotification:) toTarget:self withObject:_deviceToken];
   
    //----------------------check DB Count----------------------
   
    [NSThread detachNewThreadSelector:@selector(checkInDatabase) toTarget:self withObject:nil];
   
}

-(void) registerForRemoteNotification:(NSData *) _deviceToken
{
   
    NSString *deviceTokenRegistrationResponse = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://dotnet15.lookandget.com/registerdevice.aspx?udid=%@&subscribe=true",deviceToken]] encoding:NSUTF8StringEncoding error:nil];
   
   
    NSLog(@"deviceTokenRegistrationResponse = %@",deviceTokenRegistrationResponse);
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;   
}



- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
   
    [NSThread detachNewThreadSelector:@selector(checkInDatabase) toTarget:self withObject:nil];
   
    NSLog(@"Failed to register with error: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"remote notification: %@",[userInfo description]);
}

#pragma mark -
#pragma mark connection delegates
- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error {
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSLog(@"ERROR: NSError query result: %@", error);   
}


// From: http://www.cocoadev.com/index.pl?BaseSixtyFour
+ (NSString*)base64forData:(NSData*)theData {
   
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];
   
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;
   
    NSInteger i;
    for (i=0; i < length; i += 3) {
       
        NSInteger value = 0;
        NSInteger j;
       
        for (j = i; j < (i + 3); j++) {
           
            value <<= 8;
           
            if (j < length) {
               
                value |= (0xFF & input[j]);
            }
        }
       
        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
   
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
   

No comments:

Post a Comment