Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request authorization for remote notifications always return false at first start

request authorization for push notifications always return false when app is first loaded even user tap "allow" on dialog. Here is function for register which is called in didFinishLaunchingWithOptions. At next launch granted is true.

func registerForPushNotifications() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("Permission granted: \(granted)")
            guard granted else{return}
            self.getNotificationSettings()
        }
}
like image 667
Gorthez Avatar asked Jan 16 '18 09:01

Gorthez


1 Answers

Register for remote notification within didfinishLaunchingWithOptions and make sure registered for remote notification.

application.registerForRemoteNotifications()

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

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        guard granted else{return}

        self.getNotificationSettings()

    }
    application.registerForRemoteNotifications()
    return true
}
like image 107
adarshaU Avatar answered Sep 27 '22 22:09

adarshaU