在今天而不是明天使UNUserNotification静音

问题描述

所以我有一个小问题。 我的应用程序有重复的通知,但是如果用户在应用程序中添加或删除了某些内容,我希望今天下一个通知被静音/删除,但我仍然希望它像往常一样关闭。 是否可以使今天的待处理通知静音,但不能使重复的通知静音?

我找到了一种解决方法,目前这就是我的做法。

感谢您的帮助

let current = UNUserNotificationCenter.current()
        current.getNotificationSettings(completionHandler: { (settings) in
            if settings.authorizationStatus == .authorized {
                let startDate = self.defaults.object(forKey: startingTimeString) as! Date
                let endDate   = self.defaults.object(forKey: endingTimeString) as! Date
                let intervals = self.defaults.integer(forKey: reminderIntervalString)
                
                let tempStart = Calendar.current.dateComponents([.hour,.minute],from: startDate)
                let tempEnd   = Calendar.current.dateComponents([.hour,from: endDate)
                let tempNow   = Calendar.current.dateComponents([.hour,from: Date())
                
                //checks if the current time is within the notifications time span.
                if ((tempStart.hour! == tempNow.hour! && tempStart.minute! < tempNow.minute!) ||
                    tempStart.hour! < tempNow.hour!) &&
                    ((tempEnd.hour! == tempNow.hour! && tempEnd.minute! > tempNow.minute!) ||
                        tempEnd.hour! > tempNow.hour!)
                    {
                    //sets new reminders for the notification range
                    setReminders(Calendar.current.date(from: tempStart)!,Calendar.current.date(from: tempEnd)!,intervals)
                    current.getPendingNotificationRequests {(requests) in
                        for request in requests {
                            if let trigger = request.trigger as? UNCalendarNotificationTrigger,let triggerDate = trigger.nextTriggerDate(){
                                let difference = Calendar.current.dateComponents([.hour,from: Date(),to: triggerDate)
                                let differenceInMunutes = (difference.hour! * 60) + difference.minute!
                                // Checks if the time to the next notification is within half of the interval time.
                                if differenceInMunutes < Int(Double(intervals) * 0.5) {
                                    current.removePendingNotificationRequests(withIdentifiers: [request.identifier])
                                }
                            }
                        }
                    }
                }
            }
        })

解决方法

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

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...