Skip to content

Instantly share code, notes, and snippets.

@kenshin03
Created May 17, 2013 21:12
Show Gist options
  • Save kenshin03/5602000 to your computer and use it in GitHub Desktop.
Save kenshin03/5602000 to your computer and use it in GitHub Desktop.
Fetch notifications
- (void) fetchNotifications:(FetchNotificationsSuccess)fetchNotificationsSuccess {
InitAccountSuccessBlock successBlock = ^{
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/notifications"];
SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:@{@"include_read":@"true"}];
DDLogVerbose(@"request.URL: %@", request.URL);
request.account = self.facebookAccount;
NSMutableArray * notificationResultsArray = [@[] mutableCopy];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString * responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
DDLogVerbose(@"responseString: %@", responseString);
NSError* responseError;
NSDictionary* jsonDict = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&responseError];
NSArray * notificationsJSONArray = jsonDict[@"data"];
[notificationsJSONArray enumerateObjectsUsingBlock:^(NSDictionary * notificationJSONDict, NSUInteger idx, BOOL *stop) {
Notification * notification = [Notification createInContext:[NSManagedObjectContext defaultContext]];
// timestamps
NSString * createdTime = notificationJSONDict[@"created_time"];
NSString * updatedTime = notificationJSONDict[@"updated_time"];
notification.fromGraphID = notificationJSONDict[@"from"][@"id"];
notification.title = notificationJSONDict[@"title"];
notification.applicationName = notificationJSONDict[@"application"][@"name"];
notification.link = notificationJSONDict[@"link"];
notification.createdTime = [self.dateFormatter dateFromString:createdTime];
notification.updatedTime = [self.dateFormatter dateFromString:updatedTime];
[notificationResultsArray addObject:notification];
}];
fetchNotificationsSuccess(notificationResultsArray, nil);
}];
};
if (self.facebookAccount == nil){
[self initAccount:successBlock];
}else{
successBlock();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment