使用fireDate通知会立即开火

时间:2017-02-19 20:21:22

标签: ios swift notifications

我在下一天晚上9点(如果当前在晚上9点之前)或第二天晚上9点(如果已经过了晚上9点)已经设置了以下通知。

似乎所有东西看起来都是正确的,但无论当前时间如何,它都会立即显示通知。

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)

1 个答案:

答案 0 :(得分:0)

您正在调用UIApplication.shared.presentLocalNotificationNow,它完全按照其说法运行,它会在调用方法时显示通知。

如果您想安排本地通知,则需要先导入UserNotifications(使用iOS 10 +)

import UserNotifications

然后您可以安排如下通知:

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")
    }

}

您还需要确保使用UserNotifications

中的新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
}