我有一个关于 UNUserNotificationSettings 的问题。我们可以使用任何方法在 iOs 10+ 中打开/关闭通知(声音、警报、角标(Badge))吗?在 iOs 9 及以下版本中,我使用 registerUserNotificationSettings 方法在应用程序中打开声音、警报、角标(Badge),但在 iOs 10+ 中不能这样做。对我的情况有什么建议吗?
最佳答案
对于 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);
关于ios - UIUserNotificationSettings 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40604518/