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()
}
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With