[英]How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?
[英]How to handle UserNotifications Actions in iOS 10
所以我可以像這樣安排通知;
//iOS 10 Notification
if #available(iOS 10.0, *) {
var displayDate: String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.full
return dateFormatter.string(from: datePicker.date as Date)
}
let notif = UNMutableNotificationContent()
notif.title = "I am a Reminder"
notif.subtitle = "\(displayDate)"
notif.body = "Here's the body of the notification"
notif.sound = UNNotificationSound.default()
notif.categoryIdentifier = "reminderNotification"
let today = NSDate()
let interval = datePicker.date.timeIntervalSince(today as Date)
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)
let request = UNNotificationRequest(identifier: "reminderNotif", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
print(error)
// completion(Success: false)
} else {
//completion(Sucess: true)
}
})
}
我已經在appDelegate
請求了權限,並且使用通知擴展程序的自定義視圖中的通知顯示正常。
我在appDelegate
為通知類別添加了通知操作; 這些也會出現。
//Notifications Actions
private func configureUserNotifications() {
if #available(iOS 10.0, *) {
let tomorrowAction = UNNotificationAction(identifier: "tomorrowReminder", title: "Remind Me Tomorrow", options: [])
let dismissAction = UNNotificationAction(identifier: "dismissReminder", title: "Dismiss", options: [])
let category = UNNotificationCategory(identifier: "reminderNotification", actions: [tomorrowAction, dismissAction], intentIdentifiers: [], options: [.customDismissAction])
UNUserNotificationCenter.current().setNotificationCategories([category])
} else {
// Fallback on earlier versions
}
}
我在通知擴展名.plist
文件中設置了相同的類別。 在通知擴展中,當用戶點擊一個動作時,我有以下內容來更改文本。
//Handle Notification Actions And Update Notification Window
private func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier == "tomorrowReminder" {
print("Tomrrow Button Pressed")
subLabel.text = "Reminder For Tomorrow"
subLabel.textColor = UIColor.blue
done(.dismissAndForwardAction)
}
if response.actionIdentifier == "dismissReminder" {
print("Dismiss Button Pressed")
done(.dismiss)
} else {
print("Else response")
done(.dismissAndForwardAction)
}
}
然而,文本沒有改變,也沒有調用任何語句;
在 appDelegate 中,我有以下內容;
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
configureUserNotifications()
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
private func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
@available(iOS 10.0, *)
private func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
print("Recieved Action For \(response.actionIdentifier)")
if response.actionIdentifier == "tomorrowReminder" {
print("Tomorrow Reminder")
//Set new reminder for tomorrow using the notification content title
completionHandler()
}
if response.actionIdentifier == "dismissReminder" {
print("Dismiss Reminder...")
completionHandler()
}
}
}
這兩個函數實際上都沒有在appDelegate
調用。 我不確定更新擴展視圖的問題是否與應用程序委托有關。 我不這么認為,我一直在關注Apple的WWDC視頻以及其他教程並查看文檔API並且無法弄清楚;
PS:過去幾周我一直在研究並試圖解決這個問題,這似乎相當簡單,我不確定我錯過了什么。 我知道我不是唯一有這些問題的人。
我還沒有檢查你的整個代碼,但至少,這些函數頭需要更改如下:
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
func didReceive(_ response: UNNotificationResponse,
completionHandler done: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
簡單規則:刪除private
,添加@escaping
。
您可能從 Xcode 收到了錯誤的建議,但是通過將它們設為private
,不會生成 Objective-C 入口點。 iOS 運行時在內部使用 Objective-C 選擇器,因此它找不到您的方法,因此不會執行它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.