UIUserNotificationSettings拒绝接受swift中的多种通知类型

Jeg*_*g0g 4 notifications ios swift xcode7

我想迅速发送通知.这是我在注册通知的appdelegate.swift文件中的代码

application.registerUserNotificationSettings(
    UIUserNotificationSettings(
        forTypes:UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

二元运算符'|' 两个'UIUserNotificationType'操作数.

如果你能帮助我找到解决这个问题的解决方案,那就太好了.

谢谢

Tej*_*hna 20

试试这个

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

第一:导入UserNotification

import UserNotifications
Run Code Online (Sandbox Code Playgroud)

第二:然后将代理添加到appDelegate:

class AppDelegate: UIResponder, **UIApplicationDelegate**
Run Code Online (Sandbox Code Playgroud)

第三:然后注册通知:

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()
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

Swift 4.0

上面的步骤然后在主线程上注册通知,如:

DispatchQueue.main.async(execute: {
              UIApplication.shared.registerForRemoteNotifications()
})
Run Code Online (Sandbox Code Playgroud)