iOS8 中 registerUserNotificationSettings 的奇怪行为

Strange behavior of registerUserNotificationSettings in iOS8

我在

中尝试在应用程序启动时仅注册警报类型通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

通过调用

UIUserNotificationType types = UIUserNotificationTypeAlert;

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

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

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

我永久获得 notificationSettings

中的所有类型
<UIUserNotificationSettings: 0x16dd6160; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>

UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]

尽管我最初选择了唯一的警报类型,但所有类型都相同。

所以我无法在启动时设置受限的动态权限。

互联网上没有任何关于类似问题的信息。

您必须在 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中注册通知

更新: 根据 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

因此,如果用户已经接受了通知设置,则应用程序无法更改它们。 currentUserNotificationSettings 始终显示当前用户设置,而不是应用程序设置。

似乎无法从 iOS8 中的应用程序设置 UserNotificationType(至少)。需要使用通用通知中心来设置声音、徽章和警报的任意组合。只有在推送通知权限警报设置 UserNotificationType 时发生的第一次注册尝试才需要。

另一种方法是发送 UserNotificationType 订阅到你的推送服务器,它应该根据这种类型生成有效负载。