如何在 Swift 中使用 removeAllPendingNotificationRequests

问题描述

我尝试在 SwiftUI 中使用“removeAllPendingNotificationRequest()”中断计划的通知,但我不知道如何操作。我正在计划多个通知,并想通过一个按钮取消它们。

谢谢

       let content2 = UNMutableNotificationContent()
                        content2.title = "Dies ist deine Persönliche Erinnerung,"
                        content.subtitle = "Deine Schulsachen zu packen."
                        content2.body = "Du hast morgen: \(Fächer[Dienstag1B]) \(Fächer[Dienstag2B]) \(Fächer[Dienstag3B]) \(Fächer[Dienstag4B]) \(Fächer[Dienstag5B]) \(Fächer[Dienstag6B])"
                        content2.sound = UNNotificationSound.default
                        
                        var dateComponents2 = DateComponents()
                        dateComponents2.calendar = Calendar.current
                        
                        dateComponents2.weekday = 2 //Montag-> Dienstag
                        dateComponents2.hour = 8
                        dateComponents2.minute = 45
                        
                        let trigger2 = UNCalendarNotificationTrigger(dateMatching: dateComponents2,repeats: true)
                        
                        let uuid2 = UUID().uuidString
                        let request2 = UNNotificationRequest(identifier: uuid2,content: content2,trigger: trigger2)
                        
                        let notificationCenter2 = UNUserNotificationCenter.current()
                        notificationCenter2.add(request2)
                        
                    }
                }
               
                Button(action: UNUserNotificationCenter.current().removeAllPendingNotificationRequest(),label: {
                    Text("Cancel Notifications")
                })
                
                
            }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)