Home > AI > IOS > UserNotifications >

requestAuthorization

Make the request in a context that helps the user to understand why your app needs authorization. In a task-tracking app that sends reminder notifications, you could make the request after the user schedules a first task. Sending the request in context provides a better user experience than automatically requesting authorization on first launch, because the user can more easily see what purpose the notifications serve.

class APNSAuthManager: ObservableObject {
    func requestAuth() {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            
            if error != nil {
                // Handle the error here.
            }
            
            print("user allowed")
            
            // Enable or disable features based on the authorization.
        }
    }
}

Leave a Reply