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

时间:2016-11-03 23:26:48

标签: ios iphone apple-push-notifications richpush serviceextension

我的应用实现了新的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+上运行 - 有人可以确认吗?

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

首先初始化通知服务:

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]) {

    }

答案 2 :(得分:-1)

将框架的状态更改为可选。当需要时,一些框架不适用于ios 9。 enter image description here