How to control when to prompt user for push notification permissions in iOS

后端 未结 3 769
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 19:05

I\'ve built an application for iPhone using Swift and Xcode 6, and the Parse framework to handle services.

While following the Parse tutorials on how to set up push noti

相关标签:
3条回答
  • 2021-02-13 19:31

    This is for Swift 2. I have placed promptUserToRegisterPushNotifications() in MainViewController.swift, but I have left didRegisterForRemoteNotificationsWithDeviceToken in AppDelegate because it didn't work when I place it on the same 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)
    
    }
    
    0 讨论(0)
  • 2021-02-13 19:34

    This is method I have written in the code and works fine once it called on 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()
        }
    }
    
    0 讨论(0)
  • 2021-02-13 19:55

    The answer is simple. If you want the user to be prompted some other time, for instance on a button press then simply move the code regarding the request into that function (or call promptUserToRegisterPushNotifications() from somewhere else).

    To get a hold of the application variable outside the AppDelegate, simply do this:

    let application = UIApplication.shared
    

    Hope that helps :)

    0 讨论(0)
提交回复
热议问题