Swift 3 个本地通知未触发
Swift 3 Local notifications not firing
我有以下设置,根本没有触发任何通知。
基于堆栈上的其他类似问题,我为每个请求添加了一个唯一标识符,并且还将正文添加到内容中。
我有这个功能需要用户的许可。
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)
}
}
这些是主要功能:
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)")
}
}
}
此功能是在应用打开时收到通知
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
}
也没有错误哈哈!
编辑:
因此,在用户界面中,我选择了 2350 以及周六和周日。我希望周六和周日都在 2350 发送通知。
我做到了print(daysInt)
。它的第一个值为 1,第二个值为 7,这与预期的一样。其次,我进入函数 scheduleActualNotification
,小时的值为 23,分钟为 50,正如预期的那样。
谢谢!
经过数周的挫折,这就是解决方案:
将 dateComponents.day = day
替换为 dateComponents.weekday = day
。 .day
是一个月中的第几天,而 .weekday
是星期几!
也不要使用 dateComponents.year = 2017
,因为这会导致通知无法触发。
通知现在工作得很好!!
日历单位标志可以帮助您:
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)
}
我有以下设置,根本没有触发任何通知。
基于堆栈上的其他类似问题,我为每个请求添加了一个唯一标识符,并且还将正文添加到内容中。
我有这个功能需要用户的许可。
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)
}
}
这些是主要功能:
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)")
}
}
}
此功能是在应用打开时收到通知
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
}
也没有错误哈哈!
编辑:
因此,在用户界面中,我选择了 2350 以及周六和周日。我希望周六和周日都在 2350 发送通知。
我做到了print(daysInt)
。它的第一个值为 1,第二个值为 7,这与预期的一样。其次,我进入函数 scheduleActualNotification
,小时的值为 23,分钟为 50,正如预期的那样。
谢谢!
经过数周的挫折,这就是解决方案:
将 dateComponents.day = day
替换为 dateComponents.weekday = day
。 .day
是一个月中的第几天,而 .weekday
是星期几!
也不要使用 dateComponents.year = 2017
,因为这会导致通知无法触发。
通知现在工作得很好!!
日历单位标志可以帮助您:
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)
}