-1

I have a problem with local notifications scheduling (I'm using all available slots - 64). Main problem that it took a lot of time, on slow devices (iPhone 5C) up to 20 seconds ! Here how I'm doing this:

let notificationCenter = UNUserNotificationCenter.current()
for notification in unNotifications { //64 notifications
   notificationCenter.add(notification) { _ in
      //do nothing here
   }
}

I didn't find any bunch method to schedule all notifications with one method call. What could be wrong ?

Never_be
  • 839
  • 2
  • 10
  • 20

1 Answers1

0

Simply follow the steps you will get what you are missing.

Request Notification

// Request Notification Settings
UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
    switch notificationSettings.authorizationStatus {
    case .notDetermined:
        self.requestAuthorization(completionHandler: { (success) in
            guard success else { return }

            // Schedule Local Notification
        })
    case .authorized:
        // Schedule Local Notification
    case .denied:
        print("Application Not Allowed to Display Notifications")
    }
}

Requesting Authorization

// MARK: - Private Methods

private func requestAuthorization(completionHandler: @escaping (_ success: Bool) -> ()) {
// Request Authorization
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in
    if let error = error {
        print("Request Authorization Failed (\(error), \(error.localizedDescription))")
    }

    completionHandler(success)
}
}

Scheduling a Notification

 private func scheduleLocalNotification() {
// Create Notification Content
let notificationContent = UNMutableNotificationContent()

// Configure Notification Content
notificationContent.title = "Cocoacasts"
notificationContent.subtitle = "Local Notifications"
notificationContent.body = "In this tutorial, you learn how to schedule local notifications with the User Notifications framework."

// Add Trigger
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

// Create Notification Request
let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)

// Add Request to User Notification Center
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
    if let error = error {
        print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
    }
}
}

Implementing the Delegate Protocol

 // Configure User Notification Center
UNUserNotificationCenter.current().delegate = self

  extension ViewController: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert])
}

}

While testing result is:

enter image description here

Reference: https://cocoacasts.com/local-notifications-with-the-user-notifications-framework

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52
  • 2
    Sorry, but notifications work for me and I know how to schedule them. Main problem that scheduling 64 notifications took up to 20s. – Never_be Mar 15 '18 at 13:34