2016-09-30 3 views
4

ich diese Warnung in iOS 10 bin immer die vorher fein arbeitet an iOS 9: - enter image description hereUIUserNotificationType wurde in iOS10 Swift veraltet 3.0

Gibt es eine andere Funktion diese Warnung in iOS 10 zu fixieren, geschätzt, wenn jemand würde habe eine Idee für dieses Problem.

+0

Bitte lesen Sie die Dokumentation zu 'UIUserNotificationSettings'. – rmaddy

+0

@ Anbu.Karthik Danke für die Informationen. :) – aznelite89

+0

okie sicher, danke für deine Mühe! – aznelite89

Antwort

15

in iOS10UIUserNotificationType ist veraltet, verwenden UNUserNotificationCenter

vergessen Sie nicht, diese

enter image description here

für swift3 für Probe this siehe

zu ermöglichen

importieren Sie die UserNotifications Rahmen und fügen Sie den UNUserNotificationCenterDelegate in 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!") 
} 

die Benachrichtigungen unter Verwendung dieser Delegierten erhalten

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

für mehr Informationen Sie in Apple-API sehen Reference

+0

es funktionierte wie ein Charme. :) Vielen Dank ! – aznelite89

+2

Irgendeine Idee, wenn Unterstützung für das vorherige Rahmenwerk fallen gelassen wird? – justColbs