So, I can schedule such notifications:
//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 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) } }) }
I requested permission in appDelegate and notifications are displayed in order with my custom view using the notification extension.
I added notification actions to appDelegate for the notification category; they also appear.
//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 } }
I have the same set of categories in the .plist extension .plist . And in the notification extension, I have the following to change the text when the user clicks on the action.
//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) } }
However, the text does not change and no operator is called;
In appDelegate app, I have the following:
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() } } }
None of these functions are actually called in appDelegate . I am not sure if the problem with updating the extension view is with the application delegate. I don’t think so, I followed the Apple WWDC video, as well as other tutorials and look at the document API and can’t understand
- Why are text notifications extension tags not updated?
- Why are appDelegate functions not called?
- How can I use the notification content in an application delegate to use for an action?
PS: I spent the last few weeks researching and trying to figure it out, it looked pretty straightforward, and I'm not sure what I am missing. I know that I am not the only one who has these problems.