14

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.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
            UIUserNotificationType.Badge, categories: nil
            ))

application.applicationIconBadgeNumber = 5

However, I get an error for using '|' in UIUserNotificationSettings block and will also receive the error "Argument labels (forTypes, categories) do not match any of the available overloads" for UIUserNotificationSettings if I only have UIUserNotificationType.badge as the first argument. Did swift 3.0 change the syntax for this statement?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Lee
  • 913
  • 2
  • 7
  • 12
  • 5
    *All* syntax changed with Swift 3, but yours is even pre-Swift 2. Have a look at http://stackoverflow.com/a/30763344/1187415 and then start the "Convert to current Swift Syntax" converter in Xcode. Or read the documentation. – Martin R Jun 17 '16 at 20:06
  • I couldn't care less about the points, but you should change the correct answer for this question as it changed with iOS 10.0. It's a very common question, and people should know what the correct answer is for now, – Pierce Feb 08 '18 at 20:12

3 Answers3

36

It has been updated in both Swift 2 and Swift 3. This line should fix your issue. Also make sure any other lines with UIUserNotificationType have had their variables switched to lowercase.

let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
John Bridge
  • 572
  • 5
  • 9
  • 3
    Swift 3 + iOS 10 uses UNNotificationSettings https://developer.apple.com/reference/usernotifications/unnotificationsettings – markturnip Oct 19 '16 at 03:08
  • 3
    `UIUserNotificationSettings` has now been deprecated for iOS 10.0. I posted an answer below as to what's the current recommended syntax. – Pierce Oct 28 '16 at 18:05
15

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.

Pierce
  • 3,148
  • 16
  • 38
2

"rawValue" can use | operator.

This code works in swift3, we don't need to do that though.

let types = UIUserNotificationType(rawValue:UIUserNotificationType.alert.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.badge.rawValue)

application.registerUserNotificationSettings(UIUserNotificationSettings(types: types, categories: nil))
Yamamoto
  • 165
  • 8