ios - 使用本地通知 api 在 swift 3 中触发每周通知

标签 ios iphone swift swift3 uilocalnotification

我想触发每周一晚上 7:00 发出的每周通知。

我看到这样的代码:

let triggerWeekly = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
  1. 我应该将开始日期设置为什么?
  2. 此外,日期是否需要在现在?

我正在使用 iOS 10+ 和 swift 3。

最佳答案

传递要生成本地通知的日期

            let strDate = (data as AnyObject).value(forKey: "not_date") as? String
            let dateformatter = DateFormatter()
            dateformatter.dateFormat = "yyyy-MM-dd hh:mm a"
            let notidate = dateformatter.date(from: strDate!)

需要先stattnotificationdate like = 2018-06-25 07:00 PM 之后需要加上时间间隔

var dateStart = Date() 
dateStart = dateStart.addingTimeInterval(TimeInterval(604800))

并通过这些interwal生成通知

//MARK:- Schedule Notification with Daily bases.
        func scheduleNotification(at date: Date, body: String, titles:String) {
            let triggerDaily = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)

            if #available(iOS 10.0, *) {
                let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
                let content = UNMutableNotificationContent()
                content.title = titles
                content.body = body
                content.sound = UNNotificationSound.default()
                content.categoryIdentifier = "HardikBar"

                let request = UNNotificationRequest(identifier: "NotificationAt-\(date))", content: content, trigger: trigger)
                UNUserNotificationCenter.current().add(request) {(error) in
                    if let error = error {
                        print("Uh oh! We had an error: \(error)")
                    }
                }
            } else {
                // Fallback on earlier versions
            }

        }

关于ios - 使用本地通知 api 在 swift 3 中触发每周通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51054772/

相关文章:

iphone - iOS SDK写在UIImage上绘制文本

ios - 如何只清除缓存目录中的图像?

swift - 在 SwiftUI View 中创建一个拖动 handle (用于可拖动窗口等)

iphone - iCarousel 从 NSMutableArray 加载 UIViews

ios - 应用程序未使用 Typhoon 进入初始 ViewController

iphone - 调试 Objective-C 和 Xcode 中抛出的异常

目前无法安装 iPhone 应用程序

ios - 如何获取保存的字符串并在 swift 中使用另一个 View Controller ?

ios - Xcode 6 Beta 6 上的 iPhone 5c 配置

ios - RESTKIT解析CoreData关系错误如何解决?