Reputation: 31
For iOS >= 8, only
In my AppDelegate, I register for user notifications as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"didFinishLaunchingWithOptions called");
// iOS >= 8.0
// register to receive user notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
...
}
Upon completion, I register for remote notification as follows:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(@"didRegisterUserNotificationSettings called");
//register to receive remote notifications
[application registerForRemoteNotifications];
}
And upon completion, I check to see whether the app is registered
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken called");
BOOL pushNotificationOnOrOff = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; // always returns TRUE
}
But app always indicates that push notification is enabled, even when the user has explicitly set the app's remote notification capability off (via the notification permissions alert that appears after first time installation of the app, or via app settings.)
I have set the app's Background Modes / Remote Notifications to TRUE. I have been debugging on an actual device (tethered via USB cable) compiled with a development certificate.
Help, I've been battling this for hours.
Upvotes: 3
Views: 321
Reputation: 995
This seems to be a bug, I also discovered the same behaviour on iPhone 6, iOS 8.1.2.
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
always returns TRUE
even if user declined the push notification permission (via the alert view) or by manually disabling the notification via Settings.app > Notifications
.
After some research I discovered that if you have Background App Refresh
enabled for the app, then [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
will always returns TRUE
.
When Background App Refresh
set to FALSE
then [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
returns the correct value.
As a workaround, you can evaluate the [[UIApplication sharedApplication] currentUserNotificationSettings].types
to determine whether the push notification is allowed or not.
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received
UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received
UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received
UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_AVAILABLE_IOS(8_0);
Hope this helps.
Upvotes: 5