[英]Swift 3 Local notifications not firing
I have the following setup and no notification are firing at all. 我有以下设置,根本没有通知。
Based on other similar questions on the stack, I've put in a unique identifier for each request and I've also added the body to the content. 根据堆栈上的其他类似问题,我为每个请求添加了一个唯一的标识符,并且我还将内容添加到内容中。
I've got this function which requests permission from user. 我有这个请求用户许可的功能。
func sendIntNotifications()
{
//1. Request permission from the user
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler:
{
(granted, error) in
if granted
{
print ("Notification access granted")
}
else
{
print(error?.localizedDescription)
}
UNUserNotificationCenter.current().delegate = self
})
// This function has a lot of other code that then calls this function to set multiple notifications :
for daysInt in outputWeekdays
{
scheduleActualNotification(hourInt: hourInt, minuteInt: minuteInt, dayInt: daysInt)
}
}
And these are the main function : 这些是主要功能:
func scheduleActualNotification(hourInt hour: Int, minuteInt minute: Int, dayInt day: Int)
{
// 3.1 create date components for each day
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.day = day
//3.2 Create a calendar trigger for that day at that time
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
repeats: true)
//3.3 Message content
let content = UNMutableNotificationContent()
content.title = "Test Title"
content.body = "Test body"
content.badge = 1
// 3.4 Create the actual notification
let request = UNNotificationRequest(identifier: "bob \(i+1)",
content: content,
trigger: trigger)
// 3.5 Add our notification to the notification center
UNUserNotificationCenter.current().add(request)
{
(error) in
if let error = error
{
print("Uh oh! We had an error: \(error)")
}
}
}
This function is to receive the notification when this app is open 此功能用于在此应用程序打开时接收通知
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .sound]) //calls the completionHandler indicating that both the alert and the sound is to be presented to the user
}
There are no errors either lol! 大声笑没有错误!
Edit : 编辑:
So from the user interface, I selected 2350 and Saturday and Sunday. 所以从用户界面,我选择了2350以及周六和周日。 I expect the notification to be sent at 2350 on both Saturday and Sunday.
我希望通知在周六和周日2350发送。
I did print(daysInt)
. 我做了
print(daysInt)
。 The first value of it is 1 and the second value of it is 7 which is just as expected. 它的第一个值是1,它的第二个值是7,正如预期的那样。 Secondly, I went into the function
scheduleActualNotification
and the values of hour was 23 and minute was 50, just as expected. 其次,我进入了函数
scheduleActualNotification
,小时的值是23,分钟是50,正如预期的那样。
Thank you! 谢谢!
So after weeks of frustration, this is the solution : 经过数周的挫折,这就是解决方案:
Replace dateComponents.day = day
with dateComponents.weekday = day
. 将
dateComponents.day = day
替换为dateComponents.weekday = day
。 .day
is for day of the month whereas .weekday
is day of the week! .day
是一个月中的.weekday
一天,而.weekday
是一周中的哪一天!
Also do not use dateComponents.year = 2017
as that caused the notifications not to fire. 也不要使用
dateComponents.year = 2017
因为这会导致通知不会触发。
The notifications work perfectly fine now!! 现在通知工作完全正常!!
Calendar Unit Flags can help you : Calendar Unit Flags可以帮助您:
var notificationTriggerCalendar : UNCalendarNotificationTrigger? = nil
if let fireDate = trigger.remindAt {
let unitFlags = Set<Calendar.Component>([.hour, .year, .minute, .day, .month, .second])
var calendar = Calendar.current
calendar.timeZone = .current
let components = calendar.dateComponents(unitFlags, from: fireDate)
let newDate = Calendar.current.date(from: components)
notificationTriggerCalendar = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.