Async-
Async-

Reputation: 3289

Type UNUserNotificationCenter has no member current, swift 2.3

In iOS10 User notifications were reworked by apple. Now I am trying to adjust my app to these changes, following this:

let center = UNUserNotificationCenter.current()

gives me an error:

Type UNUserNotificationCenter has no member current, swift 2.3

I know that that tutorial is maybe for swift3. But I need to make it work in swift 2.3. Is it even feasible and if yes, how to do it?

Upvotes: 2

Views: 4008

Answers (4)

Srinivasan_iOS
Srinivasan_iOS

Reputation: 1078

swift 4.1

if #available(iOS 10.0, *) {

UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in
        self.getNotificationSettings()
    })

} else {
    // Fallback on earlier versions
    let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)
}
DispatchQueue.main.async {
    UIApplication.shared.registerForRemoteNotifications()
}

Upvotes: 0

SagarScript
SagarScript

Reputation: 1242

Import this:

import  UserNotifications

implement this delegate with in your appDelegate file:

UNUserNotificationCenterDelegate

Which will give you access to the following methods:

func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {}

and then add this to your app delegate's didFinishLaunchWithOptions method to tie it all up.

if #available(iOS 10.0, *) {
    // For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.current().delegate = self

    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(
        options: authOptions,
        completionHandler: {_, _ in })
} else {
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()

Upvotes: 0

ingconti
ingconti

Reputation: 11646

for Xcode 8 / ios9/10

simply use:

import  UserNotifications

. .

    // swift 3.0:
    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }

    } else {
        // Fallback on earlier versions
    }

// NOTE: if are in FOREGROUND, You will NEVER be called! :)

Upvotes: 7

Phillip Mills
Phillip Mills

Reputation: 31016

The documentation seems in conflict with itself. Although it describes the current() method and says to use it, the examples show let center = UNUserNotificationCenter.currentNotificationCenter().

As you say, this may be a Swift version dependency.

Upvotes: 3

Related Questions