我是 SwiftUI 的新手,需要一些幫助。我正在構建一個應用程式,我希望用戶與通知進行互動。如果他們“好的”通知,我希望它改變我的部分觀點,在這種情況下,只是一個隨著每個“好的”而上升的計數器。我遇到的問題是我不能在視圖外 @State var notificationCounter,所以它不會自動更新視圖。但是,當我在視圖中輸入 @State var notificationCounter 時,由于范圍的原因,我的 func userNotificationCenter 無法訪問該變數。我該如何解決這個問題?
這是我到目前為止所擁有的:
import SwiftUI
import UserNotifications
var notificationCounter = 0
struct HomeScreenView: View {
// @State var notificationCounter = 0
@State var delegate = NotificationDelegate()
var body: some View {
ZStack {
Color("Nice Green")
.ignoresSafeArea()
VStack {
Button(action:
createNotification,
label: {
Text("Notify User")
})
.onAppear(perform: { //request permissions
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) {(_,_) in
}
UNUserNotificationCenter.current().delegate = delegate
})
Text("Notification Interactions \(notificationCounter)")
}
}
}
func createNotification() {
let content = UNMutableNotificationContent()
content.title = "God of Posture"
content.subtitle = "Straighten Your Neck"
content.categoryIdentifier = "Actions"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 4, repeats: false)
let request = UNNotificationRequest(identifier: "In-App", content: content, trigger: trigger)
//notification actions
let close = UNNotificationAction(identifier: "Close", title: "Close", options: .destructive)
let okay = UNNotificationAction(identifier: "Okay", title: "Okay", options: .destructive)
let category = UNNotificationCategory(identifier: "Actions", actions: [close, okay], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
struct HomeScreenView_Previews: PreviewProvider {
static var previews: some View {
ContentView().previewDevice(PreviewDevice(rawValue: "iPhone X"))
}
}
}
class NotificationDelegate: NSObject, ObservableObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.badge, .banner, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "Okay" {
print("Hello")
print("counter is " String(notificationCounter))
notificationCounter = notificationCounter 1
print("counter is now " String(notificationCounter))
}
completionHandler()
}
}
uj5u.com熱心網友回復:
最簡單的解決方案是@Published在您的NotificationDelegate(順便說一下,應該是@StateObject,而不是@State)上創建一個屬性。
struct HomeScreenView: View {
@StateObject var delegate = NotificationDelegate()
var body: some View {
ZStack {
Color("Nice Green")
.ignoresSafeArea()
VStack {
Button(action: delegate.createNotification) {
Text("Notify User")
}
.onAppear {
delegate.requestAuthorization()
}
Text("Notification Interactions \(delegate.notificationCounter)")
}
}
}
}
class NotificationDelegate: NSObject, ObservableObject, UNUserNotificationCenterDelegate {
@Published var notificationCounter = 0
override init() {
super.init()
UNUserNotificationCenter.current().delegate = self
}
func requestAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) {(_,_) in
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.badge, .banner, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "Okay" {
print("Hello")
print("counter is " String(notificationCounter))
notificationCounter = notificationCounter 1
print("counter is now " String(notificationCounter))
}
completionHandler()
}
func createNotification() {
let content = UNMutableNotificationContent()
content.title = "God of Posture"
content.subtitle = "Straighten Your Neck"
content.categoryIdentifier = "Actions"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 4, repeats: false)
let request = UNNotificationRequest(identifier: "In-App", content: content, trigger: trigger)
//notification actions
let close = UNNotificationAction(identifier: "Close", title: "Close", options: .destructive)
let okay = UNNotificationAction(identifier: "Okay", title: "Okay", options: .destructive)
let category = UNNotificationCategory(identifier: "Actions", actions: [close, okay], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344349.html
上一篇:傳遞給不帶引數的呼叫的引數
