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
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(UIUserNotificationSettings(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()
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With