如何控制何时在iOS中提示用户推送通知权限

我已经使用Swift和Xcode 6为iPhone构build了一个应用程序,并使用Parse框架来处理服务。

在按照关于如何设置推送通知的Parse教程时,说明build议我将推送通知放在App Delegate文件中。

这是我已经添加到应用程序委托文件的代码…

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var pushNotificationsController: PushNotificationController? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Register for Push Notifications self.pushNotificationsController = PushNotificationController() if application.respondsToSelector("registerUserNotificationSettings:") { println("registerUserNotificationSettings.RegisterForRemoteNotificatios") let userNotificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound) let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil) application.registerUserNotificationSettings(settings) application.registerForRemoteNotifications() } return true; } func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { println("didRegisterForRemoteNotificationsWithDeviceToken") let installation = PFInstallation.currentInstallation() installation.setDeviceTokenFromData(deviceToken) installation.saveInBackground() } } 

那么会发生什么呢,只要应用程序第一次启动,就会提示用户授予这些权限。

我想要做的只是在某个操作发生后(即在应用程序function的演练期间)提示这些权限,所以我可以提供更多的上下文来说明为什么我们希望他们允许推送通知。

这只是简单的复制下面的代码在相关的ViewController,我会期待提示用户?

 // In 'MainViewController.swift' file func promptUserToRegisterPushNotifications() { // Register for Push Notifications self.pushNotificationsController = PushNotificationController() if application.respondsToSelector("registerUserNotificationSettings:") { println("registerUserNotificationSettings.RegisterForRemoteNotificatios") let userNotificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound) let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil) application.registerUserNotificationSettings(settings) application.registerForRemoteNotifications() } } func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { println("didRegisterForRemoteNotificationsWithDeviceToken") let installation = PFInstallation.currentInstallation() installation.setDeviceTokenFromData(deviceToken) installation.saveInBackground() } 

谢谢!

答案很简单。 如果您希望用户在其他时间被提示,例如在一个button上按下,那么只需将关于该请求的代码移动到该函数中(或从其他位置调用promptUserToRegisterPushNotifications() )。

希望帮助:)

这是Swift 2.我已经在MainViewController.swift中放置了promptUserToRegisterPushNotifications(),但是我在AppDelegate中留下了didRegisterForRemoteNotificationsWithDeviceToken,因为当我把它放在同一个MainViewController.swift中时,它没有工作。

 // In 'MainViewController.swift' file func promptUserToRegisterPushNotifications() { // Register for Push Notifications let application: UIApplication = UIApplication.sharedApplication() if application.respondsToSelector(#selector(UIApplication.registerUserNotificationSettings(_:))) { print("registerUserNotificationSettings.RegisterForRemoteNotificatios") let notificationSettings = UIUserNotificationSettings( forTypes: [.Badge, .Sound, .Alert], categories: nil) application.registerUserNotificationSettings(notificationSettings) // Register for Remote Push Notifications application.registerForRemoteNotifications() } } // In AppDelegate func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { let tokenChars = UnsafePointer<CChar>(deviceToken.bytes) var tokenString = "" for i in 0..<deviceToken.length { tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]]) } NSUserDefaults.standardUserDefaults().setObject(tokenString, forKey: "deviceToken") print("Device Token:", tokenString) } 

这是我写在代码中的方法,一旦它调用launch(didFinishLaunch)

 class func registerNotification() { if #available(iOS 10.0, *) { // push notifications UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in if (granted) { UIApplication.shared.registerForRemoteNotifications() } } let center = UNUserNotificationCenter.current() center.delegate = AppManager.appDel() center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in if error == nil { UIApplication.shared.registerForRemoteNotifications() } } } else { UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) UIApplication.shared.registerForRemoteNotifications() } }