iOS 8 [UIApplication sharedApplication] .scheduledLocalNotifications为空

时间:2014-09-20 11:00:03

标签: ios objective-c notifications ios8 uilocalnotification

我正在尝试将我的应用更新到iOS 8.在一个功能中,我以这种方式安排本地通知(我已经检查过该通知并且通知的所有其他部分都是正确的): / p>

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

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

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

但数组

[UIApplication sharedApplication].scheduledLocalNotifications 
即使未触发通知,

也为空。 然后,为了检查本地通知是否真的被安排,我尝试使用代码

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

中的函数

- (void)applicationWillResignActive:(UIApplication *)application 
在这种情况下,Appdelegate.m

,计划的本地通知的数组不为空,NSLog函数打印正确的通知。这只发生在真实设备上,在模拟器中我的应用程序工作正常。问题不在于检查用户安排本地通知的权限,因为我已经面对过它。有人能帮助我吗?一些想法?

4 个答案:

答案 0 :(得分:12)

这不是iOS 8问题,而是

[UIApplication sharedApplication].scheduledLocalNotifications

问题。我发现最相关的答案here

  

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

答案 1 :(得分:3)

对于iOS8:

从现在起30秒后拨打本地通知。

- 对于Objective 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];

- 对于Swift

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

注册通知设置

- 对于Objective c

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

        return YES;
    }

- 对于Swift

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

        return true
    }

现在您也可以获得本地通知。

答案 2 :(得分:0)

在指定日期触发本地通知后,它不会在 [UIApplication sharedApplication] .scheduledLocalNotifications 数组中显示详细信息。因此,如果你给fireDate一些间隔计数(从CurrentDate到10~20s),它会显示详细信息。

答案 3 :(得分:0)

我面临同样的问题,最后我得到了解决方案,为什么会发生这种情况。

如果未在通知对象中设置 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)

    }