UIUserNotificationSettings refuses to accept several types of notifications in swift

I'm trying to quickly send a notification. This is the code that I have in the appdelegate.swift file that registers the notification

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

I get this error:

Binary operator '|' for two operands of UIUserNotificationType.

If you could help me find a solution to this problem, that would be great.

thanks

+4
source share
2 answers

Try this

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

First: Import UserNotification

import UserNotifications

Second: add a 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

, , :

DispatchQueue.main.async(execute: {
              UIApplication.shared.registerForRemoteNotifications()
})
+20

AppDelegate,

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

    return true
}
0

Source: https://habr.com/ru/post/1608130/


All Articles