与UNUserNotificationSettings相关的问题
问题描述:
我收到了关于UNUserNotificationSettings的问题。我们可以使用任何方法在iOs 10+中打开/关闭通知(声音,警报,徽章)吗?在iOs 9及以下版本中,我使用registerUserNotificationSettings方法打开应用程序中的声音,警报,徽章,但不能在iOs 10+中执行同样的操作。对我的情况有何建议?与UNUserNotificationSettings相关的问题
答
为iOS 10,可以使用方法requestAuthorizationWithOptions
这样的:
//iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
这是参数options
的定义:
typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) {
UNAuthorizationOptionBadge = (1 << 0),
UNAuthorizationOptionSound = (1 << 1),
UNAuthorizationOptionAlert = (1 << 2),
UNAuthorizationOptionCarPlay = (1 << 3),
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
感谢你的小费,但该方法解决不了我的问题 –
@AD你想在授权后开启/关闭吗? – isaced
是的,这正是我想要做的。我打算使用此方法: [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings){ }]; 但它只是获取设置状态,无法更新设置 –