【问题标题】:Using UNUserNotificationCenter for iOS 10使用适用于 iOS 10 的 UNUserNotificationCenter
【发布时间】:2017-01-28 16:59:33
【问题描述】:

尝试使用 Firebase 注册远程通知,但是在执行以下代码时出现错误:

UNUserNotificationCenter 仅适用于 iOS 10.0 或更新版本

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        var soundID: SystemSoundID = 0
        let soundFile: String = NSBundle.mainBundle().pathForResource("symphony", ofType: "wav")!
        let soundURL: NSURL = NSURL(fileURLWithPath: soundFile)
        AudioServicesCreateSystemSoundID(soundURL, &soundID)
        AudioServicesPlayAlertSound(soundID)
        Fabric.with([Twitter.self])


        //Firebase configuration
        FIRApp.configure()

        //Resource code from stackoverflow to create UNUserNotificationCenter
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()
        return true
    }

通过基于操作系统版本号创建 if 语句来执行简单的“修复”并不能解决我的问题。对于 UserNotifications 框架的这个解决方案,我应该做什么或考虑什么?

【问题讨论】:

    标签: swift apple-push-notifications firebase-cloud-messaging


    【解决方案1】:

    一方面,使用新的UNUserNotificationCenter,您只想在用户授予权限的情况下注册远程通知。您的代码设置方式,无论您是否获得许可,您都试图这样做,这可能是原因之一。你应该这样做:

    import UserNotifications
    
    ...
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
    
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
    
        }
    
        return true
    }
    

    如果您需要检查用户的操作系统是否低于 iOS 10.0 - 您可以尝试这样的方法来包含旧系统:

    if #available(iOS 10.0, *) {
    
        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
    
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
    
        }
    
    } else {
    
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
            UIUserNotificationType.Badge, categories: nil))
    }
    

    让我知道这是否有效,以及是否是您想要完成的。如果没有,我会删除我的答案。

    【讨论】:

    • 如果您提到像我这样的傻瓜需要为此导入 UserNotifications 可能会有所帮助。
    • @user3069232 - 谢谢,我照做了。
    • 对于新语法,see
    • 收到此错误Swift Compiler Error (Xcode): Reference to member 'registerUserNotificationSettings' cannot be resolved without a contextual type
    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多