如何在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.

我找不到任何苹果官方声明你的应用程序只能在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中工作。此处输入图像描述

最新更新