2016-09-30 2 views
4

Я получаю это предупреждение в ИО 10, который ранее работает нормально на прошивке 9: - enter image description hereUIUserNotificationType осуждались в iOS10 Swift 3,0

Есть еще одна функции, чтобы исправить это предупреждение в ИО 10, понятно, если кто-то будет есть идея по этой проблеме.

+0

Пожалуйста, ознакомьтесь с документацией для 'UIUserNotificationSettings'. – rmaddy

+0

@ Anbu.Karthik Спасибо за информацию. :) – aznelite89

+0

ОК, конечно, спасибо за ваши усилия! – aznelite89

ответ

15

в iOS10UIUserNotificationType имеет осуждается, используйте UNUserNotificationCenter

не забудьте включить эту опцию

enter image description here

для Swift3 для образца см this

импортировать 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!") 
} 

получить уведомление с помощью этих делегатов

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

для получения дополнительной информации вы можете увидеть в компании Apple API Reference

+0

он работал как шарм. :) Благодаря ! – aznelite89

+2

Любая идея, когда поддержка предыдущей структуры будет удалена? – justColbs

Смежные вопросы