Monday 18 March 2013

Push Notification in iphone sdk

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [NSThread sleepForTimeInterval:5.0];
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.viewController = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
    
    [self.window addSubview:navControll.view];
    [self.window makeKeyAndVisible];
    
    NSLog(@"Initiating remoteNoticationssAreActive"); 
    // / Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    
    // UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    //[self sendProviderDeviceToken];
    NSLog(@"%@",[[[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"aps"] objectForKey:@"alert"]);
    
    
    return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    //NSLog(@"selected is %@",viewController);
    
    if(tabBarController.selectedViewController)
    {
        NSLog(@"selected is %@",tabBarController.selectedViewController);
    }
    
    
    NSArray *vc= tabBarController.viewControllers;
    for (int i = 0; i < [vc count]; i++) {
        UINavigationController *nc = [vc objectAtIndex:i];
        if (nc == tabBarController.selectedViewController) {
            continue;
        }
        [nc popToRootViewControllerAnimated:NO];
    }
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    
    NSString *str = [NSString 
                        stringWithFormat:@"Device Token=%@",deviceToken];
    NSLog(@"%@",str);
    
    str = [[[deviceToken description] 
              stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@""]] 
              stringByReplacingOccurrencesOfString:@" " withString:@""];;
    NSLog(@"Device Token : %@", str);
    ///c7dc86e8376a46825c98a7d4aa48c8d46d43025798ee2ac85fe97b47488c4a88
    NSLog(@"Remote type : %d", [[UIApplication sharedApplication] enabledRemoteNotificationTypes]);
    
    
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
    
    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(@"%@",str); 
    
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // for (id key in userInfo) {
    // NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    // } 
    NSLog(@"Received Notification");
    NSLog(@"remote notification: %@",[userInfo description]);
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    
    NSString *alert = [apsInfo objectForKey:@"alert"];
    NSLog(@"Received Push Alert: %@", alert);
    
    NSString *sound = [apsInfo objectForKey:@"sound"];
    NSLog(@"Received Push Sound: %@", sound);
    
    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);
    application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
    
    
}

1 comment: