为特定日期和时间设置本地通知 swift 3
Set local notifications for particular days and time swift 3
在我的应用程序中,我想添加本地通知。该场景将是用户可以 select 从周一到周日的任何时间和日期。例如,如果用户 select 的星期一、星期四和星期六作为日期和时间作为 11:00 下午,那么现在应该在 select 的所有日期和特定时间通知用户.
代码:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
// let test = UNCalendarNotificationTrigger()
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
我正在使用此代码,但它无法满足我的需要。
要获取在特定工作日的特定时间重复的本地通知,您可以使用 UNCalendarNotificationTrigger
:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
// add notification for Mondays at 11:00 a.m.
var dateComponents = DateComponents()
dateComponents.weekday = 2
dateComponents.hour = 11
dateComponents.minute = 0
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
如果您想在周一、周四和周六在 11:00 收到通知,您需要添加 3 个单独的请求。为了能够删除它们,您必须跟踪标识符。
在我的应用程序中,我想添加本地通知。该场景将是用户可以 select 从周一到周日的任何时间和日期。例如,如果用户 select 的星期一、星期四和星期六作为日期和时间作为 11:00 下午,那么现在应该在 select 的所有日期和特定时间通知用户.
代码:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
// let test = UNCalendarNotificationTrigger()
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
我正在使用此代码,但它无法满足我的需要。
要获取在特定工作日的特定时间重复的本地通知,您可以使用 UNCalendarNotificationTrigger
:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
// add notification for Mondays at 11:00 a.m.
var dateComponents = DateComponents()
dateComponents.weekday = 2
dateComponents.hour = 11
dateComponents.minute = 0
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
如果您想在周一、周四和周六在 11:00 收到通知,您需要添加 3 个单独的请求。为了能够删除它们,您必须跟踪标识符。