我读过一些如何使用的指南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
)
我很高兴收到任何建议和帮助。