Ticket #631: ios-notification.diff

File ios-notification.diff, 17.6 KB (added by daniloercoli, 23 months ago)

dummy test!

  • Classes/Constants.h

     
    1717#define kTextFieldFont          @"Arial" 
    1818#define kTextViewPlaceholder    @"Tap here to begin writing." 
    1919#define kAppStoreURL                    @"http://itunes.apple.com/us/app/wordpress/id335703880?mt=8" 
     20#define kNotificationAuthURL    @"https://ercolid.wordpress.com/xmlrpc.php" 
    2021 
    2122#define kDisabledTextColor      [UIColor grayColor] 
    2223 
  • Classes/WPDataController.h

     
    7575 
    7676#pragma mark - 
    7777#pragma mark Push Notifications 
    78 - (void)registerForPushNotifications; 
    79 - (void)registerForPushNotificationsInBackground; 
     78- (void)registerForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token deviceUDID:(NSString *)deviceUDID; 
     79- (void)setBlogsForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token blogsID:(NSArray *) blogsID; 
    8080 
    8181#pragma mark - 
    8282#pragma mark XMLRPC 
  • Classes/WordPressAppDelegate.h

     
    6565- (void)showContentDetailViewController:(UIViewController *)viewController; 
    6666- (void)deleteLocalDraft:(NSNotification *)notification; 
    6767- (void)dismissCrashReporter:(NSNotification *)notification; 
    68  
     68- (void)sendApnsToken; 
     69- (void)sendApnsTokenInBackground; 
     70- (void)sendPushNotificationBlogsList; 
     71- (void)sendPushNotificationBlogsListInBackground; 
     72- (void)openNotificationScreenWithOptions:(NSDictionary *)remoteNotif; 
    6973@end 
  • Classes/WPDataController.m

     
    179179    return nil;     
    180180} 
    181181 
    182 - (void)registerForPushNotifications { 
    183         [self performSelectorInBackground:@selector(registerForPushNotificationsInBackground) withObject:nil]; 
    184 } 
    185  
    186 - (void)registerForPushNotificationsInBackground { 
    187         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    188          
    189         if([[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"] != nil) { 
    190                 XMLRPCRequest *req = [[[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:@"http://frsh.wordpress.com/xmlrpc.php"]] autorelease]; 
    191                 NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"]; 
    192                 NSArray *result; 
    193                 NSError *pwError; 
    194                 NSArray *params = [NSArray arrayWithObjects: 
    195                                                    username,  
    196                                                    [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@"WordPress.com" error:&pwError],  
    197                                                    [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"], 
    198                                                    nil]; 
    199                 [req setMethod:@"wpcom.addiOSDeviceToken" withObjects:params]; 
    200                  
    201         retryOnTimeout = YES; 
    202                 result = [self executeXMLRPCRequest:req]; 
    203                  
    204                 // We want this to fail silently. 
    205                 if(![result isKindOfClass:[NSError class]]) 
    206                         NSLog(@"successfully registered for push notifications with WordPress.com: %@", result); 
    207                 else 
    208                         NSLog(@"failed to register for push notifications with WordPress.com: %@", result); 
    209         } 
    210          
    211         [pool release]; 
    212 } 
    213  
    214182- (BOOL)authenticateUser:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password { 
    215183        BOOL result = NO; 
    216184        if((xmlrpc != nil) && (username != nil) && (password != nil)) { 
     
    728696} 
    729697 
    730698#pragma mark - 
     699#pragma mark Notifications 
     700 
     701- (void)registerForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token deviceUDID:(NSString *)deviceUDID {    
     702    XMLRPCRequest *xmlrpcRequest = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:xmlrpc]]; 
     703    [xmlrpcRequest setMethod:@"wpcom.mobile_push_register_token" withObjects:[NSArray arrayWithObjects:username, password, token, deviceUDID, @"apple", nil]]; 
     704    retryOnTimeout = YES; 
     705    id result = [self executeXMLRPCRequest:xmlrpcRequest]; 
     706     
     707    // We want this to fail silently. 
     708    if(![result isKindOfClass:[NSError class]]) 
     709        NSLog(@"successfully registered for push notifications with WordPress.com: %@", result); 
     710    else 
     711        NSLog(@"failed to register for push notifications with WordPress.com: %@", result); 
     712} 
     713 
     714- (void)setBlogsForPushNotifications:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password token:(NSString *)token blogsID:(NSArray *) blogsID {    
     715    XMLRPCRequest *xmlrpcRequest = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:xmlrpc]]; 
     716    [xmlrpcRequest setMethod:@"wpcom.mobile_push_set_blogs_list" withObjects:[NSArray arrayWithObjects:username, password, token, blogsID, @"apple", nil]]; 
     717    retryOnTimeout = YES; 
     718    id result = [self executeXMLRPCRequest:xmlrpcRequest]; 
     719     
     720    // We want this to fail silently. 
     721    if(![result isKindOfClass:[NSError class]]) 
     722        NSLog(@"successfully sent the blogIDs for push notifications with WordPress.com: %@", result); 
     723    else 
     724        NSLog(@"failed to sent blogIDs for notifications with WordPress.com: %@", result); 
     725} 
     726 
     727 
     728#pragma mark - 
    731729#pragma mark XMLRPC 
    732730 
    733731- (NSArray *)getXMLRPCArgsForBlog:(Blog *)blog  withExtraArgs:(NSArray *)args { 
  • Classes/WordPressAppDelegate.m

     
    9898 
    9999#pragma mark - 
    100100#pragma mark UIApplicationDelegate Methods 
    101  
    102 - (void)applicationDidFinishLaunching:(UIApplication *)application {     
     101- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     
    103102#ifndef DEBUG 
    104103//#warning Need Flurry api key for distribution 
    105104#endif 
     
    244243        [window makeKeyAndVisible]; 
    245244         
    246245        // Register for push notifications 
    247         /*[[UIApplication sharedApplication] 
     246        [[UIApplication sharedApplication] 
    248247         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | 
    249248                                                                                 UIRemoteNotificationTypeSound | 
    250                                                                                  UIRemoteNotificationTypeAlert)];*/ 
     249                                                                                 UIRemoteNotificationTypeAlert)]; 
     250     
     251    //Information related to the reason for its launching, which can include things other than notifications. 
     252    NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
     253    if (remoteNotif) { 
     254        NSLog(@"Launched with a remote notification as parameter:  %@", remoteNotif); 
     255        [self openNotificationScreenWithOptions:remoteNotif];   
     256    } 
     257    //the guide say: NO if the application cannot handle the URL resource, otherwise return YES.  
     258    //The return value is ignored if the application is launched as a result of a remote notification. 
     259    return YES; 
    251260} 
    252261 
    253262- (void)handleCrashReport { 
     
    868877        [defaults synchronize]; 
    869878} 
    870879 
    871 #pragma mark Push Notification delegate 
    872  
     880#pragma mark Push Notification  
    873881- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    874882        // Send the deviceToken to our server... 
    875883        NSString *myToken = [[[[deviceToken description] 
     
    881889        [[NSUserDefaults standardUserDefaults] setObject:myToken forKey:@"apnsDeviceToken"]; 
    882890        NSLog(@"Registered for push notifications and stored device token: %@",  
    883891                  [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"]); 
     892 
     893    [self sendApnsTokenInBackground]; 
    884894} 
    885895 
    886896- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 
    887897        NSLog(@"Failed to register for push notifications: %@", error); 
    888898} 
    889899 
     900// The notification is delivered when the application is running 
     901- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
     902    NSLog(@"didReceiveRemoteNotification: %@", userInfo); 
     903    application.applicationIconBadgeNumber = 0; 
     904    /* 
     905     { 
     906     aps =     { 
     907     alert = "New comment on test from maria"; 
     908     badge = 1; 
     909     sound = default; 
     910     }; 
     911     "blog_id" = 16841252; 
     912     "comment_id" = 571; 
     913     }*/ 
     914     
     915    //You can determine whether an application is launched as a result of the user tapping the action button or  
     916    //whether the notification was delivered to the already-running application by examining the application state. 
     917    switch (application.applicationState) { 
     918        case UIApplicationStateActive: 
     919            NSLog(@"app state UIApplicationStateActive"); //application is in foreground 
     920            //we should show an alert since the OS doesn't show anything in this case. Unfortunately no sound!! 
     921            if([self isAlertRunning] != YES) { 
     922                [self setAlertRunning:YES]; 
     923                NSString *msg = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; 
     924                [self showAlertWithTitle:NSLocalizedString(@"Ciao!", @"") message:msg]; 
     925            } 
     926            break; 
     927        case UIApplicationStateInactive: 
     928            NSLog(@"app state UIApplicationStateInactive"); //application is in bg and the user tapped the view button 
     929             [self openNotificationScreenWithOptions:userInfo]; 
     930            break; 
     931        case UIApplicationStateBackground: 
     932            NSLog(@" app state UIApplicationStateBackground"); //?? doh! 
     933            break; 
     934        default: 
     935            break; 
     936    } 
     937} 
     938 
     939- (void)sendApnsToken { 
     940    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
     941         
     942    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"]; 
     943    if( nil == token ) return; //no apns token available 
     944     
     945    NSString *authURL = kNotificationAuthURL;            
     946    NSError *error = nil; 
     947        if([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"] != nil) { 
     948        NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"]; 
     949        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"] != nil) { 
     950            // Migrate password to keychain 
     951            [SFHFKeychainUtils storeUsername:username 
     952                                 andPassword:[[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"] 
     953                              forServiceName:@"WordPress.com" 
     954                              updateExisting:YES error:&error]; 
     955            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"wpcom_password_preference"]; 
     956            [[NSUserDefaults standardUserDefaults] synchronize]; 
     957        } 
     958        NSString *password = [SFHFKeychainUtils getPasswordForUsername:username 
     959                                                        andServiceName:@"WordPress.com" 
     960                                                                 error:&error]; 
     961        if (password != nil) { 
     962            [[WPDataController sharedInstance] registerForPushNotifications: authURL 
     963                                                                   username:username 
     964                                                                   password:password 
     965                                                                      token:token 
     966                                                                 deviceUDID:[[UIDevice currentDevice] uniqueIdentifier] 
     967             ]; 
     968        }  
     969        } 
     970    [self sendPushNotificationBlogsList]; 
     971        [pool release]; 
     972    } 
     973 
     974//send the apns token to out backend. WP.COM credentials are used to avoid spammers 
     975- (void)sendApnsTokenInBackground { 
     976    [self performSelectorInBackground:@selector(sendApnsToken) withObject:nil]; 
     977} 
     978 
     979 
     980- (void)sendPushNotificationBlogsListInBackground { 
     981    [self performSelectorInBackground:@selector(sendPushNotificationBlogsList) withObject:nil]; 
     982} 
     983 
     984- (void)sendPushNotificationBlogsList { 
     985    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
     986     
     987    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"]; 
     988    if( nil == token ) return; //no apns token available 
     989     
     990    NSString *authURL = kNotificationAuthURL;            
     991    NSError *error = nil; 
     992        if([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"] == nil) return; 
     993    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_username_preference"]; 
     994    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"] != nil) { 
     995        // Migrate password to keychain 
     996        [SFHFKeychainUtils storeUsername:username 
     997                             andPassword:[[NSUserDefaults standardUserDefaults] objectForKey:@"wpcom_password_preference"] 
     998                          forServiceName:@"WordPress.com" 
     999                          updateExisting:YES error:&error]; 
     1000        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"wpcom_password_preference"]; 
     1001        [[NSUserDefaults standardUserDefaults] synchronize]; 
     1002    } 
     1003    NSString *password = [SFHFKeychainUtils getPasswordForUsername:username 
     1004                                                    andServiceName:@"WordPress.com" 
     1005                                                             error:&error]; 
     1006    if (password == nil) return; 
     1007     
     1008    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     1009    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Blog" inManagedObjectContext:self.managedObjectContext]]; 
     1010    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"blogName" ascending:YES]; 
     1011    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
     1012    [fetchRequest setSortDescriptors:sortDescriptors]; 
     1013     
     1014    // For some reasons, the cache sometimes gets corrupted 
     1015    // Since we don't really use sections we skip the cache here 
     1016    NSFetchedResultsController *aResultsController = [[NSFetchedResultsController alloc] 
     1017                                                      initWithFetchRequest:fetchRequest 
     1018                                                      managedObjectContext:self.managedObjectContext 
     1019                                                      sectionNameKeyPath:nil 
     1020                                                      cacheName:nil]; 
     1021     
     1022    [aResultsController performFetch:nil]; 
     1023     
     1024    NSMutableArray *blogsID = [NSMutableArray array]; 
     1025     
     1026    //get a references to media files linked in a post 
     1027    for (Blog *blog in [aResultsController fetchedObjects]) { 
     1028        if( [blog isWPcom] ) { 
     1029            [blogsID addObject:[blog blogID] ]; 
     1030        } 
     1031    } 
     1032     
     1033    [[WPDataController sharedInstance] setBlogsForPushNotifications: authURL 
     1034                                                           username:username 
     1035                                                           password:password 
     1036                                                              token:token 
     1037                                                            blogsID:blogsID 
     1038     ];         
     1039     
     1040     
     1041    [aResultsController release]; 
     1042    [fetchRequest release]; 
     1043    [sortDescriptor release]; sortDescriptor = nil; 
     1044    [sortDescriptors release]; sortDescriptors = nil; 
     1045     
     1046    [pool release]; 
     1047 
     1048  } 
     1049 
     1050- (void)openNotificationScreenWithOptions:(NSDictionary *)remoteNotif { 
     1051    NSLog(@"Opening the notification screen"); 
     1052} 
     1053 
    8901054#pragma mark - 
    8911055#pragma mark NSURLConnection callbacks 
    8921056 
  • Classes/BlogsViewController.h

     
    4141- (void)quickPhotoPost; 
    4242- (void)uploadQuickPhoto:(Post *)post; 
    4343- (void)showQuickPhotoButton:(BOOL)delay; 
    44  
    4544@end 
  • Classes/BlogsViewController.m

     
    130130 
    131131- (void)blogsRefreshNotificationReceived:(NSNotification *)notification { 
    132132        [resultsController performFetch:nil]; 
     133    [appDelegate sendPushNotificationBlogsListInBackground];  
    133134        [self checkEditButton]; 
    134135} 
    135136 
     
    254255                                WPFLog(@"Unresolved Core Data Save error %@, %@", error, [error userInfo]); 
    255256                                exit(-1); 
    256257                        } 
     258            [appDelegate sendPushNotificationBlogsListInBackground]; 
    257259                } else { 
    258260                        //the blog is using the network connection and cannot be stoped, show a message to the user 
    259261                        UIAlertView *blogIsCurrentlyBusy = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", @"Info alert title") 
     
    501503        } 
    502504} 
    503505 
    504  
    505506#pragma mark - 
    506507#pragma mark UIAlertView delegate 
    507508 
  • Classes/WPcomLoginViewController.m

     
    330330                [self saveLoginData]; 
    331331                 
    332332                // Register this device for push notifications with WordPress.com if necessary 
    333                 [[WPDataController sharedInstance] registerForPushNotifications]; 
     333        [[WordPressAppDelegate sharedWordPressApp] sendApnsTokenInBackground]; 
    334334        } 
    335335        else { 
    336336                isAuthenticated = NO; 
  • Classes/AddUsersBlogsViewController.m

     
    365365        else { 
    366366                [appDelegate.navigationController popToRootViewControllerAnimated:YES]; 
    367367        } 
     368    [appDelegate sendPushNotificationBlogsListInBackground];  
    368369} 
    369370 
    370371- (void)createBlog:(NSDictionary *)blogInfo {