Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIUserNotificationSettings refusing to accept multiple notification types in swift [duplicate]

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

like image 756
Jeg0g Avatar asked Nov 30 '22 17:11

Jeg0g


1 Answers

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()
})
like image 188
Tejas Ardeshna Avatar answered Dec 10 '22 11:12

Tejas Ardeshna