ios - UIUserNotificationSettings 的问题

标签 ios objective-c iphone swift

我有一个关于 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/

相关文章:

ios - 使用 Parse.com iOS 搜索用户名以获取用户

objective-c - 如何垂直移动 UITableView

objective-c - Xcode6:IBDesignable 和 IBInspectable 与 Objective-C

iOS 11 AVPlayerViewController 禁用捏合/拖动手势

ios - Admob SDK - iOS - 找不到文件

iPhone Dropbox文件同步攻略?

iphone - 为什么导航栏中的框架大小为空?

objective-c - 写入磁带驱动器有什么不同吗? (普通 C 和 Objective-C)

iphone - 我的 UIPickerView 具有与键盘相同的行为

iphone - 如何将选项(到 didFinishLaunchingWithOptions)传递到我从调整中激活的应用程序?