【问题标题】:repeated alarm notifications in swiftuiswiftui中的重复警报通知
【发布时间】:2021-08-02 04:47:16
【问题描述】:

我创建了一段代码来在下午 3 点发送本地通知,用户可以使用拨动开关来切换它。我使用 AppStorage 包装器来记住它所处的状态。代码一直有效,直到我打开和关闭开关(模拟用户是否多次切换)以及我切换开关的次数,相同的数字是通知(即我切换 5 次,我一次收到 5 个通知)。这是代码


struct trial: View {
    
    
    
    @AppStorage("3") var three = false
    
    
    func firstNotify() {
        
        let content = UNMutableNotificationContent()
        content.title = "It is 3pm"
        content.subtitle = "Read the Prime Hour"
        content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "notifysound.wav"))
        
      //  content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "notifysound"))
        
        
        var dateComponents = DateComponents()
        dateComponents.hour = 15
        dateComponents.minute = 00
        dateComponents.second = 00

        
        
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
        
        // choose a random identifier
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        
        // add our notification request
        UNUserNotificationCenter.current().add(request)
        
        
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
            if success {
                print("All set!")
            } else if let error = error {
                print(error.localizedDescription)
            }
        }
        
    }
    
    
    @State private var name: String = "Tim"
    
    
    var body: some View {
        VStack {
            
            Toggle(isOn: $three) {
                Text("3 pm")
            }
            .padding(.horizontal)
            .onChange(of: three) { newValue in
                if newValue {
                    firstNotify()
                }
            }
            
   
        }
    }
}

struct trial_Previews: PreviewProvider {
    static var previews: some View {
        trial()
    }
}


我认为正在发生的事情是 func 没有读取开关是打开还是关闭,而是在每次打开开关时添加一个通知。有谁知道这个问题的解决方案吗???

【问题讨论】:

    标签: xcode swiftui xcode12


    【解决方案1】:

    切换实际上只是在每次将值更改为 true 时安排一个新通知。

    这段代码意味着每次调用这个方法都会安排一个新的通知。

         // choose a random identifier
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
            
        // add our notification request
        UNUserNotificationCenter.current().add(request)
    

    不完全确定所需的逻辑是什么,但您可以通过更新现有通知或清除旧通知并添加新通知来解决此问题。最好不要为每个通知创建一个新的 UUID,最好有一个 ID 约定或以其他方式存储它,以便可以访问或删除它。

    您可以存储而不是切换值:

    
    @AppStorage("notification_id") var notificationID = nil // I don't know if this can be a string or nil, but hopefully it leads you in the right direction.
    
    // Then when creating the request.
    
    let id = notificationID ?? UUID().uuidString
    let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
    notificationID = id
    
    
    

    当他们切换另一个方向时,需要有另一种方法来取消计划。您可以简单地检查 id 是否存在。

    
    if let id = notificationID {
        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers identifiers: [id])
    
        // OR
    
        UNUserNotificationCenter.current()removeAllPendingNotificationRequests()
    }
    
    

    【讨论】:

    • 我希望用户能够简单地决定是否要通知他们。我有点明白你想说什么,但我不知道如何给这个警报一个 id。
    • 我用更多细节更新了我上面的答案。如果您对如何将其拼凑在一起有任何更具体的问题,请告诉我。
    • 我在使用新功能时遇到了一点问题。我创建了一个名为 turnoff 的新函数,但它给了我错误“条件绑定的初始化程序必须具有可选类型,而不是 'String'”
    • 好的,听起来 ID 不是可选的。因此,不用我上面显示的展开,只需继续并使用存储的 Id 或全部删除关闭方法中的通知,无需展开任何内容。
    • 我试过你的建议,但还是不行。通知仍然会被触发很多次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2022-10-05
    相关资源
    最近更新 更多