我阅读了一些关于如何使用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");
}
}
在模拟器和设备上一切都正常 - 当应用程序运行时,我会收到我的预定通知。但是我需要在应用程序处于非活动状态(按下主页键一次)时接收通知。
问题在于,如果我例如在按钮按下时设置我的通知,我将毫无问题地收到它,并且它将显示在通知中心中,并且将添加徽章到我的应用程序图标上,就像我想要的那样。但是我只想在我的 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
中设置的那样显示提示)。
我很高兴听取任何建议和帮助。
progCreate == 100
时,我需要接收通知。 - vendettacore