好吧,现有的讨论都没有为我提供可行的解决方案。所以这是我显示本地通知的代码。我错过了什么?
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()
最佳答案
我一直在测试,这是我的结果,问题似乎与时间单位 weekOfMonth
和 day
按预期工作有关,这是控制台的图片日志,使用 XCode 8.2,设备 iOS 10.2,仍然有效
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
那么
我认为您可以使用此代码作为解决方法,它不是最佳解决方案,但也许可以帮助您实现所需
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/