gpt4 book ai didi

ios - 如何在 iOS 10 中设置应用程序图标角标(Badge)编号?

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:32 25 4
gpt4 key购买 nike

问题:

我正在尝试在 iOS 10 中设置应用程序图标角标(Badge)编号,但它失败了。我了解 UIUserNotificationSettings 现在已在 iOS 中弃用,UNNotificationSettings 取而代之。

问题:

How do I modify the below code to use UNNotificationSettings to update the icon badge number in iOS 10? Or is there another concise method?

代码:

以下代码展示了我如何在 iOS 7 - iOS 9 中设置角标(Badge)。

let badgeCount: Int = 123
let application = UIApplication.sharedApplication()

if #available(iOS 7.0, *) {
application.applicationIconBadgeNumber = badgeCount
}

if #available(iOS 8.0, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge], categories: nil))
application.applicationIconBadgeNumber = badgeCount
}

if #available(iOS 9.0, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge], categories: nil))
application.applicationIconBadgeNumber = badgeCount
}

if #available(iOS 10.0, *) {
// ?????
}

最佳答案

您需要将 UserNotifications 实现到 AppDelegate 中。

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

然后在 didFinishLaunchingWithOptions 中使用以下代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if error != nil {
//
}
}
return true

}

Here您可以在通知主题上找到大量重要内容。

对于角标(Badge):

let content = UNMutableNotificationContent()
content.badge = 10 // your badge count

关于ios - 如何在 iOS 10 中设置应用程序图标角标(Badge)编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39863338/

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