【问题标题】:swift create user notification快速创建用户通知
【发布时间】:2017-01-23 06:04:37
【问题描述】:

我想创建一个在应用运行时将显示在通知中心的 userNotification。 单击通知将终止应用程序。

需要使用哪个触发器? 如何实现此功能?

【问题讨论】:

    标签: ios swift3 usernotifications unusernotificationcenter


    【解决方案1】:

    试试这个。

    func scheduleNotification(at date: Date, body: String) {
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar.dateComponents(in: .current, from: date)
        let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
    
        let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
    
        let content = UNMutableNotificationContent()
        content.title = "Dont Forget"
        content.body = body
        content.sound = UNNotificationSound.default()
    
        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
    
        UNUserNotificationCenter.current().delegate = self
        //UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
          if let error = error {
            print("Uh oh! We had an error: \(error)")
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-11
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多