0

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!

Henrique Dourado
  • 320
  • 1
  • 7
  • 16

1 Answers1

1
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.

tktsubota
  • 9,371
  • 3
  • 32
  • 40
  • Thanks for the answer! I'm only able to test this code tomorrow tho, I'll let u know if it worked, but it should. I think the problem was that I was checking if it was nil, but it is never nil I guess. Thanks again! Also, not related to this question tho, how do I detect a first launch of an app and present a view controller? – Henrique Dourado Mar 17 '16 at 02:46
  • @HenriqueDourado I can't give you a complete answer here, but you basically use NSUserDefaults. Here is a [popular question in Objective-C](http://stackoverflow.com/questions/9964371/how-to-detect-first-time-app-launch-on-an-iphone?lq=1) and [this answer in Swift](http://stackoverflow.com/a/30253149/5644794) for more information. – tktsubota Mar 17 '16 at 02:52