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:
- Assume that remote notifications aren't available until
didRegisterForRemoteNotificationsWithDeviceToken
is called - Check the value
enabledRemoteNotificationTypes
on theUIApplication
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
Upload Files to Dropbox from iOS App with Swift
Xcode 10: a Valid Provisioning Profile for This Executable Was Not Found
Maskstobounds VS. Clipstobounds
Format Uitextfield Text Without Having Cursor Move to the End
Navigation Bar Under Status Bar After Video Playback in Landscape Mode
Writing an iOS 8 Share Extension Without a Storyboard
Scrolling Slow on Mobile/iOS When Using Overflow:Scroll
iPhone iOS Will Not Display Box-Shadow Properly
iOS Safari Memory Usage with "-Webkit-Transform"
How to Prevent iOS 13 Dark Mode from Breaking Emails
Why Can't I Invert My Image Back to Original with Cifilter in My Swift iOS App
Get Random Child from Firebase Database
Simulator Slow-Motion Animations Are Now On
Table Header View Height Is Wrong When Using Auto Layout, Ib, and Font Sizes
How Is Possible That "Display: Flex" and "Align-Items: Center" Do Not Work Anymore on My Iphone
Swiftui: Make Scrollview Scrollable Only If It Exceeds the Height of the Screen