UIUserNotificationType was deprecated in iOS10 Swift 3.0
Asked Answered
L

2

8

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

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

Lowminded answered 30/9, 2016 at 2:33 Comment(3)
Please read the documentation for UIUserNotificationSettings.Seldun
@Anbu.Karthik Thanks for the information. :)Lowminded
okie sure , thanks for your effort!Lowminded
H
26

in iOS10 UIUserNotificationType has deprecated , use UNUserNotificationCenter

dont forget to enable this

enter image description here

for Swift3 for sample see this

import the UserNotifications framework and add the 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!")
 }

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

Hughett answered 30/9, 2016 at 4:9 Comment(2)
Any idea when support for the previous framework will be dropped?Ardine
can you look into itTeodoor
D
3
// 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

        }
Denice answered 28/12, 2018 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.