gpt4 book ai didi

swift - LocalNotification警报干扰

转载 作者:行者123 更新时间:2023-11-30 11:52:17 25 4
gpt4 key购买 nike

我已经在我的应用程序中启用了 UserNotifications,除了一开始(首次安装)时出现的错误之外,一切都运行良好。本地通知需要询问用户发送通知的权限,并且它在首次安装时作为警报出现,用户可以选择他/她的选项(“允许”、“不允许”)。问题是,此通知请求在 AppDelegate 中的“applicationDidFinishLaunchingWithOptions”方法中调用,并被另一个警报中断,该警报是我在 viewDidLoad 中启动的 LocalAuthorization(TouchID) 警报。有没有一种方法可以将所有这些警报放入某种队列中,以便它们被一个接一个地触发而不是相互重叠?或者,以某种方式告诉 viewDidLoad 警报等待 AppDelegate 警报完成显示?欢迎任何意见。谢谢。

最佳答案

扩展 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()
}

}

关于swift - LocalNotification警报干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48217730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com