Swift 3.0 Syntax change for UIUserNotificationSettings

后端 未结 3 1080
滥情空心
滥情空心 2021-02-13 11:53

I am using swift 3.0 and am trying to add badge numbers to my app. I believe the correct way to do this is similar to what is below.

application.registerUserNoti         


        
3条回答
  •  别跟我提以往
    2021-02-13 12:09

    From what I understand, UIUserNotificationSettings has been deprecated for iOS 10.0. It is now recommended that you use the UNUserNotificationCenter.

    Here's what I did to make sure my code was up to date:

    1) Import the UserNotifications framework in your AppDelegate

    import UserNotifications

    2) Inside the didFinishLaunchingWithOptions function inside the AppDelegate, add the following:

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

    Once you have the notifications registered and allowed, you can change the badge number at any point:

    UIApplication.shared.applicationIconBadgeNumber = value

    This worked for me, I just tested it by sending a remote notification to my phone, and it worked fine. Hope this helps.

提交回复
热议问题