如何在iOS 10之前使用UNNotificationServiceExtension运行应用程序?

我的应用程序实现了新的iOS 10富推送NotificationService扩展。

一切都按预期在iOS 10上运行,但我也想支持iOS 10之前的设备 – 当然不是丰富的推送,而只是常规推送。 将Xcode中的部署目标降低到例如8.0或9.0并尝试在较旧的模拟器或设备上运行时,会出现以下错误:

Simulator: The operation couldn't be completed. (LaunchServicesError error 0.) Device: This app contains an app extension that specifies an extension point identifier that is not supported on this version of iOS for the value of the NSExtensionPointIdentifier key in its Info.plist. 

Apple无法正式发现任何东西,只要您添加服务扩展程序,您的应用程序将仅在iOS 10+上运行 – 有人可以确认吗?

Bhavuk Jain正在讨论如何支持旧版ios上的通知,但没有解决LaunchServicesError问题。 要解决此问题,您需要转到部署信息下的扩展目标 – >常规 – >设置部署目标(本案例为10.0)。

首先初始化通知服务:

 func initializeNotificationServices() -> Void { if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.delegate = self center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in if granted { UIApplication.shared.registerForRemoteNotifications() } } }else { let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil) UIApplication.shared.registerUserNotificationSettings(settings) } } 

如果成功注册了远程通知,则将为所有设备调用:

 optional public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 

仅适用于iOS 10,用于处理远程通知:

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo notificationReceived(userInfo: userInfo, application: nil) } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo } 

对于iOS 10以下的设备:

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { } 

将框架的状态更改为可选。 当需要一些框架不能通过ios 9. 在这里输入图像描述