Index: Classes/Constants.h
===================================================================
--- Classes/Constants.h	(revision 2133)
+++ Classes/Constants.h	(working copy)
@@ -17,6 +17,7 @@
 #define kTextFieldFont          @"Arial"
 #define kTextViewPlaceholder	@"Tap here to begin writing."
 #define kAppStoreURL			@"http://itunes.apple.com/us/app/wordpress/id335703880?mt=8"
+#define kNotificationAuthURL	@"https://ercolid.wordpress.com/xmlrpc.php"
 
 #define kDisabledTextColor      [UIColor grayColor]
 
Index: Classes/WPDataController.h
===================================================================
--- Classes/WPDataController.h	(revision 2133)
+++ Classes/WPDataController.h	(working copy)
@@ -75,8 +75,8 @@
 
 #pragma mark -
 #pragma mark Push Notifications
-- (void)registerForPushNotifications;
-- (void)registerForPushNotificationsInBackground;
+- (void)registerForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token deviceUDID:(NSString *)deviceUDID;
+- (void)setBlogsForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token blogsID:(NSArray *) blogsID;
 
 #pragma mark -
 #pragma mark XMLRPC
Index: Classes/WordPressAppDelegate.h
===================================================================
--- Classes/WordPressAppDelegate.h	(revision 2133)
+++ Classes/WordPressAppDelegate.h	(working copy)
@@ -65,5 +65,9 @@
 - (void)showContentDetailViewController:(UIViewController *)viewController;
 - (void)deleteLocalDraft:(NSNotification *)notification;
 - (void)dismissCrashReporter:(NSNotification *)notification;
-
+- (void)sendApnsToken;
+- (void)sendApnsTokenInBackground;
+- (void)sendPushNotificationBlogsList;
+- (void)sendPushNotificationBlogsListInBackground;
+- (void)openNotificationScreenWithOptions:(NSDictionary *)remoteNotif;
 @end
Index: Classes/WPDataController.m
===================================================================
--- Classes/WPDataController.m	(revision 2133)
+++ Classes/WPDataController.m	(working copy)
@@ -179,38 +179,6 @@
     return nil;    
 }
 
-- (void)registerForPushNotifications {
-	[self performSelectorInBackground:@selector(registerForPushNotificationsInBackground) withObject:nil];
-}
-
-- (void)registerForPushNotificationsInBackground {
-	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-	
-	if([[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"] != nil) {
-		XMLRPCRequest *req = [[[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:@"http://frsh.wordpress.com/xmlrpc.php"]] autorelease];
-		NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"];
-		NSArray *result;
-		NSError *pwError;
-		NSArray *params = [NSArray arrayWithObjects:
-						   username, 
-						   [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@"WordPress.com" error:&pwError], 
-						   [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"],
-						   nil];
-		[req setMethod:@"wpcom.addiOSDeviceToken" withObjects:params];
-		
-        retryOnTimeout = YES;
-		result = [self executeXMLRPCRequest:req];
-		
-		// We want this to fail silently.
-		if(![result isKindOfClass:[NSError class]])
-			NSLog(@"successfully registered for push notifications with WordPress.com: %@", result);
-		else
-			NSLog(@"failed to register for push notifications with WordPress.com: %@", result);
-	}
-	
-	[pool release];
-}
-
 - (BOOL)authenticateUser:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password {
 	BOOL result = NO;
 	if((xmlrpc != nil) && (username != nil) && (password != nil)) {
@@ -728,6 +696,36 @@
 }
 
 #pragma mark -
+#pragma mark Notifications
+
+- (void)registerForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token deviceUDID:(NSString *)deviceUDID {   
+    XMLRPCRequest *xmlrpcRequest = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:xmlrpc]];
+    [xmlrpcRequest setMethod:@"wpcom.mobile_push_register_token" withObjects:[NSArray arrayWithObjects:username, password, token, deviceUDID, @"apple", nil]];
+    retryOnTimeout = YES;
+    id result = [self executeXMLRPCRequest:xmlrpcRequest];
+    
+    // We want this to fail silently.
+    if(![result isKindOfClass:[NSError class]])
+        NSLog(@"successfully registered for push notifications with WordPress.com: %@", result);
+    else
+        NSLog(@"failed to register for push notifications with WordPress.com: %@", result);
+}
+
+- (void)setBlogsForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token blogsID:(NSArray *) blogsID {   
+    XMLRPCRequest *xmlrpcRequest = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:xmlrpc]];
+    [xmlrpcRequest setMethod:@"wpcom.mobile_push_set_blogs_list" withObjects:[NSArray arrayWithObjects:username, password, token, blogsID, @"apple", nil]];
+    retryOnTimeout = YES;
+    id result = [self executeXMLRPCRequest:xmlrpcRequest];
+    
+    // We want this to fail silently.
+    if(![result isKindOfClass:[NSError class]])
+        NSLog(@"successfully sent the blogIDs for push notifications with WordPress.com: %@", result);
+    else
+        NSLog(@"failed to sent blogIDs for notifications with WordPress.com: %@", result);
+}
+
+
+#pragma mark -
 #pragma mark XMLRPC
 
 - (NSArray *)getXMLRPCArgsForBlog:(Blog *)blog  withExtraArgs:(NSArray *)args {
Index: Classes/WordPressAppDelegate.m
===================================================================
--- Classes/WordPressAppDelegate.m	(revision 2133)
+++ Classes/WordPressAppDelegate.m	(working copy)
@@ -98,8 +98,7 @@
 
 #pragma mark -
 #pragma mark UIApplicationDelegate Methods
-
-- (void)applicationDidFinishLaunching:(UIApplication *)application {	
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
 #ifndef DEBUG
 //#warning Need Flurry api key for distribution
 #endif
@@ -244,10 +243,20 @@
 	[window makeKeyAndVisible];
 	
 	// Register for push notifications
-	/*[[UIApplication sharedApplication]
+	[[UIApplication sharedApplication]
 	 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
 										 UIRemoteNotificationTypeSound |
-										 UIRemoteNotificationTypeAlert)];*/
+										 UIRemoteNotificationTypeAlert)];
+    
+    //Information related to the reason for its launching, which can include things other than notifications.
+    NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
+    if (remoteNotif) {
+        NSLog(@"Launched with a remote notification as parameter:  %@", remoteNotif);
+        [self openNotificationScreenWithOptions:remoteNotif];  
+    }
+    //the guide say: NO if the application cannot handle the URL resource, otherwise return YES. 
+    //The return value is ignored if the application is launched as a result of a remote notification.
+    return YES;
 }
 
 - (void)handleCrashReport {
@@ -868,8 +877,7 @@
 	[defaults synchronize];
 }
 
-#pragma mark Push Notification delegate
-
+#pragma mark Push Notification 
 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
 	// Send the deviceToken to our server...
 	NSString *myToken = [[[[deviceToken description]
@@ -881,12 +889,168 @@
 	[[NSUserDefaults standardUserDefaults] setObject:myToken forKey:@"apnsDeviceToken"];
 	NSLog(@"Registered for push notifications and stored device token: %@", 
 		  [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"]);
+
+    [self sendApnsTokenInBackground];
 }
 
 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
 	NSLog(@"Failed to register for push notifications: %@", error);
 }
 
+// The notification is delivered when the application is running
+- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
+    NSLog(@"didReceiveRemoteNotification: %@", userInfo);
+    application.applicationIconBadgeNumber = 0;
+    /*
+     {
+     aps =     {
+     alert = "New comment on test from maria";
+     badge = 1;
+     sound = default;
+     };
+     "blog_id" = 16841252;
+     "comment_id" = 571;
+     }*/
+    
+    //You can determine whether an application is launched as a result of the user tapping the action button or 
+    //whether the notification was delivered to the already-running application by examining the application state.
+    switch (application.applicationState) {
+        case UIApplicationStateActive:
+            NSLog(@"app state UIApplicationStateActive"); //application is in foreground
+            //we should show an alert since the OS doesn't show anything in this case. Unfortunately no sound!!
+            if([self isAlertRunning] != YES) {
+                [self setAlertRunning:YES];
+                NSString *msg = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
+                [self showAlertWithTitle:NSLocalizedString(@"Ciao!", @"") message:msg];
+            }
+            break;
+        case UIApplicationStateInactive:
+            NSLog(@"app state UIApplicationStateInactive"); //application is in bg and the user tapped the view button
+             [self openNotificationScreenWithOptions:userInfo];
+            break;
+        case UIApplicationStateBackground:
+            NSLog(@" app state UIApplicationStateBackground"); //?? doh!
+            break;
+        default:
+            break;
+    }
+}
+
+- (void)sendApnsToken {
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+	
+    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"];
+    if( nil == token ) return; //no apns token available
+    
+    NSString *authURL = kNotificationAuthURL;   	
+    NSError *error = nil;
+	if([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"] != nil) {
+        NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"];
+        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"] != nil) {
+            // Migrate password to keychain
+            [SFHFKeychainUtils storeUsername:username
+                                 andPassword:[[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"]
+                              forServiceName:@"WordPress.com"
+                              updateExisting:YES error:&error];
+            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"wpcom_password_preference"];
+            [[NSUserDefaults standardUserDefaults] synchronize];
+        }
+        NSString *password = [SFHFKeychainUtils getPasswordForUsername:username
+                                                        andServiceName:@"WordPress.com"
+                                                                 error:&error];
+        if (password != nil) {
+            [[WPDataController sharedInstance] registerForPushNotifications: authURL
+                                                                   username:username
+                                                                   password:password
+                                                                      token:token
+                                                                 deviceUDID:[[UIDevice currentDevice] uniqueIdentifier]
+             ];
+        } 
+	}
+    [self sendPushNotificationBlogsList];
+	[pool release];
+    }
+
+//send the apns token to out backend. WP.COM credentials are used to avoid spammers
+- (void)sendApnsTokenInBackground {
+    [self performSelectorInBackground:@selector(sendApnsToken) withObject:nil];
+}
+
+
+- (void)sendPushNotificationBlogsListInBackground {
+    [self performSelectorInBackground:@selector(sendPushNotificationBlogsList) withObject:nil];
+}
+
+- (void)sendPushNotificationBlogsList {
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    
+    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"];
+    if( nil == token ) return; //no apns token available
+    
+    NSString *authURL = kNotificationAuthURL;   	
+    NSError *error = nil;
+	if([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"] == nil) return;
+    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"];
+    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"] != nil) {
+        // Migrate password to keychain
+        [SFHFKeychainUtils storeUsername:username
+                             andPassword:[[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"]
+                          forServiceName:@"WordPress.com"
+                          updateExisting:YES error:&error];
+        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"wpcom_password_preference"];
+        [[NSUserDefaults standardUserDefaults] synchronize];
+    }
+    NSString *password = [SFHFKeychainUtils getPasswordForUsername:username
+                                                    andServiceName:@"WordPress.com"
+                                                             error:&error];
+    if (password == nil) return;
+    
+    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
+    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Blog" inManagedObjectContext:self.managedObjectContext]];
+    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"blogName" ascending:YES];
+    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
+    [fetchRequest setSortDescriptors:sortDescriptors];
+    
+    // For some reasons, the cache sometimes gets corrupted
+    // Since we don't really use sections we skip the cache here
+    NSFetchedResultsController *aResultsController = [[NSFetchedResultsController alloc]
+                                                      initWithFetchRequest:fetchRequest
+                                                      managedObjectContext:self.managedObjectContext
+                                                      sectionNameKeyPath:nil
+                                                      cacheName:nil];
+    
+    [aResultsController performFetch:nil];
+    
+    NSMutableArray *blogsID = [NSMutableArray array];
+    
+    //get a references to media files linked in a post
+    for (Blog *blog in [aResultsController fetchedObjects]) {
+        if( [blog isWPcom] ) {
+            [blogsID addObject:[blog blogID] ];
+        }
+    }
+    
+    [[WPDataController sharedInstance] setBlogsForPushNotifications: authURL
+                                                           username:username
+                                                           password:password
+                                                              token:token
+                                                            blogsID:blogsID
+     ];        
+    
+    
+    [aResultsController release];
+    [fetchRequest release];
+    [sortDescriptor release]; sortDescriptor = nil;
+    [sortDescriptors release]; sortDescriptors = nil;
+    
+    [pool release];
+
+  }
+
+- (void)openNotificationScreenWithOptions:(NSDictionary *)remoteNotif {
+    NSLog(@"Opening the notification screen");
+}
+
 #pragma mark -
 #pragma mark NSURLConnection callbacks
 
Index: Classes/BlogsViewController.h
===================================================================
--- Classes/BlogsViewController.h	(revision 2133)
+++ Classes/BlogsViewController.h	(working copy)
@@ -41,5 +41,4 @@
 - (void)quickPhotoPost;
 - (void)uploadQuickPhoto:(Post *)post;
 - (void)showQuickPhotoButton:(BOOL)delay;
-
 @end
Index: Classes/BlogsViewController.m
===================================================================
--- Classes/BlogsViewController.m	(revision 2133)
+++ Classes/BlogsViewController.m	(working copy)
@@ -130,6 +130,7 @@
 
 - (void)blogsRefreshNotificationReceived:(NSNotification *)notification {
 	[resultsController performFetch:nil];
+    [appDelegate sendPushNotificationBlogsListInBackground]; 
 	[self checkEditButton];
 }
 
@@ -254,6 +255,7 @@
 				WPFLog(@"Unresolved Core Data Save error %@, %@", error, [error userInfo]);
 				exit(-1);
 			}
+            [appDelegate sendPushNotificationBlogsListInBackground];
 		} else {
 			//the blog is using the network connection and cannot be stoped, show a message to the user
 			UIAlertView *blogIsCurrentlyBusy = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", @"Info alert title")
@@ -501,7 +503,6 @@
 	}
 }
 
-
 #pragma mark -
 #pragma mark UIAlertView delegate
 
Index: Classes/WPcomLoginViewController.m
===================================================================
--- Classes/WPcomLoginViewController.m	(revision 2133)
+++ Classes/WPcomLoginViewController.m	(working copy)
@@ -330,7 +330,7 @@
 		[self saveLoginData];
 		
 		// Register this device for push notifications with WordPress.com if necessary
-		[[WPDataController sharedInstance] registerForPushNotifications];
+        [[WordPressAppDelegate sharedWordPressApp] sendApnsTokenInBackground];
 	}
 	else {
 		isAuthenticated = NO;
Index: Classes/AddUsersBlogsViewController.m
===================================================================
--- Classes/AddUsersBlogsViewController.m	(revision 2133)
+++ Classes/AddUsersBlogsViewController.m	(working copy)
@@ -365,6 +365,7 @@
 	else {
 		[appDelegate.navigationController popToRootViewControllerAnimated:YES];
 	}
+    [appDelegate sendPushNotificationBlogsListInBackground]; 
 }
 
 - (void)createBlog:(NSDictionary *)blogInfo {
