Local notifications do not work on the device, but work on the simulator

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){ //shedule notification for immediately showing UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; localNotification.alertBody = [NSString stringWithFormat:@"Notification text!"]; localNotification.alertAction = @"Show"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [localNotification setSoundName:UILocalNotificationDefaultSoundName]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; // [[UIApplication sharedApplication]presentLocalNotificationNow:localNotification]; the same result as above } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; NSLog(@"Error: %@", error); }]; } 

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.

+4
source share
3 answers

I see that you are using Objective-C. Given that this question was asked a year ago, you probably either understood it or were using Swift 2.0 now. I had the same problem and was able to resolve it using Swift 2.0 by adding the following code.

AppDelegate.swift

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)) application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil) return true } 

You can see my full answer here .

+5
source

The fact that you receive a notification in iOS 8.0+ Simulator is a limitation on the runtime of iOS Simulator. On the device, when your application is in the background, it does not actually start, the task pauses. In iOS Simulator, the task continues to run in the background.

+3
source

Sometimes we do not receive a local notification instantly, but after 1 hour I receive a notification. So, wait a while, then check.

0
source

Source: https://habr.com/ru/post/1241469/


All Articles