[英]Swift: Converting Local Notification to User Notification IOS 10
I have recently updated my project to IOS 10, which has caused any local notifications I had registered to not fire.我最近将我的项目更新到 IOS 10,这导致我注册的任何本地通知都没有触发。 I have tried to convert the local notification to user notifications but it is not having the same effect.
我尝试将本地通知转换为用户通知,但效果不同。 Here is the original code I tried to convert.
这是我尝试转换的原始代码。 Within viewDidLoad:
在 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)
This caused a local notification to fire at 11:00 am every saturday.这导致本地通知在每个星期六上午 11:00 触发。
Here is the code I used to try and achieve the same effect in IOS 10:这是我用来尝试在 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")
}
}
}
In viewDidLoad:在 viewDidLoad 中:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
print("Error")
}
}
This code works fine if the user selects the date using a datePicker, but I can't figure out how to programmatically set the date of the notification to the same date I had in the local notification (2017-28-01 11:00), and also how to make the notification fire every week at the same time as there is no repeatInterval property.如果用户使用 datePicker 选择日期,此代码工作正常,但我无法弄清楚如何以编程方式将通知的日期设置为与本地通知中的日期相同(2017-28-01 11:00) ,以及如何在没有 repeatInterval 属性的情况下每周触发通知。
Could you explain how you could only specify for example Monday at 11:00 am
你能解释一下你如何只能指定例如星期一上午 11:00
It's just a matter of leaving out everything that doesn't apply to the repeated value.这只是忽略所有不适用于重复值的问题。 Thus, in your case you want to specify the date components
hour:11
and weekday:2
and nothing else.因此,在您的情况下,您要指定日期组件
hour:11
和weekday:2
而不是别的。
To understand how this works, run this in a playground:要了解这是如何工作的,请在操场上运行它:
let dc = DateComponents(hour:11, weekday:2)
let greg = Calendar(identifier: .gregorian)
let d = greg.nextDate(after: Date(), matching: dc, matchingPolicy:.nextTime)
You will see that d
is this coming Monday at 11AM.您将看到
d
是本周一上午 11 点。
That is exactly what UNNotificationTrigger will do to figure out when to send this notification.这正是 UNNotificationTrigger 用来确定何时发送此通知的方法。
Now, if you also set this to be a repeats
trigger, you have just specified every Monday at 11AM.现在,如果您还将此设置为
repeats
触发器,则您刚刚指定了每周一上午 11 点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.