【问题标题】:Local notifications are not working on device but working on simulator本地通知在设备上不起作用,但在模拟器上起作用
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 你在说什么任务?如果您的意思是我的 getProgress 任务无法在后台运行,那就错了。因为当应用程序未运行然后重新打开时进度更改(必须)所以我不明白为什么如果我的任务有效,为什么不会在后台发送通知
    • 运行时,您的应用程序是一个 mach 任务/BSD 进程。当用户将您的应用程序置于后台时,任务会暂停(您没有时间在 CPU 上)。在 sim 中,您的任务在置于后台时会继续运行。这是模拟器运行时中的一个错误,它与设备不匹配。
    • 是的,我尝试使用 UILocalNotification 发布示例通知。它不适用于 iOS 模拟器“iPhone 6 / 8.3”
    【解决方案3】:

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

    【讨论】:

    • 但在我的情况下,我需要获得即时通知,因为它们会通知我例如完成下载
    • 检查您为该通知设置的时间和日期。
    猜你喜欢
    • 2013-06-18
    • 2012-07-11
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多