gpt4 book ai didi

swift - 在 iOS 10 上使用 UNUserNotificationCenter

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:48 24 4
gpt4 key购买 nike

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

UNUserNotificationCenter is only available on iOS 10.0 or newer

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 框架的这个解决方案,我应该做什么或考虑什么?

最佳答案

一方面,使用新的 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))
}

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

关于swift - 在 iOS 10 上使用 UNUserNotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41912386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com