2

I am trying to check if the user has enabled remote notifications for my app but it always return true even if I disable/uninstall my app.

I am checking with this code:

if UIApplication.shared.isRegisteredForRemoteNotifications == false {
    OneSignal.registerForPushNotifications()
    print("Notifications is off")
} else {
   print("Notifications is on")
}

But this will always run: print("Notifications is on")

So how can I check if user has enabled notification for my app?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2636197
  • 3,982
  • 9
  • 48
  • 69

2 Answers2

1
    if let settings = UIApplication.shared.currentUserNotificationSettings {
         if settings.types != UIUserNotificationType() {
             print("is on!")
         }else{
             print("is off!")
         }
}else{
     print("is off")
}

This seems to work, not sure if its the right way?

user2636197
  • 3,982
  • 9
  • 48
  • 69
  • @RomOne no its all there, try copy/paste it :) – user2636197 Nov 10 '16 at 23:59
  • Yep my mistake :) I'm not entirely sure, but here I think you are testing if the user has enable notification in general, not for your app specifically – RomOne Nov 11 '16 at 00:02
  • @RomOne this didnt work either : / If I press deny it wont prompt me anymore – user2636197 Nov 11 '16 at 00:09
  • This is normal, you can ask for permission only once. And once you have asked, isRegisteredForRemoteNotifications will always be set to YES. In addition currentUserNotificationSettings return if user has a set on its notification in general, meaning that you still cannot know the notification setting for your app – RomOne Nov 11 '16 at 00:11
0

Actually this will always return true, it will return false only in the case the permission for remote notification were never asked. Meaning that once you have asked the user, not matter what he has answered, this will always return true.

The raison why is that when you ask permission, it registers directly the user (without even waiting his answer, you can check by putting breakpoints that the delegate is triggered before the answer of the user). Then if the user denied the permission, the device will still be registered but notifications won't show up until the user turn them on in general settings.

This is not a bug, Apple does this to avoid dev keep asking for permissions. So it's actually not possible to know if a user has accepted or denied permission :/

RomOne
  • 2,065
  • 17
  • 29
  • But how come certain apps show a popUp message sometimes telling me to enable notifications. Also check my answer bellow – user2636197 Nov 10 '16 at 23:57
  • They are some way to guess, but it can be tricky. and you won't be sure at all, it's just a guess. I'll try to find how I did that in previous work and post an update ;) – RomOne Nov 11 '16 at 00:03