OGeek|极客世界-中国程序员成长平台

标题: ios - 一次创建多个本地通知失败 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:34
标题: ios - 一次创建多个本地通知失败

我正在处理 iOS 上的本地通知,但是当 iOS 尝试创建通知时我遇到了一些问题。其中有些是已创建的,有些则不会。

在大量查看我的代码后,我发现它在创建本地通知时失败了。

UIApplication.sharedApplication().scheduleLocalNotification(notification)

我能想到的唯一原因是在循环中创建通知,大约 50-60 个通知,iOS 无法处理太多。我这样做是因为所有通知都有不同的时间和不同的日期,属于不同的事物。

这是我创建本地通知的 block :

    let createdUid = self.generateNotificationUUID()

    // create a corresponding local notification
    let notification = UILocalNotification()

    /* Time and timezone settings */
    notification.fireDate = self.buildTime()
    notification.repeatInterval = NSCalendarUnit.WeekOfYear
    notification.timeZone = NSTimeZone.systemTimeZone()

    /* Information settings */
    notification.alertBody = "Sector \(notificationData["sector"]!): located at \(notificationData["name"]!) closes in 15 min."
    notification.alertAction = "Open"

    /* Badge settings */
    notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    notification.soundName = "ring.caf"
    notification.userInfo = ["UUID": createdUid, ]

    /* Schedule the notification */       
UIApplication.sharedApplication().scheduleLocalNotification(notification)

所有这些代码都在一个循环中。调用通知创建之前的相同循环,动态构建 notificationData 数组。

通知数据数组包含扇区、名称、时间和日期。

时间和日期用于计算通知触发日期。 Sector 和 Name 用于警报正文。 并且这四个值都用于生成 UID(UUID)。

如果我打印并删除 UIApplication.sharedApplication()... 所有数据看起来都不错,而且它需要是什么。

我已经尝试过解决问题,使用

dispatch_async(dispatch_get_main_queue()) {...}

dispatch_sync(dispatch_get_main_queue()) {...}

但是使用 async 我收到了与没有它时相同的结果,并且使用 sync 我的屏幕卡住了。

我有点迷失了,我确定我的问题是因为 iOS 没有及时处理所有通知的创建,但我不知道如何解决它。

希望有人能帮助我,我正在使用 XCode 7.3.1Swift 2.2



Best Answer-推荐答案


来自文档:https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/

An app can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest.

关于ios - 一次创建多个本地通知失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802241/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4