本地通知不在设备上工作,但在模拟器上工作

我读过一些如何使用UILocalNotification的指南。 所以我从第一次尝试就试过了,没有成功。 要在AppDelegate.m中注册通知,我使用:

  - (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; } 

并在应用程序运行时收到通知:

  - (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"); } } 

在模拟器和设备上一切正常 – 当应用程序正在运行时,我收到我的通知。 但我需要收到通知,当应用程序不运行,即当应用程序不活动(家庭按下一次)

问题是,如果我按照我的通知button按下例如,我会得到它没有任何问题,它会显示在通知中心和徽章将被添加到我的应用程序的图标,如我所愿。 但是我只想在我的AFNetworking任务结束时收到通知。

为了设置通知我使用以下方法:

  -(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); }]; } 

主要的问题是,这种方法只适用于iOS 8.0的模拟器,它不适用于iOS 7.1.1的iPhone 4和iOS 8.1.2的iPhone 5c,它也不适用于iOS 7.1的模拟器。 在iOS 7.1+的真实设备和模拟器上,我从来没有注意到在后台接收通知,只有当我打开应用程序才能得到它(然后显示我警觉,因为我在AppDelegate.m设置),我会很高兴任何build议和任何帮助。

我看到你正在使用Objective-C。 考虑到一年前问过这个问题,你可能已经想出了这个问题,或者你现在正在使用Swift 2.0。 我有同样的问题,我能够通过添加以下代码与Swift 2.0解决它。

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 } 

你可以在这里看到我的完整答案。

您在iOS 8.0+ Simulator中获取通知的事实是iOS Simulator运行时的限制。 在设备上,当你的应用程序在后台,它并没有真正运行,任务被暂停。 在iOS模拟器中,任务继续在后台运行。

有时我们不会立即得到本地通知,但在1小时后我几乎得到通知。 所以,等待一个特定的时间,然后再次检查。