swift - 如何在 SwiftUI 中打开通知点击的特定视图?
问题描述
一旦点击通知,我就会尝试将用户发送到 DetailView。我已经遵循了多个说明,例如这个。但是,当我对其进行测试时,点击通知仍然会将我带到错误的视图(settingsView)。
在 App Delegate 中,我目前有这个设置(下面的更新代码滚动):
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
registerBackgroundTasks()
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "open" {
NotificationCenter.default.post(name: NSNotification.Name("DetailViewNew"), object: nil)
}
completionHandler()
}
在 SettingsDetailView 中(触发和设置通知的位置)
func configureNewAlert() {
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications()
center.removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()
content.title = "Header"
content.body = "Test"
content.sound = UNNotificationSound.default
if let selectedHour = Calendar.current.dateComponents([.hour], from: userData.wakeUpTime).hour, let selectedMinute = Calendar.current.dateComponents([.minute], from: userData.wakeUpTime).minute{
var dateComponents = DateComponents()
dateComponents.hour = selectedHour
dateComponents.minute = selectedMinute
let open = UNNotificationAction(identifier: "open", title: "Open ", options: .foreground)
let cancel = UNNotificationAction(identifier: "cancel", title: "Close", options: .destructive)
let categories = UNNotificationCategory(identifier: "action", actions: [open,cancel], intentIdentifiers: [])
UNUserNotificationCenter.current().setNotificationCategories([categories])
content.categoryIdentifier = "action"
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let randomIdentifier = UUID().uuidString
let request = UNNotificationRequest(identifier: randomIdentifier, content: content, trigger: trigger)
center.add(request) { error in
if let error = error {
print(error.localizedDescription)
}
}
}
}
在我的 DetailView 中(我希望用户在点击通知时登陆)
struct DetailViewNew: View {
let poemPublisher = NotificationCenter.default.publisher(for: NSNotification.Name("DetailViewNew"))
@State private var showScreen = false
var body: some View {
ZStack{
Text("Text")
}
.onReceive(poemPublisher) { notification in
self.showScreen = true
}
}
}
编辑 我删除了类别和操作,因为我只想让用户点击通知或关闭它(不需要自定义操作)。
当我这样做时,if response.actionIdentifier == UNNotificationDefaultActionIdentifier
确实会调用完成处理程序。现在的问题是我如何路由它以便在点击时打开 DetailView?
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == UNNotificationDismissActionIdentifier {
print ("Message Closed")
}
else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
print ("App is Open")
NotificationCenter.default.post(name: NSNotification.Name("DetailViewNew"), object: nil)
}
completionHandler()
}
有什么建议么?谢谢。
解决方案
推荐阅读
- performance - 如何阅读 Chrome 开发工具性能分析 --> 网络选项卡?
- flask - 使用 Flask,Pymongo 从 mongo 集群访问集合中的数据项时出错
- asp.net-identity - Blazor AuthenticationStateProvider
- api - 没有来自 Wikipedia API 搜索的内容
- java - Springboot 中的 RabbitMQ
- f# - 如何映射/绑定 FParsec 用户状态?
- c# - 如何绘制多个多边形并将它们连接起来?
- java - 将 curl 请求转换为 Spring Flux WebClient
- python - 提交时的 sqlalchemy 错误 [WinError 10061]
- java - 选择父实体时,OneToOne 映射未获取子实体