Ios 立即通知fireDate火灾
我收到以下通知,该通知被设置为在当天晚上9点(如果当前在晚上9点之前)或第二天晚上9点(如果已经超过晚上9点)开火 看起来一切正常,但无论当前时间如何,它总是立即显示通知Ios 立即通知fireDate火灾,ios,swift,notifications,Ios,Swift,Notifications,我收到以下通知,该通知被设置为在当天晚上9点(如果当前在晚上9点之前)或第二天晚上9点(如果已经超过晚上9点)开火 看起来一切正常,但无论当前时间如何,它总是立即显示通知 let hour = 21 let minute = 0 let calendar = NSCalendar(identifier: .gregorian)!; var dateFire = Date()
let hour = 21
let minute = 0
let calendar = NSCalendar(identifier: .gregorian)!;
var dateFire = Date()
// if today's date is passed, use tomorrow
var fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire)
if (fireComponents.hour! > hour
|| (fireComponents.hour == hour && fireComponents.minute! >= minute) ) {
dateFire = dateFire.addingTimeInterval(86400) // Use tomorrow's date
fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire);
}
// set up the time
fireComponents.hour = hour
fireComponents.minute = minute
// schedule local notification
dateFire = calendar.date(from: fireComponents)!
print(dateFire)
let notification = UILocalNotification()
notification.alertBody = "TEST"
notification.soundName = "Default"
notification.fireDate = dateFire
UIApplication.shared.presentLocalNotificationNow(notification)
您正在调用
UIApplication.shared.presentLocalNotificationNow
,它完全按照它所说的做,它在调用方法时正确地显示通知
如果要安排本地通知,需要首先导入UserNotifications
(使用iOS 10+)
然后您可以按如下方式安排通知:
func registerLocalNotification(date: Date) {
let content = UNMutableNotificationContent()
content.categoryIdentifier = "YOUR_IDENTIFIER"
content.title = "YOUR_TITLE"
content.body = "NOTIFICATION_BODY"
content.sound = UNNotificationSound.default()
// Use date components to create a trigger time
let triggerDate = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: date)
print("Register: \(triggerDate)")
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
// Instantiate the notification request
let request = UNNotificationRequest(identifier: "SOME_IDENTIFIER", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
// Handle error if necessary
print("Notification Added")
}
}
您还需要确保已使用AppDelegate
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if granted {
UIApplication.shared.registerForRemoteNotifications()
}
}
return true
}
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if granted {
UIApplication.shared.registerForRemoteNotifications()
}
}
return true
}