قالب وردپرس درنا توس
Home / IOS Development / Push Notification requestAuthorization always error on first app run: iOSProgramming

Push Notification requestAuthorization always error on first app run: iOSProgramming



I have a problem with registration for external push notifications. They work, but only after the power has killed the app and running the registration code again. On the first app run (code below), "given" is always incorrect when the user tapes to "Allow" alerts.

Basically, I had this code running in a class Push inside a static function, so it can be called after the user's login. For example, after login, I would call Push.requestAuthorizationAndRegister () . I have also tried to move this code to a viewer only to see if there was a problem where the code ran, but there are still errors in the first round.

After the first round, requestAuthorizaton returns granted = true as expected, and registration and push notifications also work as expected. I've seen some resources saying you should call requestAuthorization from AppDelegate.didFinishLaunchingWithOptions (before returning true), but then I also see resources saying you can call it from anywhere. Finally, this code cannot be called before AppDelegate.didFinishLaunchingWithOptions is finished because I want to ask the user after they sign in.

Any thoughts or ideas will be appreciated. Thanks!

  la center = UNUserNotificationCenter.current ()

center.requestAuthorization (options: [.badge, .sound, .alert]) {(given, error) i
guard given otherwise {
return
}

center.getNotificationSettings {(settings) in
guard settings.authorizationStatus == .authorized else {return}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications ()
}
}
}

Edit: Forget about mentioning. Our distribution goal is iOS 11.


Source link