xcode UIUserNotificationType 在 iOS10 Swift 3.0 中被弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39782776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:22:24  来源:igfitidea点击:

UIUserNotificationType was deprecated in iOS10 Swift 3.0

iosswiftxcodeapple-push-notificationsswift3

提问by aznelite89

I am getting this warning in iOS 10 which previously is working fine on iOS 9 :- enter image description here

我在 iOS 10 中收到此警告,之前在 iOS 9 上运行良好:- 在此处输入图片说明

Is there another function to fix this warning in iOS 10, appreciated if anyone would have idea for this issue.

iOS 10 中是否有其他功能可以修复此警告,如果有人对此问题有所了解,我们将不胜感激。

回答by Anbu.Karthik

in iOS10UIUserNotificationTypehas deprecated , use UNUserNotificationCenter

iOS10 UIUserNotificationType已弃用,使用UNUserNotificationCenter

dont forget to enable this

不要忘记启用此功能

enter image description here

在此处输入图片说明

for Swift3for sample see this

对于 Swift3示例,请参阅

import the UserNotificationsframework and add the UNUserNotificationCenterDelegatein Appdelegate

导入UserNotifications框架并UNUserNotificationCenterDelegate在 Appdelegate 中添加

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate  


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    //create the notificationCenter
    let center  = UNUserNotificationCenter.current()
    center.delegate = self
    // set the type as sound or badge
    center.requestAuthorization(options: [.sound,.alert,.badge]) { (granted, error) in
        // Enable or disable features based on authorization

        }
        application.registerForRemoteNotifications()
    return true
}

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  let chars = UnsafePointer<CChar>((deviceToken as NSData).bytes)
  var token = ""

  for i in 0..<deviceToken.count {
token += String(format: "%02.2hhx", arguments: [chars[i]])
  }

  print("Registration succeeded!")
  print("Token: ", token)
 }

 func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
  print("Registration failed!")
 }

receive the Notifications using this delegates

使用此委托接收通知

 func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {
    print("Handle push from foreground")
    // custom code to handle push while app is in the foreground
    print("\(notification.request.content.userInfo)")
 }

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Handle push from background or closed")
    // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background
    print("\(response.notification.request.content.userInfo)")
}

for more Information you can see in Apple API Reference

有关更多信息,您可以在 Apple API参考中看到

回答by Faris

// REGISTER FOR PUSH NOTIFICATIONS
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.alert, .badge, .sound])  { (granted, error) in
                // Enable or disable features based on authorization.
            }
        } else {
            // REGISTER FOR PUSH NOTIFICATIONS
            let notifTypes:UIUserNotificationType  = [.alert, .badge, .sound]
            let settings = UIUserNotificationSettings(types: notifTypes, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
            application.applicationIconBadgeNumber = 0

        }