嘿我正在使用以下两种方法注册推送通知:
func registerForPushNotifications() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
guard granted else { return }
self.getNotificationSettings()
}
} else {
// Fallback on earlier versions
// iOS 9 and below register
}
}
func getNotificationSettings() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
guard settings.authorizationStatus == .authorized else { return }
UIApplication.shared.registerForRemoteNotifications()
}
} else {
// Fallback on earlier versions
}
}
在registerForPushNotifications
中调用 didFinishLaunchingWithOptions
。这是教程中的代码。我的问题是,为什么我必须检查settings.authorizationStatus == .authorized
?如果用户拒绝在第一次应用启动时使用推送通知,然后在推送通知设置granted
中将此设置更改为已批准,则registerForPushNotifications
将为真。为什么需要进行第二次检查?