【发布时间】:2015-01-20 16:57:30
【问题描述】:
我已经阅读了一些关于如何使用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");
}
}
在模拟器和设备上一切正常 - 当应用程序运行时,我会收到我的预定通知。 但是当 应用程序未运行时,即当应用程序处于非活动状态时,我需要接收通知( home 按了一次)
问题是,例如,如果我在按钮按下时取消通知,我会毫无问题地得到它,它会显示在通知中心,并且会根据需要在我的应用程序图标上添加徽章。但我只想在我的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 中设置的警报)
我很乐意提供任何建议和帮助。
【问题讨论】:
-
你为什么将开火日期设置为即时?这不会总是在前台为你开火吗?它应该在未来的某个日期触发
-
@StevenRitchie 因为它满足我的条件。我需要在
progCreate == 100时收到通知 -
@Bhavin 不,它通过服务器端的推送通知解决,本地通知仍然不显示
-
你...同样的问题....我不知道为什么
-
如果您在后台立即需要它,您需要将它添加到主运行循环中。对于任何遇到上述问题的人。
标签: ios objective-c background ios-simulator uilocalnotification