gpt4 book ai didi

swift - 未收到来自 LocalNotification.fireDate 的通知

转载 作者:行者123 更新时间:2023-11-28 09:16:24 25 4
gpt4 key购买 nike

为什么按下按钮后不显示通知?

如果 fireDate 是 NSDate(timeIntervalSinceNow: 10) 它会工作正常

如何从数组中获取通知?

    @IBAction func buttonPressed(sender: AnyObject) {

var stringDate = dateFormatter.stringFromDate(timePicker.date)
alarmArray.append(stringDate)

let fixedAlarmArray = alarmArray
NSUserDefaults.standardUserDefaults().setObject(fixedAlarmArray, forKey: "alarmArray")
NSUserDefaults.standardUserDefaults().synchronize()


//Local Notification
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Go to app"
localNotification.alertBody = "Its Time!"
localNotification.fireDate = timePicker.date
localNotification.timeZone = NSTimeZone.defaultTimeZone()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

self.view.endEditing(true)

}

最佳答案

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

localNotification.fireDate = timePicker.date
localNotification.timeZone = NSTimeZone.defaultTimeZone()
// the problem is here, you are setting the timezone after inputing your date

你应该使用 localtimeZone 并在设置 fireDate 之前设置它

localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.fireDate = timePicker.date

注意:

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.

这样做:

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))

创建一个扩展,将选择的时间与 0 秒结合起来,如下所示:

extension NSDate {

var day: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay, fromDate: self).day }
var month: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth, fromDate: self).month }
var year: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: self).year }
var minute: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMinute, fromDate: self).minute }
var hour: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitHour, fromDate: self).hour }

var fireDate: NSDate {
return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: hour, minute: minute, second: 0, nanosecond: 0)!
}
}

然后按如下方式使用它:

localNotification.fireDate = timePicker.date.fireDate

关于swift - 未收到来自 LocalNotification.fireDate 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27407245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com