在iOS 10之前,我可以注册并接收通知。现在我什么也没收到。下面是我的代码:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if error == nil {
if granted {
print("granted")
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
print("not granted")
}
} else {
}
}
} else {
// Fallback on earlier versions
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [UIUserNotificationType.badge, UIUserNotificationType.sound, UIUserNotificationType.alert], categories: nil)
UIApplication.shared.registerForRemoteNotifications()
UIApplication.shared.registerUserNotificationSettings(settings)
}顺便说一下,granted有时是真的,有时是假的。无论哪种方式,我都没有收到通知。回调函数与通知工作时的回调函数相同。
发布于 2018-03-22 00:33:52
您的代码看起来是正确的,但您可以尝试如下所示。
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) {[weak self] (granted, error) in
guard error == nil else { return }
if granted {
// register for remote notifications
UIApplication.shared.registerForRemoteNotifications()
// set delegate
UNUserNotificationCenter.current().delegate = self
}
}您将通过以下方式收到ios 10的通知。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}当您点击通知时,将调用以下方法。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}https://stackoverflow.com/questions/49411461
复制相似问题