Detect "Allow Notifications" Is On/Off for iOS8

Checking Push Notification Registration: isRegisteredForRemoteNotifications Not Updating

Because iOS 8 does register the device and provides a Token even if the user opts out from pushes.

In that case pushes are not presented to the user when the push is sent, but if your app is running it gets the payload so you can update it when the app is running...

To check if push notifications are enabled in iOS 8 you should check for the enabled user notification types:

- (BOOL)pushNotificationsEnabled {
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
return (types & UIUserNotificationTypeAlert);
}
else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
}
}

How to know user tapped Don't allow in push notification

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.

iOS7 and iOS8: how to detect when user said No to a request for push notifications

Your didFailToRegisterForRemoteNotificationsWithError method isn't called, because the registration didn't fail - it was never even attempted because the user denied the request.

On iOS7 you can do a couple of things:

  1. Assume that remote notifications aren't available until didRegisterForRemoteNotificationsWithDeviceToken is called
  2. Check the value enabledRemoteNotificationTypes on the UIApplication object

Check if Local Notifications are enabled in IOS 8

You can check it by using UIApplication 's currentUserNotificationSettings

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permiossion granted");
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(@"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(@"Alert Permission Granted");
}
}

Hope this helps , Let me know if you need more info

Swift ios check if remote push notifications are enabled in ios9 and ios10

Updated answer after iOS 10 is using UNUserNotificationCenter .

First you need to import UserNotifications then

let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: { permission in
switch permission.authorizationStatus {
case .authorized:
print("User granted permission for notification")
case .denied:
print("User denied notification permission")
case .notDetermined:
print("Notification permission haven't been asked yet")
case .provisional:
// @available(iOS 12.0, *)
print("The application is authorized to post non-interruptive user notifications.")
case .ephemeral:
// @available(iOS 14.0, *)
print("The application is temporarily authorized to post notifications. Only available to app clips.")
@unknown default:
print("Unknow Status")
}
})

this code will work till iOS 9, for iOS 10 use the above code snippet.

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
// User is registered for notification
} else {
// Show alert user is not registered for notification
}


Related Topics



Leave a reply



Submit