SwiftUI:如何使通知交互更改成为我视图的一部分?
SwiftUI: How can I make a notification interaction change part of my view?
我是 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()
}
}
最简单的解决方案是在您的 NotificationDelegate
上创建一个 @Published
属性(顺便说一句,应该是 @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)
}
}
我是 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()
}
}
最简单的解决方案是在您的 NotificationDelegate
上创建一个 @Published
属性(顺便说一句,应该是 @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)
}
}