I read several tutorials on how to use UILocalNotification . Therefore, I tried and did not succeed on the first try. To register notifications in AppDelegate.m, I use:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... //if it is iOS 8 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:settings]; } else // if iOS 7 { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (locationNotification) { // Set icon badge number to zero application.applicationIconBadgeNumber = 0; } return YES; }
and receive notification when the application is running :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"notification recieved %@",notification.alertBody); // NSLog(@"notification userinfo %@",notification.userInfo); UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { NSLog(@"notification fired in foreground"); UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Notification!" message:notification.alertBody delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [message show]; } else if (state == UIApplicationStateInactive){ NSLog(@"notification fired in inactive"); } else if (state == UIApplicationStateBackground){ NSLog(@"notification fired in background"); } }
And everything is fine on the simulator and on the device too - when the application is running. I receive my scheduled notification. But I need to receive notifications when the application does not work, i.e. when the application is inactive (once pressed at home)
The problem is that if I, for example, plan my notification on the button, I will receive it without any problems and it will be shown in the notification center and the icon will be added to the icon of my application as I want. But I want to receive notifications only when my AFNetworking task is completed.
To configure the notification, I use the following method:
-(void)getProgress{ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSString *token = [defaults objectForKey:@"token"]; NSString *header = [NSString stringWithFormat:@"Bearer %@",token]; NSDictionary *params = @{@"lang": @"en",@"project":projId}; manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager.requestSerializer setValue:header forHTTPHeaderField:@"Authorization"]; [manager POST:@"http://example.com/api/get-progress" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if ([[responseObject objectForKey:@"result"]isEqualToString:@"success"]){ progCreate = [responseObject objectForKey:@"progress"]; if ([progCreate intValue]==100){
The main problem is that this method only works on a simulator with iOS version 8.0 , and it does not work on iPhone 4 with iOS 7.1.1 and iPhone 5c with iOS 8.1.2, it also does not work. Do not work with a simulator with iOS 7.1 . On real devices and simulators with iOS 7.1+, I never noticed that I receive a notification in the background and can only receive it if I open the application (then it will show me a warning when I installed in AppDelegate.m ). I will be happy for any suggestions and any help.