如何在 pre iOS 10 上使用 UNNotificationServiceExtension 运行 应用程序?
How to run app with UNNotificationServiceExtension on pre iOS 10?
我的应用程序实现了新的 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+ 添加服务扩展后 - 有人可以确认吗?
首先初始化通知服务:
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
}
对于 iOS10 以下的设备:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
}
Bhavuk Jain 正在谈论如何支持旧 ios 上的通知,但没有解决 LaunchServicesError。要解决此问题,您需要转到部署信息下的扩展目标 -> 常规 -> 设置部署目标(本例为 10.0)。
将框架的状态更改为可选。当需要时,ios 9. 某些框架无法工作。
enter image description here
我的应用程序实现了新的 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+ 添加服务扩展后 - 有人可以确认吗?
首先初始化通知服务:
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
}
对于 iOS10 以下的设备:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
}
Bhavuk Jain 正在谈论如何支持旧 ios 上的通知,但没有解决 LaunchServicesError。要解决此问题,您需要转到部署信息下的扩展目标 -> 常规 -> 设置部署目标(本例为 10.0)。
将框架的状态更改为可选。当需要时,ios 9. 某些框架无法工作。 enter image description here