Swift:将本地通知转换为用户通知IOS 10

Swift: Converting Local Notification to User Notification IOS 10

我最近将我的项目更新为 IOS 10,这导致我注册的所有本地通知都没有触发。我试图将本地通知转换为用户通知,但效果不同。这是我尝试转换的原始代码。在 viewDidLoad:

        guard let settings = UIApplication.shared.currentUserNotificationSettings else { return
        }
        if settings.types == UIUserNotificationType() {
            let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            present(ac, animated: true, completion: nil)
            return
        }


        var dateComp: DateComponents = DateComponents()
        dateComp.year = 2017;
        dateComp.month = 01;
        dateComp.day = 28;
        dateComp.hour = 11;
        dateComp.minute = 0;
        (dateComp as NSDateComponents).timeZone = TimeZone.current

        let calender:Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        let date:Date = calender.date(from: dateComp)!


        let notification = UILocalNotification()
        notification.fireDate = date
        notification.alertBody = "Notification!"
        notification.alertAction = "You just Received a notification!"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.userInfo = ["customField1": "w00t"]
        notification.repeatInterval = NSCalendar.Unit.weekOfYear
        UIApplication.shared.scheduleLocalNotification(notification)

这导致本地通知在每个星期六上午 11:00 触发。

这是我用来尝试在 IOS 10 中实现相同效果的代码:

func scheduleNotification(at date: Date) {
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: true)

let content = UNMutableNotificationContent()
content.title = "Notification"
content.body = "You just Received a notification!"
content.sound = UNNotificationSound.default()

let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)


UNUserNotificationCenter.current().add(request) {(error) in
    if let error = error {
        print("Error")
    }
}
}

在 viewDidLoad 中:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
    if !accepted {
        print("Error")
    }
}

如果用户使用 datePicker 选择日期,此代码可以正常工作,但我无法弄清楚如何以编程方式将通知日期设置为我在本地通知中的相同日期 (2017-28- 01 11:00),以及如何在没有 repeatInterval 属性.

的情况下每周同时触发通知

Could you explain how you could only specify for example Monday at 11:00 am

只需删除不适用于重复值的所有内容即可。因此,在您的情况下,您想要指定日期组件 hour:11weekday:2,而不是其他任何内容。

要了解这是如何工作的,运行在操场上:

let dc = DateComponents(hour:11, weekday:2)
let greg = Calendar(identifier: .gregorian)
let d = greg.nextDate(after: Date(), matching: dc, matchingPolicy:.nextTime)

您会看到 d 是周一上午 11 点。

这正是 UNNotificationTrigger 确定何时发送此通知所要做的。

现在,如果您还将此设置为 repeats 触发器,则您刚刚指定 每个 星期一上午 11 点。