【问题标题】:iOS 8 [UIApplication sharedApplication].scheduledLocalNotifications emptyiOS 8 [UIApplication sharedApplication].scheduledLocalNotifications 为空
【发布时间】:2014-09-20 11:00:03
【问题描述】:

我正在尝试将我的应用程序更新到 iOS 8。在一个函数中,我以这种方式安排了本地通知(我已经检查了 firedate 和通知的所有其他部分是否正确):

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

然后我使用此代码打印预定的本地通知:

NSLog(@"notifications %@", [UIApplication sharedApplication].scheduledLocalNotifications );

但数组

[UIApplication sharedApplication].scheduledLocalNotifications 

即使没有触发通知也是空的。 然后,为了检查本地通知是否真的安排好了,我尝试使用代码

NSLog(@"notification appdelegate %@", application.scheduledLocalNotifications );

在函数中

- (void)applicationWillResignActive:(UIApplication *)application 

Appdelegate.m 在这种情况下,计划的本地通知数组不为空,NSLog 函数打印正确的通知。这只发生在真实设备上,在模拟器中我的应用程序运行良好。问题不在于检查用户安排本地通知的权限,因为我已经遇到过。有人可以帮助我吗?有什么想法吗?

【问题讨论】:

    标签: ios objective-c notifications ios8 uilocalnotification


    【解决方案1】:

    这不是 iOS 8 的问题,而是

    [UIApplication sharedApplication].scheduledLocalNotifications
    

    问题。我找到的最相关的答案here

    现在有类似的问题。我的猜测是iOS没有 立即安排通知,但仅在结束时 当前运行循环。我在设置时遇到了这些问题 scheduleLocalNotifications 属性在同一运行中多次 循环和更改似乎没有相应更新。我想我会 自己保留一份本地通知数组的副本,只设置 scheduleLocalNotifications 并且从不阅读它。

    【讨论】:

    • 大多数人不会在完全相同的运行循环中添加通知并要求返回。我只是不断地得到一个空数组,无论我什么时候要求它,通知都会触发。
    【解决方案2】:

    对于 iOS8 :

    从现在起 30 秒后触发本地通知。

    - 用于目标 c

        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.alertAction = @"Testing notifications on iOS8";
        localNotification.alertBody = @"Woww it works!!";
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow: 30];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    

    - 斯威夫特

        var localNotification:UILocalNotification = UILocalNotification()
        localNotification.alertAction = "Testing notifications on iOS8"
        localNotification.alertBody = "Woww it works!! 
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 30)
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    

    注册通知设置

    - 用于目标 c

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]
    
            return YES;
        }
    

    - 斯威夫特

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool 
        {
            application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound|UIUserNotificationType.Alert |UIUserNotificationType.Badge, categories: nil)   
    
            return true
        }
    

    现在您也可以收到本地通知了。

    【讨论】:

    • 这没有回答问题。这解释了如何注册通知,问题显然是询问查询已注册的通知。
    • 它没有回答问题,但它帮助我弄清楚我忘记要求用户接受通知;)
    • 不回答问题。如果读者正在寻找所提问题的答案,那是在浪费读者的时间。
    • 兄弟,你拯救了我的一天
    • 不是问题的答案
    【解决方案3】:

    一旦在给定日期触发本地通知,它就不会在 [UIApplication sharedApplication].scheduledLocalNotifications 数组中显示详细信息。因此,如果您为 fireDate 提供一些间隔计数(从 CurrentDate 到 10~20 秒),它将向您显示详细信息。

    【讨论】:

      【解决方案4】:

      我也面临同样的问题,最后我得到了解决它的原因。

      如果您没有在通知对象中设置 fireDate 。它的工作很完美,但是当您尝试获取通知列表时,它总是空白。

      这里是没有设置触发日期的通知对象

      <UIConcreteLocalNotification: 0x7fc1637aedb0>{fire date = (null), time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Tuesday, September 5, 2017 at 3:31:44 PM India Standard Time, user info = (null)}
      

      这是带有触发日期的通知对象

      <UIConcreteLocalNotification: 0x7f96715d0ff0>{fire date = Tuesday, September 5, 2017 at 3:35:17 PM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Tuesday, September 5, 2017 at 3:35:17 PM India Standard Time, user info = (null)}
      

      这里是您可以评论开火日期并检查其不同之处的代码

          let localNotification = UILocalNotification()
           **//Comment ..firedate and test it**
          localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
          localNotification.alertBody = "new Blog Posted at iOScreator.com"
          localNotification.timeZone = NSTimeZone.defaultTimeZone()
          localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
           UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
      
          print(localNotification)
      
         for notification in   UIApplication.sharedApplication().scheduledLocalNotifications! as [UILocalNotification] {
      
              print(notification)
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-22
        • 2023-03-23
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多