计时器到期时推送通知?

发布于 2025-02-02 21:35:46 字数 4932 浏览 6 评论 0原文

我正在尝试在正在构建的WatchOS中设置计时器应用程序的推送通知,以防用户将应用程序放置在后台。我已经在初始化中收集了用户的许可,但是我试图使用unotificationRequest来设置本地通知。但是,当我确实设置unotificationRequest时,我会遇到type'()'的错误我的代码。

我尝试将此请求添加到“导航链接”区域和第二视图控制器区域,这两者都导致我遇到了相同的错误(type'()'()'不符合'view'> ) - 为了启用通知的选择是什么?

该代码在下面列出:

import SwiftUI
import UserNotifications

struct ContentView: View {
    @State var secondScreenShown = false
    @State var timerVal = 60
    var body: some View {
        VStack{
            Picker(selection: $timerVal, label: Text("Set Half Time")) {
                Text("1").tag(60)
                Text("2").tag(120)
                Text("20").tag(1200)
                Text("25").tag(1500)
                Text("30").tag(1800)
                Text("35").tag(2100)
                Text("40").tag(2400)
                Text("45").tag(2700)
                
            }
            NavigationLink(destination: SecondView(secondScreenShown: $secondScreenShown, timerVal: timerVal), isActive: $secondScreenShown) {
                Text("Set Half!")
                ///Trying to enable notifications when the user presses "Set Half"
                let center = UNUserNotificationCenter.current()
                
                let content = UNMutableNotificationContent()
                content.title = "Title"
                content.subtitle = "Subtitle"
                
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerVal), repeats: false)
                
                let request = UNNotificationRequest(identifier: "Identifier", content: content, trigger: trigger)
                
                UNUserNotificationCenter.current().add(request)
                
            }
        }
    }
}
struct SecondView: View {
    @Binding var secondScreenShown: Bool
    @State var timerVal: Int
    @State var stoppage: Int = 60
    @State var overtime: Bool = false
    
    var body: some View {
            VStack{
                if timerVal > 0 || overtime {
                    Text("Time Remaining in Half")
                        .font(.system(size: 14))
                    HStack(spacing: 33){
                        Text("\(timerVal / 60)")
                            .font(.system(size: 40))
                        Text("\(timerVal % 60)")
                            .font(.system(size: 40))
                            .onAppear(){
                                Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { inv in
                                    if self.timerVal > 0 {
                                        self.timerVal -= 1
                                        if self.timerVal == 0 {
                                            inv.invalidate()
                                        }
                                    }
                                }
                            }
                        
                    }
                    HStack{
                        Text("Minutes")
                            .font(.system(size: 14))
                        Text("seconds")
                            .font(.system(size: 14))
                    }
                    Button(action: {
                        self.secondScreenShown = false
                    }) {
                        Text("Cancel")
                            .foregroundColor(.red)
                    }
    
                } else {
                    
                    VStack{
                        Picker(selection: $stoppage, label: Text("Add Stoppage Time?")) {
                            Text("1").tag(60)
                            Text("2").tag(120)
                            Text("3").tag(180)
                            Text("4").tag(240)
                            Text("5").tag(300)
                            Text("6").tag(360)
                            Text("7").tag(420)
                            Text("8").tag(480)
                            Text("9").tag(540)
                        }
                        Button(action: {
                            self.timerVal = self.stoppage
                            self.overtime = true
                        }) {
                            Text("Add Stoppage")
                                .foregroundColor(.green)
                        }
                        Button(action: {
                            self.secondScreenShown = false
                        }) {
                            Text("Done")
                                .foregroundColor(.red)
                        }
                        
                    }
                    
            }

            }
    }
    
}





struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
            ContentView()
        }
    }
}

I'm trying to setup push notifications for a timer app in watchOS I'm building, in case a user puts the app into the background. I've already gathered permission from the user in my init, however I'm trying to set up a local notification using UNNotificationRequest. However, when I do set up my UNNotificationRequest, I'm met with the error of Type '()' cannot conform to 'View' when I attempt to add it to my code.

I've tried to add this request to both the NavigationLink area and the second View Controller area, both resulting in me getting the same error, listed above (Type '()' cannot conform to 'View') - what's the alternative in order to enable the notifications?

The code is listed below:

import SwiftUI
import UserNotifications

struct ContentView: View {
    @State var secondScreenShown = false
    @State var timerVal = 60
    var body: some View {
        VStack{
            Picker(selection: $timerVal, label: Text("Set Half Time")) {
                Text("1").tag(60)
                Text("2").tag(120)
                Text("20").tag(1200)
                Text("25").tag(1500)
                Text("30").tag(1800)
                Text("35").tag(2100)
                Text("40").tag(2400)
                Text("45").tag(2700)
                
            }
            NavigationLink(destination: SecondView(secondScreenShown: $secondScreenShown, timerVal: timerVal), isActive: $secondScreenShown) {
                Text("Set Half!")
                ///Trying to enable notifications when the user presses "Set Half"
                let center = UNUserNotificationCenter.current()
                
                let content = UNMutableNotificationContent()
                content.title = "Title"
                content.subtitle = "Subtitle"
                
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerVal), repeats: false)
                
                let request = UNNotificationRequest(identifier: "Identifier", content: content, trigger: trigger)
                
                UNUserNotificationCenter.current().add(request)
                
            }
        }
    }
}
struct SecondView: View {
    @Binding var secondScreenShown: Bool
    @State var timerVal: Int
    @State var stoppage: Int = 60
    @State var overtime: Bool = false
    
    var body: some View {
            VStack{
                if timerVal > 0 || overtime {
                    Text("Time Remaining in Half")
                        .font(.system(size: 14))
                    HStack(spacing: 33){
                        Text("\(timerVal / 60)")
                            .font(.system(size: 40))
                        Text("\(timerVal % 60)")
                            .font(.system(size: 40))
                            .onAppear(){
                                Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { inv in
                                    if self.timerVal > 0 {
                                        self.timerVal -= 1
                                        if self.timerVal == 0 {
                                            inv.invalidate()
                                        }
                                    }
                                }
                            }
                        
                    }
                    HStack{
                        Text("Minutes")
                            .font(.system(size: 14))
                        Text("seconds")
                            .font(.system(size: 14))
                    }
                    Button(action: {
                        self.secondScreenShown = false
                    }) {
                        Text("Cancel")
                            .foregroundColor(.red)
                    }
    
                } else {
                    
                    VStack{
                        Picker(selection: $stoppage, label: Text("Add Stoppage Time?")) {
                            Text("1").tag(60)
                            Text("2").tag(120)
                            Text("3").tag(180)
                            Text("4").tag(240)
                            Text("5").tag(300)
                            Text("6").tag(360)
                            Text("7").tag(420)
                            Text("8").tag(480)
                            Text("9").tag(540)
                        }
                        Button(action: {
                            self.timerVal = self.stoppage
                            self.overtime = true
                        }) {
                            Text("Add Stoppage")
                                .foregroundColor(.green)
                        }
                        Button(action: {
                            self.secondScreenShown = false
                        }) {
                            Text("Done")
                                .foregroundColor(.red)
                        }
                        
                    }
                    
            }

            }
    }
    
}





struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
            ContentView()
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风流物 2025-02-09 21:35:46

它显示错误,因为您已经在View部分中写下了添加通知代码。

视图仅用于呈现内容,而不是编写动作。

您可以在执行导航时编写以下代码以添加本地通知。

struct ContentViewNotification: View {

    @State var secondScreenShown = false
    @State var timerVal = 60

    var body: some View {
        
        NavigationView {
         
            VStack{
                Picker(selection: $timerVal, label: Text("Set Half Time")) {
                    Text("1").tag(60)
                    Text("2").tag(120)
                    Text("20").tag(1200)
                    Text("25").tag(1500)
                    Text("30").tag(1800)
                    Text("35").tag(2100)
                    Text("40").tag(2400)
                    Text("45").tag(2700)
                    
                }.onChange(of: timerVal) { val in
                    self.secondScreenShown = true
                }
                
                
                NavigationLink(destination: SecondView(secondScreenShown: $secondScreenShown, timerVal: timerVal), isActive: $secondScreenShown) {
                    Text("Set Half!")
                }.simultaneousGesture(TapGesture().onEnded {
                    
                    //Trying to enable notifications when the user presses "Set Half"

                    let center = UNUserNotificationCenter.current()

                    let content = UNMutableNotificationContent()
                    content.title = "Title"
                    content.subtitle = "Subtitle"

                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerVal), repeats: false)

                    let request = UNNotificationRequest(identifier: "Identifier", content: content, trigger: trigger)

                    UNUserNotificationCenter.current().add(request)
                })
            }
        }
    }
}

It is showing error because you have write the add notification code inside the View section.

View is only used for presenting the contents, not writing the actions.

You can write the below code to add the local notification when your navigation is performed.

struct ContentViewNotification: View {

    @State var secondScreenShown = false
    @State var timerVal = 60

    var body: some View {
        
        NavigationView {
         
            VStack{
                Picker(selection: $timerVal, label: Text("Set Half Time")) {
                    Text("1").tag(60)
                    Text("2").tag(120)
                    Text("20").tag(1200)
                    Text("25").tag(1500)
                    Text("30").tag(1800)
                    Text("35").tag(2100)
                    Text("40").tag(2400)
                    Text("45").tag(2700)
                    
                }.onChange(of: timerVal) { val in
                    self.secondScreenShown = true
                }
                
                
                NavigationLink(destination: SecondView(secondScreenShown: $secondScreenShown, timerVal: timerVal), isActive: $secondScreenShown) {
                    Text("Set Half!")
                }.simultaneousGesture(TapGesture().onEnded {
                    
                    //Trying to enable notifications when the user presses "Set Half"

                    let center = UNUserNotificationCenter.current()

                    let content = UNMutableNotificationContent()
                    content.title = "Title"
                    content.subtitle = "Subtitle"

                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerVal), repeats: false)

                    let request = UNNotificationRequest(identifier: "Identifier", content: content, trigger: trigger)

                    UNUserNotificationCenter.current().add(request)
                })
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文