2017-02-19 11 views
0

私は、その日(9PM以前)または9PM(9PMがすでに過ぎている場合)のいずれかで9pmに火をつけるように設定されています。fireDateの直後の通知

すべてが正しく表示されているように見えますが、現在の時間に関係なく常に通知が表示されます。あなたはそれが言うまさにませUIApplication.shared.presentLocalNotificationNowを呼んでいる

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) 

答えて

0

は、それはメソッドが呼び出された権利としての通知を提示します。

ローカル通知をスケジュールする場合、あなたは(10+のiOSとの)最初のインポートUserNotificationsに必要

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 
} 
関連する問題