malex
malex

Reputation: 10096

Strange behavior of registerUserNotificationSettings in iOS8

I try to register only alert type notification at application start in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

by calling

UIUserNotificationType types = UIUserNotificationTypeAlert;

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

In

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

I permanently get all types in notificationSettings

<UIUserNotificationSettings: 0x16dd6160; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>

And

UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]

gives me the same all types despite my initial choice of the only alert type.

So I can't setup restricted dynamical permissions on start.

There is no any information about similar problems in the internet.

Upvotes: 4

Views: 313

Answers (2)

Alexander Ushakov
Alexander Ushakov

Reputation: 5407

You must register notifications in (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

UPDATE: According to the Apple documentation:

The first time you call the registerUserNotificationSettings: method, iOS presents a dialog that asks the user for permission to present the types of notifications the app registered. After the user replies, iOS asynchronously calls back to the UIApplicationDelegate object with the application:didRegisterUserNotificationSettings: method, passing a UIUserNotificationType object that specifies the types of notifications the user allows.

Users can change their notification settings at any time using the Settings app. Your app is added to the Settings app as soon as you call registerUserNotificationSettings:. Users can enable or disable notifications, as well as modify where and how notifications are presented. Because the user can change their initial setting at any time, call currentUserNotificationSettings before you do any work preparing a notification for presentation

So if user has already accepted Notification settings, application can't change them. currentUserNotificationSettings always show current user settings, not application settings.

Upvotes: 0

malex
malex

Reputation: 10096

It seems that it is impossible to setup UserNotificationType from application in iOS8 (at least). One needs to use general Notification Center to set any combination of sound, badge and alert. Only the first attempt to register which occurs with Push notification permissions alert sets UserNotificationType needed.

Another way is to send UserNotificationType on subscription to your push server which should make payload depending of this type.

Upvotes: 1

Related Questions