Ticket #631: ios-notification.diff
| File ios-notification.diff, 17.6 KB (added by daniloercoli, 23 months ago) |
|---|
-
Classes/Constants.h
17 17 #define kTextFieldFont @"Arial" 18 18 #define kTextViewPlaceholder @"Tap here to begin writing." 19 19 #define kAppStoreURL @"http://itunes.apple.com/us/app/wordpress/id335703880?mt=8" 20 #define kNotificationAuthURL @"https://ercolid.wordpress.com/xmlrpc.php" 20 21 21 22 #define kDisabledTextColor [UIColor grayColor] 22 23 -
Classes/WPDataController.h
75 75 76 76 #pragma mark - 77 77 #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; 80 80 81 81 #pragma mark - 82 82 #pragma mark XMLRPC -
Classes/WordPressAppDelegate.h
65 65 - (void)showContentDetailViewController:(UIViewController *)viewController; 66 66 - (void)deleteLocalDraft:(NSNotification *)notification; 67 67 - (void)dismissCrashReporter:(NSNotification *)notification; 68 68 - (void)sendApnsToken; 69 - (void)sendApnsTokenInBackground; 70 - (void)sendPushNotificationBlogsList; 71 - (void)sendPushNotificationBlogsListInBackground; 72 - (void)openNotificationScreenWithOptions:(NSDictionary *)remoteNotif; 69 73 @end -
Classes/WPDataController.m
179 179 return nil; 180 180 } 181 181 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 else208 NSLog(@"failed to register for push notifications with WordPress.com: %@", result);209 }210 211 [pool release];212 }213 214 182 - (BOOL)authenticateUser:(NSString *)xmlrpc username:(NSString *)username password:(NSString *)password { 215 183 BOOL result = NO; 216 184 if((xmlrpc != nil) && (username != nil) && (password != nil)) { … … 728 696 } 729 697 730 698 #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 - 731 729 #pragma mark XMLRPC 732 730 733 731 - (NSArray *)getXMLRPCArgsForBlog:(Blog *)blog withExtraArgs:(NSArray *)args { -
Classes/WordPressAppDelegate.m
98 98 99 99 #pragma mark - 100 100 #pragma mark UIApplicationDelegate Methods 101 102 - (void)applicationDidFinishLaunching:(UIApplication *)application { 101 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 103 102 #ifndef DEBUG 104 103 //#warning Need Flurry api key for distribution 105 104 #endif … … 244 243 [window makeKeyAndVisible]; 245 244 246 245 // Register for push notifications 247 /*[[UIApplication sharedApplication]246 [[UIApplication sharedApplication] 248 247 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | 249 248 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; 251 260 } 252 261 253 262 - (void)handleCrashReport { … … 868 877 [defaults synchronize]; 869 878 } 870 879 871 #pragma mark Push Notification delegate 872 880 #pragma mark Push Notification 873 881 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 874 882 // Send the deviceToken to our server... 875 883 NSString *myToken = [[[[deviceToken description] … … 881 889 [[NSUserDefaults standardUserDefaults] setObject:myToken forKey:@"apnsDeviceToken"]; 882 890 NSLog(@"Registered for push notifications and stored device token: %@", 883 891 [[NSUserDefaults standardUserDefaults] objectForKey:@"apnsDeviceToken"]); 892 893 [self sendApnsTokenInBackground]; 884 894 } 885 895 886 896 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 887 897 NSLog(@"Failed to register for push notifications: %@", error); 888 898 } 889 899 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 890 1054 #pragma mark - 891 1055 #pragma mark NSURLConnection callbacks 892 1056 -
Classes/BlogsViewController.h
41 41 - (void)quickPhotoPost; 42 42 - (void)uploadQuickPhoto:(Post *)post; 43 43 - (void)showQuickPhotoButton:(BOOL)delay; 44 45 44 @end -
Classes/BlogsViewController.m
130 130 131 131 - (void)blogsRefreshNotificationReceived:(NSNotification *)notification { 132 132 [resultsController performFetch:nil]; 133 [appDelegate sendPushNotificationBlogsListInBackground]; 133 134 [self checkEditButton]; 134 135 } 135 136 … … 254 255 WPFLog(@"Unresolved Core Data Save error %@, %@", error, [error userInfo]); 255 256 exit(-1); 256 257 } 258 [appDelegate sendPushNotificationBlogsListInBackground]; 257 259 } else { 258 260 //the blog is using the network connection and cannot be stoped, show a message to the user 259 261 UIAlertView *blogIsCurrentlyBusy = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", @"Info alert title") … … 501 503 } 502 504 } 503 505 504 505 506 #pragma mark - 506 507 #pragma mark UIAlertView delegate 507 508 -
Classes/WPcomLoginViewController.m
330 330 [self saveLoginData]; 331 331 332 332 // Register this device for push notifications with WordPress.com if necessary 333 [[WPDataController sharedInstance] registerForPushNotifications];333 [[WordPressAppDelegate sharedWordPressApp] sendApnsTokenInBackground]; 334 334 } 335 335 else { 336 336 isAuthenticated = NO; -
Classes/AddUsersBlogsViewController.m
365 365 else { 366 366 [appDelegate.navigationController popToRootViewControllerAnimated:YES]; 367 367 } 368 [appDelegate sendPushNotificationBlogsListInBackground]; 368 369 } 369 370 370 371 - (void)createBlog:(NSDictionary *)blogInfo {
