1

I need to detect when an user allow or don't allow push notification.

I should call an push api in server when the user tapped allow or don't allow button in the first push notification alert. (allow -> pushYn = Y, don't allow -> pushYn = N) and the user turn on, off in iPhone's Settings - Notifications

So, I called the api in "didRegisterForRemoteNotificationsWithDeviceToken" like this code

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Convert token to string
        var token = ""
        for i in 0..<deviceToken.count {
            token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
        }
        print(token)
        pushTokenSetHttpRequest()
    }

but the user tapped "Don't allow", it's not called.

How to know user tapped "Don't allow" in push notification alert or On Off iPhone's Settings-Notifications?

Register notification

if #available(iOS 10, *) {
            UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
            application.registerForRemoteNotifications()
        }

        else {
            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()
        }

Thank you

양홍범
  • 87
  • 8
  • You have to realize that for registering for the token, you don't need to get the user's permission. The silent notifications work without permission. **If** you want to show something (e.g. badge/alert/sound) to the user, *then* you need to get their permission – mfaani Jul 27 '17 at 03:34

3 Answers3

1

If this is the iOS level popup then I believe its not possible to find out if user has tapped don't allow in push notification. But you can always check whether your push notification is enabled or not using below method and accordingly navigate to the settings page:-

func isPushEnabledAtOSLevel() -> Bool {
 guard let settings = UIApplication.shared.currentUserNotificationSettings?.types else { return false }
 return settings.rawValue != 0
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
1

Depends on the iOS version:

// nil if not authorized. If I recall correctly, nullability was changed from iOS 8 to 9 correctly
UIApplication.shared.currentUserNotificationSettings()
// iOS 10+ (If you are using UNUserNotificationCenter). Docs link below
UNUserNotificationCenter.current().getNotificationSettings(completionHandler:)
UNUserNotificationCenter.current()authorizationStatus



Docs: https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649524-getnotificationsettings

nathan
  • 9,329
  • 4
  • 37
  • 51
1

Unfortunately, we are not having accurate way to check whether user denies it or not.

But you can always check if it is given permission or not by this method. UIApplication.shared.isRegisteredForRemoteNotifications which will just tell you if the application has actually registered with Apple's push servers and has received a device token:

Return Value YES

if the app is registered for remote notifications and received its device token or NO if registration has not occurred, has failed or has been denied by the user.

Community
  • 1
  • 1
Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45