ios - UILocalNotifications 未按计划触发

标签 ios swift

好吧,现有的讨论都没有为我提供可行的解决方案。所以这是我显示本地通知的代码。我错过了什么?

let notification = UILocalNotification()
notification.alertBody = "Reminder" // text that will be displayed in the notification
notification.alertAction = "open"
notification.applicationIconBadgeNumber = 1
notification.soundName = UILocalNotificationDefaultSoundName


notification.fireDate = NSDate(timeIntervalSinceNow: 5)
print("Notification scheduled")

notification.userInfo = ["title": "Test", "UUID": "1"]
notification.repeatInterval = NSCalendarUnit.WeekOfMonth

UIApplication.sharedApplication().scheduleLocalNotification(notification)

我知道当应用程序处于前台时,didReceiveLocalNotification 中应该有一个事件。我没有在 appdelegate 的 didReceiveLocalNotification 或通知中收到事件。但是,当我将 presentLocalNotificationNow 与通知一起使用时 - 我会调用应用程序委托(delegate)中的 didReceiveLocalNotification。我也尝试过其他 fireDates,但它不起作用。我错过了什么?

哦,我的 appdelegate didfinishlaunchingapplication 中确实有以下代码

让应用程序 = UIApplication.sharedApplication()

let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

最佳答案

我一直在测试,这是我的结果,问题似乎与时间单位 weekOfMonthday 按预期工作有关,这是控制台的图片日志,使用 XCode 8.2,设备 iOS 10.2,仍然有效

enter image description here

func scheduleTestNotification()
{
    let notification = UILocalNotification()
    notification.alertBody = "Reminder" // text that will be displayed in the notification
    notification.alertAction = "open"
    notification.applicationIconBadgeNumber = 1
    notification.soundName = UILocalNotificationDefaultSoundName

    notification.timeZone = NSTimeZone.default
    notification.fireDate = Date(timeIntervalSinceNow: 5)
    print("Notification scheduled")

    notification.userInfo = ["title": "Test", "UUID": "1"]
    notification.repeatInterval = NSCalendar.Unit.day
    UIApplication.shared.scheduleLocalNotification(notification)
    debugPrint(UIApplication.shared.scheduledLocalNotifications!)
}

但是如果我使用 weekOfMonth 那么

enter image description here

我认为您可以使用此代码作为解决方法,它不是最佳解决方案,但也许可以帮助您实现所需

func scheduleTestNotification()
    {
        let notification = UILocalNotification()
        notification.alertBody = "Reminder" // text that will be displayed in the notification
        notification.alertAction = "open"
        notification.applicationIconBadgeNumber = 1
        notification.soundName = UILocalNotificationDefaultSoundName

        notification.timeZone = NSTimeZone.default
        notification.fireDate = Date(timeIntervalSinceNow: 5)
        print("Notification scheduled")

        notification.userInfo = ["title": "Test", "UUID": "1"]

        UIApplication.shared.scheduleLocalNotification(notification)
        notification.repeatInterval = NSCalendar.Unit.day
        notification.fireDate = Calendar.current.date(byAdding: .day, value: 7, to: notification.fireDate!)
        UIApplication.shared.scheduleLocalNotification(notification)
        debugPrint(UIApplication.shared.scheduledLocalNotifications!)
    }

希望对你有帮助

关于ios - UILocalNotifications 未按计划触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499298/

相关文章:

ios - 在 View Controller 之间传输变量而不会出错

ios - 无法使用 PureLayout 设置 ScrollView

ios - Xcode 6 创建新的 Objective-C 类

iOS:-dynamic 未指定以下标志无效:-sectcreate

iphone - NSUserDefaults + NSMutableArray -> 存储2个数组并显示在tableview中

ios - 委托(delegate)给正确的同名方法

swift - 以编程方式触发展开 segue

Swift - tableView.reloadData() 不工作,即使它看起来在正确的线程中

ios - Alamofire 4.0 上传时的模糊引用

ios - Swift语言中如何定义char数组