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

时间:2015-01-20 16:57:31

标签: ios objective-c background ios-simulator uilocalnotification

我已阅读了一些有关如何使用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中设置时显示警报) 我很高兴有任何建议和帮助。

2 个答案:

答案 0 :(得分:5)

我看到你正在使用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
}

您可以看到我的完整答案here

答案 1 :(得分:3)

您在iOS 8.0+模拟器中收到通知的事实是iOS模拟器运行时的限制。在设备上,当您的应用在后台时,它实际上并未运行,任务将被暂停。在iOS模拟器中,任务继续在后台运行。