【问题标题】:UIUserNotificationSettings refusing to accept multiple notification types in swift [duplicate]UIUserNotificationSettings 拒绝快速接受多种通知类型 [重复]
【发布时间】:2015-09-19 12:58:45
【问题描述】:

我正在尝试快速发送通知。这是我在 appdelegate.swift 文件中注册通知的代码

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

我收到此错误:

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

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

谢谢

【问题讨论】:

    标签: ios swift notifications xcode7


    【解决方案1】:

    试试这个

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

    首先:导入 UserNotification

    import UserNotifications
    

    第二次:然后将委托添加到appDelegate:

    class AppDelegate: UIResponder, **UIApplicationDelegate**
    

    第三:然后注册通知:

    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()
    })
    

    【讨论】:

    • 谢谢,您的回答确实解决了错误。但是,您的设置声明包括警报和徽章,但不包括声音。不会有问题吧?
    • @DaveG 声音已添加。谢谢。
    • 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,支持早期版本的 iOS
    【解决方案2】:

    如果你在 AppDelegate 里面试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 2020-08-10
      • 2011-09-11
      • 2021-06-11
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多