在没有弹出权限对话框的情况下添加用户通知类别

adding usernotification category without permission dialog popping up

是否可以在不弹出通知权限对话框的情况下添加交互式通知类别?问题是,如果他们在原始通知权限对话框中点击了 "Don't Allow",但随后手动更改了通知设置,您的类别将永远不会被添加,而且似乎也无法将它们添加回来。有没有办法把两者分开?

我试过:

    UIMutableUserNotificationAction* snoozeAction = [[UIMutableUserNotificationAction alloc] init];
[snoozeAction setIdentifier:@"snooze_action_id"];
[snoozeAction setTitle:@"Snooze"];
[snoozeAction setActivationMode:UIUserNotificationActivationModeBackground];
[snoozeAction setDestructive:NO];
[snoozeAction setAuthenticationRequired:NO];



UIMutableUserNotificationCategory* SnoozeCategory = [[UIMutableUserNotificationCategory alloc] init];
[SnoozeCategory setIdentifier:kNotifCategory];
[SnoozeCategory setActions:@[snoozeAction] forContext:UIUserNotificationActionContextDefault];
[SnoozeCategory setActions:@[snoozeAction] forContext:UIUserNotificationActionContextMinimal];

NSSet* categories = [NSSet setWithArray:@[SnoozeCategory]];
//NOT asking for permission to send any type of notifications here, just making sure our categories get saved
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

如您所见,我将通知设置设置为 UIUserNOtificationTypeNone,但权限对话框仍然弹出。

来自 registerUserNotificationSettings:

上的文档

The first time your app launches and calls this method, the system asks the user whether your app should be allowed to deliver notifications and stores the response. Thereafter, the system uses the stored response to determine the actual types of notifications you may use.

...

Calling this method with a new user settings object replaces the previous settings request.

所以我认为您可以做的是将您的代码包装在每次应用程序启动时调用的方法中(可能在 application:didFinishLaunchingWithOptions: 甚至 applicationDidBecomeActive: 中)。用户只会收到一次提示,如果他们拒绝并稍后启用,您的新方法应该添加正确的设置。