代码之家  ›  专栏  ›  技术社区  ›  Sonali Pawar

设置特定日期和时间的本地通知swift 3

  •  3
  • Sonali Pawar  · 技术社区  · 7 年前

    代码:

     let notification = UNMutableNotificationContent()
     notification.title = "Danger Will Robinson"
     notification.subtitle = "Something This Way Comes"
     notification.body = "I need to tell you something, but first read this."
    
     let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
     // let test = UNCalendarNotificationTrigger()
     let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
     UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    

    1 回复  |  直到 7 年前
        1
  •  8
  •   joern    7 年前

    要获取在某个工作日的某个时间重复的本地通知,您可以使用 UNCalendarNotificationTrigger :

    let notification = UNMutableNotificationContent()
    notification.title = "Danger Will Robinson"
    notification.subtitle = "Something This Way Comes"
    notification.body = "I need to tell you something, but first read this."
    
    // add notification for Mondays at 11:00 a.m.
    var dateComponents = DateComponents()
    dateComponents.weekday = 2
    dateComponents.hour = 11
    dateComponents.minute = 0
    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    
    let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)