UNNotificationTrigger about fireDate

2.4k views Asked by At

In iOS 10 I use new frameWork UNNotification .When setting UNNotificationTrigger I want to set a fire Date on UNNotification. But I can't find set a fire Date in UNNotificationTrigger's properties. So How can i set a fire Date in UNNotificationTrigger. I want set a fire Date in UNNotificationTrigger, how can i set

1

There are 1 answers

1
Devesh Laungani On

There are two types of triggers in User Notifications framework:

  1. UNTimeIntervalNotificationTrigger - Used for creating a notification with a set interval time:

    Fire in 30 minutes (60 seconds times 30) ,

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (30*60), repeats: false)
    
  2. UNCalendarNotificationTrigger- Used for creating a notification at a certain date as in your case and for repeating with specific criteria:

    let date = DateComponents()
    date.hour = 8
    date.minute = 30 
    let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
    

The solution (Swift 3) for you would be to convert your date to a dateComponent and this can be done by:

import UserNotifications

var newComponents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: date)

let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)