Error while checking if user has registered for local notifications in Swift

I'm trying to check if my user has or not accepted to receive notifications from my app. I was using UIApplication.sharedApplication().isRegisteredForRemoteNotifications() but it was always saying that it was false, even if I have accepted to receive them.

Now I changed it up a bit, I'm using this code to check:

func checkRegisteredUserForNotifications() {
    if UIApplication.sharedApplication().currentUserNotificationSettings() != nil {

        notificationStatus.hidden = false

        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.notificationStatus.alpha = 1.0
        })

        notificationStatus.text = "Thats greay!"

        btn.hidden = false

        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.btn.alpha = 1.0
        })

    } else {

        notificationStatus.hidden = false

        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.notificationStatus.alpha = 1.0
        })

        notificationStatus.text = "That's bad"

        btn.hidden = false

        UIView.animateWithDuration(0.3, animations: { () -> Void in
            btn.alpha = 1.0
        })

        print("not registered!!")
    }

But it always giving me true, even if I have rejected to receive the notifications. Why is this happening?

Some more info: I have a button, and when the user taps on it, it will call the function registerForRemoteNotifications(), and after 10 seconds it will call this function to check if user has accepted.

Any help is appreciated, thanks!

Answers


if UIApplication.sharedApplication().currentUserNotificationSettings.types == .None {
    // not registered
} else {
    // registered in some way
}

This simply checks if there are any allowed notification settings types (there are 3 and you can have any combination of them: Badge, Sound, Alert). If there is a specific type you need, you can modify the if-statement if necessary.


Need Your Help

Decimal to Binary, strange output

c++ binary new-operator

I wrote program to convert decimal to binary for practice purposes but i get some strange output. When doing modulo with decimal number, i get correct value but what goes in array is forward slash?...

Adding an exe to start-up using batch

windows batch-file windows-7 exe startup

Need a batch file that can add and remove a .exe file from the start-up.