[英]xcode 13 - trying to trigger a local Notification every 30 sec using a public function
我试图每 30 秒触发一次本地通知,但出现错误“Type '()' cannot conform to 'View'” 我刚刚在 Xcode 13 上打开了一个新项目,它说你好世界并想触发本地在“hello world”ContentView() function 之后使用以下 A() 函数进行通知
我究竟做错了什么? 有更好的方法吗?
谢谢++++++++++++++++++
import SwiftUI
import UserNotifications
@main
struct ozApp: App {
var body: some Scene {
WindowGroup { /// error /// Type '()' cannot conform to 'View'
ContentView()
A()
}
}
}
public func A(){
// Swift
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
print("INSIDE NOTIFICATION")
// let request = UNNotificationRequest(identifier: CommonViewController.Identifier, content: content, trigger: trigger)
//print("INSIDE NOTIFICATION")
UNUserNotificationCenter.current().add(request, withCompletionHandler: {(error) in
if let error = error {
print("SOMETHING WENT WRONG")
}
})
}
尝试这个:
@main
struct ozApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear { A() }
}
}
}
如果重复,时间间隔必须至少为 60。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.