在iOS中注册推送通知为什么要打电话

时间:2017-09-19 06:33:23

标签: ios swift apple-push-notifications

嘿我正在使用以下两种方法注册推送通知:

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将为真。为什么需要进行第二次检查?

0 个答案:

没有答案