4

I am trying to get swift to send a notification. This is the code I have in the appdelegate.swift file that is registering the notification

application.registerUserNotificationSettings(
    UIUserNotificationSettings(
        forTypes:UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))

I get this error:

Binary operator '|' to two 'UIUserNotificationType' operands.

If you could help me get a solution for this problem that would be great.

Thank you

ArnonZ
  • 3,822
  • 4
  • 32
  • 42
Jeg0g
  • 51
  • 1
  • 2

2 Answers2

19

Try this

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)

First: import UserNotification

import UserNotifications

Second: Then add delegate to appDelegate :

class AppDelegate: UIResponder, **UIApplicationDelegate**

Third: Then Register notifications :

if #available(iOS 10.0, *)
        {
            let center = UNUserNotificationCenter.currentNotificationCenter()
            center.delegate = self
            //center.setNotificationCategories(nil)
            center.requestAuthorizationWithOptions([.Alert,.Badge,.Sound]) {
                granted,error in
                if (error == nil) {
                    UIApplication.sharedApplication().registerForRemoteNotifications()
                }
            }
        }

Swift 3.0

 if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization. 
        }
    }
    else
    {
        UIApplication.shared.registerUserNotificationSettings(UIUser‌NotificationSettings‌(types: [.sound, .alert, .badge], categories: nil))
    }
    UIApplication.shared.registerForRemoteNotifications()

Swift 4.0

Do above steps then register notification on the main thread like:

DispatchQueue.main.async(execute: {
              UIApplication.shared.registerForRemoteNotifications()
})
MRizwan33
  • 2,723
  • 6
  • 31
  • 42
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
  • Thanks, your answer does resolve the error. However, your settings declaration includes alert and badge, but not sound. That's not going to be a problem? – Dave G Feb 24 '16 at 00:22
  • @DaveG sound added. Thank you. – Tejas Ardeshna Feb 24 '16 at 06:31
  • ```if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } } else { UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)) } UIApplication.shared.registerForRemoteNotifications() ``` swift 3, with fallback for earlier versions of iOS – trickster77777 Oct 21 '16 at 11:53
0

If you are inside AppDelegate try this

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
    UIApplication.sharedApplication().cancelAllLocalNotifications()

    return true
}
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
KSJain
  • 71
  • 1
  • 1
  • 2