我在我的应用程序中启用了UserNotifications,除了一开始的错误(首次安装)外,一切都很好用。本地通知要求用户请求发送通知的权限,并在第一次安装时提供警报,用户选择他/她的选项("允许","不允许" )。问题是这个通知请求在" applicationDidFinishLaunchingWithOptions"中被调用。 AppDelegate中的方法,它被另一个警报切断,这是我在viewDidLoad中启动的LocalAuthorization(TouchID)警报。有没有办法将所有这些警报放在某种队列中,所以它们会一个接一个地被激发而不是相互激活?或者,以某种方式告诉viewDidLoad警报等待AppDelegate警报完成显示?欢迎任何输入。感谢。
答案 0 :(得分:0)
将UNUserNotification授权请求从AppDelegate移至viewDidLoad并在完成块中调用其他警报。
答案 1 :(得分:0)
扩展名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()
}
}