xcode iOS Swift 本地通知未“弹出”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33691739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Swift Local notification not "popping up"
提问by Chan Jing Hong
I'm trying to send a local notification at scheduled time. But the notifications does not appear on the screen, but it is showing in the notification center when I swipe down.
This is what I'm trying to achieve This is what I getting.
我正在尝试在预定时间发送本地通知。但是通知没有出现在屏幕上,但是当我向下滑动时它会显示在通知中心。
这就是我要实现的目标 这就是我得到的。
This code is from my AppDelegate's didFinishLaunchingWithOptions().
此代码来自我的 AppDelegate 的 didFinishLaunchingWithOptions()。
// Setup local push notifications
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil))
scheduleNotifications()
And this is the code for scheduleNotifications()
这是 scheduleNotifications() 的代码
func scheduleNotifications() {
// create a corresponding local notification
let notification = UILocalNotification()
// Get today's date, time and year
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year], fromDate: NSDate())
// Sets the fire time to 2pm/1400 hours to anticipate user for lunch time
components.hour = 19
components.minute = 13
components.second = 00
notification.fireDate = components.date // Sets the fire date
notification.alertBody = "Enjoyed your lunch? Don't forget to track your expenses!"
notification.alertAction = "Add expense"
notification.repeatInterval = NSCalendarUnit.Day // Repeats the notifications daily
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
Any help would be appreciated. Thanks!
任何帮助,将不胜感激。谢谢!
采纳答案by JoGoFo
Your issue is the way you are converting the NSDateComponents object to an NSDate.
您的问题是您将 NSDateComponents 对象转换为 NSDate 的方式。
Simply calling components.date
without setting a calendar
for the NSDateComponents
object will return nil
.
简单地调用components.date
而不calendar
为NSDateComponents
对象设置 a将返回nil
。
Try using this instead:
尝试改用这个:
notification.fireDate = calendar.dateFromComponents(components)
Alternatively, you can set the calendar
property on the components object:
或者,您可以calendar
在组件对象上设置属性:
components.calendar = calendar
Because you are setting the fireDate
property on the notification object to nil
, it will fire immediately, i.e. before you have a chance to close the app and lock the screen. This behavour is documented in the UILocalNotification class reference
因为您fireDate
将通知对象上的属性设置为nil
,它会立即触发,即在您有机会关闭应用程序并锁定屏幕之前。这种行为记录在UILocalNotification 类参考中
回答by insu
I get the same odd behavior. Just created a new project to test your code.
我得到了同样奇怪的行为。刚刚创建了一个新项目来测试您的代码。
What i noticed when you change your fireDate to:
当您将 fireDate 更改为:
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
You will get your wanted behavior.
你会得到你想要的行为。
Hope that helps a little!
希望有点帮助!