请求接收推送操作的权限

Request permission to receive push on action

我只想在用户按下 ViewController 中的按钮后询问用户的许可。我该怎么做?

应用程序启动后在我的代码对话框弹出窗口中。我无法在任何地方执行 application.registerUserNotificationSettings 除了 AppDelegate class(我总是出错)。

AppDelegate.swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Notifications
    let notificationType = UIUserNotificationType.Alert
    let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
    application.registerUserNotificationSettings(settings) // <-- that method popups dialog to allow/decline Push

    // Override point for customization after application launch.
    return true
}

你可以在任何地方使用它:

UIApplication.sharedApplication().registerUserNotificationSettings(settings)