LGSideMenuController:注册多个本地推送通知会导致屏幕处理延迟

Posted

技术标签:

【中文标题】LGSideMenuController:注册多个本地推送通知会导致屏幕处理延迟【英文标题】:LGSideMenuController: Registration of several local push notifications leads to delay in screen handling 【发布时间】:2017-07-26 16:26:03 【问题描述】:

我在注册多个本地推送通知时发现了一个奇怪的问题:

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

当我的本地推送通知以后台模式出现时,我点击它并想直接呈现一个屏幕:

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)

但是我的 menuVC 和我的destinationVC 之间的转换最多需要一分钟。当我的应用程序打开时,推送视图控制器可以完美运行。我是不是忘记了什么?我的代码有什么问题?延迟仅在应用程序关闭并在点击推送通知后再次重新打开时发生。

【问题讨论】:

【参考方案1】:

UILocalNotification 自 ios10 起已弃用。您可能会收到推送通知,但 func application(_ application: UIApplication, didReceive notification: UILocalNotification) 永远不会被调用。您遇到的“延迟”只是第二个本地推送通知,当您的应用程序处于活动状态时,其处理将起作用。

请改用 UNUserNotificationCenter,但保留对 iOS9 设备的“旧处理”。

这样设置您的通知:

        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)

并像这样处理您收到的通知:

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()
 

【讨论】:

谢谢!为什么没有编译器警告表明此弃用?多么讨厌的错误。唯一没有触发委托方法的情况是应用程序终止时。我会调整iOS10的通知处理。

以上是关于LGSideMenuController:注册多个本地推送通知会导致屏幕处理延迟的主要内容,如果未能解决你的问题,请参考以下文章

ios开发-常用三方工具

如何用一个手机号注册多个百度云网盘?

是否可以在 mio 中注册多个超时?

如何创建注册到尤里卡服务注册表的多个尤里卡服务实例?

一个频道的多个推送注册

如何为多个活动注册制作网站? [关闭]