ios - 带有 fireDate 的通知会立即触发

标签 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)

最佳答案

您正在调用 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")
    }

}

您还需要确保您已在 AppDelegate 中使用新的 UserNotifications 正确注册通知

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
}

关于ios - 带有 fireDate 的通知会立即触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42332418/

相关文章:

ios - 收到通知时播放声音在开发中有效但在生产中无效

iphone - Storyboard自定义表标题

ios - 谷歌地图 iOS SDK : optimizing finding a random street view

ios - 手势识别器代码在离开和回来查看后被调用两次

ios - 尝试执行 UIAlert 或 PerformSegueWithIdentifier 时,以 NSException 类型的未捕获异常终止

ios - 如何在ios上获取所有收到的通知

ios - CLLocationManager EXC_BAD_ACCESS 错误

ios - Xamarin 调试无法正常工作

ios - 访问了 Xcode 按钮文本,但未反射(reflect)背景更改 UI

android - 在收到通知时显示警报或 View