2017-09-07 87 views
1

在我的應用程序中,我想添加本地通知。該場景將是用戶可以選擇從Mon到Sun的任何時間和日期。例如,如果用戶選擇星期一,星期四和星期六爲下午11點的日期和時間,那麼現在應在所有選定日期和特定時間通知用戶。迅速設置特定日期和時間的本地通知3

代碼:

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) 

我使用此代碼,但這並不根據我所需要的工作。

+0

您在此代碼中的timeInterval是60秒。所以,它會在60秒後開火。你沒有實施任何在多天內觸發它的邏輯? –

+0

類似於https://stackoverflow.com/questions/41800189/local-notifications-repeat-interval-in-swift-3看起來很有趣 – MadProgrammer

回答

4

要獲得在某個時間在某個工作日的重複本地通知您可以使用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分開請求。爲了能夠刪除它們,你必須跟蹤標識符。