Objective D Objective D - 1 year ago 123
iOS Question

LGSideMenuController: Registration of several local push notifications leads to delay in screen handling

I noticed a strange issue when I registered several local push notifications:

for model in myModelArray
{
let calendar = Calendar.current
let scheduleDate = calendar.date(byAdding: .second, value: i * 5, to: Date())!

let notification = UILocalNotification()
notification.fireDate = scheduleDate
notification.alertBody = "My push notification title"

UIApplication.shared.scheduleLocalNotification(notification)
i += 1
}


When my local push notifications appear in background mode, I tap on it and want to present one screen directly:

func application(_ application: UIApplication, didReceive notification: UILocalNotification)
{
let mainMenuVC = self.window?.rootViewController as! MenuViewController
let navController = mainMenuVC.sideMenuController?.rootViewController as! UINavigationController

let myDestinationVC = navController.storyboard?.instantiateViewController(withIdentifier: "MyIdentifier") as! MyDestinationViewController
navController.pushViewController(mydestinationVC, animated: false)
}


But it takes up to one minute for the transition between my menuVC and my destinationVC to happen. When my app is opened, pushing the view controller works perfectly. Did I forget about something? What is wrong with my code? The delay only happens when the app is closed and reopens again after tapping on the push notification.

ANE ANE
Answer Source

UILocalNotification is deprecated since iOS10. You might receive your push notification, but func application(_ application: UIApplication, didReceive notification: UILocalNotification) will never be called. The „delay“ you are experiencing is just the second local push notification, whose handling will work when your app is active.

Use UNUserNotificationCenter instead, but keep your „old handling“ for iOS9 devices.

Setup your notification like this:

        let center = UNUserNotificationCenter.current()
        let content = UNMutableNotificationContent()
        content.title = „MyTitle“
        content.body = „MyBody“
        content.categoryIdentifier = UNNotificationDefaultActionIdentifier

        let units: Set<Calendar.Component> = [.second, .hour, .minute, .day, .month, .year]
        let dateComponents = Calendar.current.dateComponents(units, from: date)

        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        center.add(request)

and handle your received notification like that:

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
 {
    // Local Push notifications
    if #available(iOS 10.0, *)
    {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

        }
    }
    else
    {
        application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
    }

    return true
 }

 @available(iOS 10.0, *)
 func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void)
 {
    // handle your response here
    completionHandler()
 }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download