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

后端 未结 3 751
伪装坚强ぢ
伪装坚强ぢ 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: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()
        }
    }
    

提交回复
热议问题