我正试图迅速发送通知。这是我在appdelegate.swift文件中注册通知的代码
application.registerUserNotificationSettings(
UIUserNotificationSettings(
forTypes:UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
我收到此错误:
二元运算符' |'两个UIUserNotificationType'操作数。
如果你能帮助我找到解决这个问题的解决办法,那就太好了。
谢谢
答案 0 :(得分:20)
试试这个
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(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
}
UIApplication.shared.registerForRemoteNotifications()
Swift 4.0
上面的步骤然后在主线程上注册通知,如:
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
答案 1 :(得分:0)
如果你在AppDelegate里面试试这个
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
UIApplication.sharedApplication().cancelAllLocalNotifications()
return true
}