Skip to content

Instantly share code, notes, and snippets.

@akatreyt
Created December 27, 2016 21:55
Show Gist options
  • Save akatreyt/242ba195f31d1b31189b3043dfcc1397 to your computer and use it in GitHub Desktop.
Save akatreyt/242ba195f31d1b31189b3043dfcc1397 to your computer and use it in GitHub Desktop.
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateBackground || (state == UIApplicationStateInactive)) {
NSDictionary *aps = userInfo[@"aps"];
if (aps) {
NSLog(@"Background fetch started...");
NSString *urlString = [NSString stringWithFormat:@"https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201612081039"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlString]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (!error && httpResp.statusCode == 200) {
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"Background fetch completed...");
} else {
NSLog(@"%@", error.description);
completionHandler(UIBackgroundFetchResultFailed);
NSLog(@"Background fetch Failed...");
}
}
] resume
];
}
} else if (state == UIApplicationStateInactive) {
// user tapped notification
completionHandler(UIBackgroundFetchResultNoData);
} else {
// app is active
completionHandler(UIBackgroundFetchResultNoData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment