如何从 iOS14 小部件安排本地通知?

Posted

技术标签:

【中文标题】如何从 iOS14 小部件安排本地通知?【英文标题】:How to schedule local notification from iOS14 widget? 【发布时间】:2021-01-04 17:48:41 【问题描述】:

我想从 ios14 小部件安排本地通知。

我该怎么做?

我尝试实现以下内容:

    let center = UNUserNotificationCenter.current()
    center.delegate = self

    let content = UNMutableNotificationContent()
    content.title = "Some Title"
    content.body = "Some Body"

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)

    let request = UNNotificationRequest(identifier: UUID().uuidString,
                content: content, trigger: trigger)

    center.add(request)  (error) in
       if error != nil 
          // Handle any errors.
       

【问题讨论】:

你有什么解决办法吗? 很遗憾,我没有 但我昨天做了 【参考方案1】:

我这样做了。

    func createDate(day: Int, month : Int, hour: Int, minute: Int, year: Int)->Date
        var components = DateComponents()
        components.hour = hour
        components.minute = minute
        components.year = year
        components.day = day
        components.month = month
        components.timeZone = .current
        let calendar = Calendar(identifier: .gregorian)
        return calendar.date(from: components)!
    

  //  ?///CreateNitification
    func scheduleNotification(at date: Date, identifierUnic : String, body: String, titles:String) 
        let triggerWeekly = Calendar.current.dateComponents([.day, .month, .hour,.minute, .year], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
        let content = UNMutableNotificationContent()
        content.title = titles
        content.body = body
        content.sound = UNNotificationSound.default
        content.categoryIdentifier = "todoList2"
        let request = UNNotificationRequest(identifier: identifierUnic, content: content, trigger: trigger)
        
      //  UNUserNotificationCenter.current().delegate = self
        /// UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["textNotification2"])
        /// UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) (error) in
            if let error = error 
                print(" We had an error: \(error)")
            
    

// Call 
func callNotifications()
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    let adte =  createDate(day: 3, month: 6, hour: 16, minute: 51, year: 2021)
    scheduleNotification(at: adte, identifierUnic: UUID().uuidString, body: "Widgets on Work", titles: "check 123")

 func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) 
        callNotifications()
        var entries: [SimpleEntry] = []
        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate)
            entries.append(entry)
        
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    
 

通知工作正常 但有一件事待定,我怎样才能调用 UNUserNotificationCenter.current().delegate = self? 谢谢

【讨论】:

以上是关于如何从 iOS14 小部件安排本地通知?的主要内容,如果未能解决你的问题,请参考以下文章

如果在 iOS 设置中禁用通知访问时安排本地通知,则不会在 iOS 13 上触发本地通知

如何安排本地通知

安排本地通知的数量

iOS - 安排多个本地通知,UNCalenderNotificationTrigger

iOS 本地通知 - 在后台时回调

安排iOS本地推送通知