0

I have enabled UserNotifications in my app and it all works great, apart from a bug in the very beginning (First install). Local Notification requires to ask user for permission to send notifications and it comes as an alert on first install, where user chooses his/hers options ("Allow", "Don't Allow"). The problem is that this notification request is called in "applicationDidFinishLaunchingWithOptions" method in AppDelegate and it gets cut off by another alert, which is my LocalAuthorization(TouchID) alert initiated in viewDidLoad. Is there a way to put all those alerts in some kind of a queue, so they are fired one after another and not over each other? Or, to somehow tell viewDidLoad alert to wait for AppDelegate alert to finish showing? Any input is welcome. Thanks.

Dusan Juranovic
  • 157
  • 2
  • 12

2 Answers2

0

Move UNUserNotification authorization request from AppDelegate to viewDidLoad and call other alerts in completion block.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Dusan Juranovic
  • 157
  • 2
  • 12
0

extension ViewController: UNUserNotificationCenterDelegate {

//for displaying notification when app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    //If you don't want to show notification when app is open, do something here else and make a return here. 
    //Even you you don't implement this delegate method, you will not see the notification on the specified controller. So, you have to implement this delegate and make sure the below line execute. i.e. completionHandler.

    completionHandler([.alert, .badge, .sound]) 
}

// For handling tap and user actions
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    switch response.actionIdentifier {
    case "action1":
        print("Action First Tapped")//here you can your alert
    case "action2":
        print("Action Second Tapped")//here you can your alert
    default:
        break
    }
    completionHandler()
}

}

Sachin Kishore
  • 324
  • 3
  • 8