Помехи оповещения LocalNotification

Я включил UserNotifications в своем приложении, и все отлично работает, за исключением ошибки в самом начале (первая установка). Для локального уведомления требуется запросить у пользователя разрешение на отправку уведомлений, и оно приходит в виде предупреждения при первой установке, когда пользователь выбирает свои параметры («Разрешить», «Не разрешать»). Проблема в том, что этот запрос уведомления вызывается в методе «applicationDidFinishLaunchingWithOptions» в AppDelegate, и он прерывается другим предупреждением, которое является моим предупреждением LocalAuthorization (TouchID), инициированным в viewDidLoad. Есть ли способ поставить все эти оповещения в какую-то очередь, чтобы они срабатывали одно за другим, а не друг над другом? Или каким-то образом сообщить предупреждению viewDidLoad, чтобы дождаться завершения отображения предупреждения AppDelegate? Любой вклад приветствуется. Спасибо.


person Dusan Juranovic    schedule 12.01.2018    source источник


Ответы (2)


расширение 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()
}

}

person Sachin Kishore    schedule 12.01.2018
comment
Спасибо за ваш ответ. То, что вы предложили, было не совсем тем, что я просил, но все же полезно. :) - person Dusan Juranovic; 15.01.2018

Переместите запрос авторизации UNUserNotification из AppDelegate в viewDidLoad и вызовите другие оповещения в блоке завершения.

person Dusan Juranovic    schedule 12.01.2018