如何设置任务的每日本地通知,但在用户之前完成任务时不显示 [英] How to set up daily local notification for tasks but don't show it when user completes the task before

查看:53
本文介绍了如何设置任务的每日本地通知,但在用户之前完成任务时不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前也曾出现过类似的问题,但我想我需要进一步澄清,因为我仍然不知道如何做到.我是一名初学者程序员,所以请原谅我任何错误.

I know similar questions appeared before but I think I need more clarification since i still don't know how to make it done. I'm a beginner programmer so please forgive me any mistakes.

如果用户还没有完成我的应用程序,我想从我的应用程序中获取日常提醒,那么当他已经完成任务时,如何使它不显示?

I'm trying to have daily reminders for daily tasks from my app IF user didn't complete it yet, so how can i make it not show up when he had already done the task?

到目前为止,我发现的解决方案建议删除待处理的通知,并在同一时间为将来的日期设置新的通知.

Solutions i found so far suggest to remove pending notification and setting up new one for future date in the same time.

我使用以下代码成功设置了每日通知:

I successfully set up daily notifications using this code:

    func sendDailyReminder() {
        let content = UNMutableNotificationContent()
        content.title = "Daily reminder"
        content.body = "You still have task to complete today."
        content.sound = UNNotificationSound.default
        var dateComponents = DateComponents()
        dateComponents.hour = 20
        dateComponents.minute = 00
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
        let request = UNNotificationRequest(identifier: "dailyTrigger", content: content, trigger: trigger)

        center.add(request) { (error) in
            if let error = error {
                print("Notification Error: ", error)
            }
        }
    }

我还可以使用removePendingNotificationRequest方法成功删除待处理的通知,但是如何在此处使用dateComponents为明天设置触发器?

i can also successfully remove pending notification with removePendingNotificationRequest method but how can I set it up trigger for tomorrow using dateComponents here?

还是有其他方法可以实现?也许使用后台获取功能来检查是否在发送通知之前完成了操作?

Or is there any other way to achieve that? Maybe using background fetch to check if its done just before sending notification?

我发现一些答复表明它实际上是不可能的,但是任何任务或待办事项应用程序如何才能实现这样的目标?

Some replies i found suggest that its actually impossible but then how any task or to-do app can achieve something like that?

推荐答案

我最终每个工作日都使用了多个通知,但设置方式却几乎没有什么不同:

I ended up using multiple notifications for each weekday but set it up in little different way:

首先使用工作日Int作为标识符设置每日提醒

First set up daily reminder using weekday Int as identifier

func setWeekdayReminder(weekday: Int) {
        let center = UNUserNotificationCenter.current()
        let content = UNMutableNotificationContent()
        content.title = "Daily reminder"
        content.body = "You still have some tasks to complete today."
        content.sound = UNNotificationSound.default
        var dateComponents = DateComponents()
        dateComponents.hour = 18
        dateComponents.minute = 35
        dateComponents.weekday = weekday
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true
        let request = UNNotificationRequest(identifier: String(weekday), content: content, trigger: trigger)

        center.add(request) { (error) in
            if let error = error {
                print("Notification Error: ", error)
            }
        }
    }

然后,我创建了一个功能来检查用户启动应用程序后是否还有缺失的一天(今天除外,因此即使用户完成任务后将其删除,我也不会再次请求今天的通知)

then i made a function to check if there is any missing day after users launches app (except today, so i won't request todays notification again even after removing it earlier when user completes the task)

func checkDailyReminder() {

       let currentWeekday = Calendar.current.component(.weekday, from: Date())

       center.getPendingNotificationRequests { (requests) in
            var weekdayArray : [Int] = []

            for each in requests {
               weekdayArray.append(Int(each.identifier)!)
            }
            for number in 1...7 {
                if weekdayArray.contains(number) {
                    print("weekdayArray contains weekday \(number)")
                } else {
                    print("weekdayArray doesnt contain weekday \(number)")
                    if number != currentWeekday {
                          self.setWeekdayReminder(weekday: number)
                    }
                }
            }

        }
    }

当然这是一种黑客行为,当用户完成任务并且以某种方式不会返回一周并在同一工作日再次将其打开时,他当天将不会收到通知,但在其余时间都可以使用时间.

Of course it's kind of a hack and when user completes the task and somehow won't go back for a week and open it again on the same weekday then he won't get notification that day but it works for rest of the time.

这篇关于如何设置任务的每日本地通知,但在用户之前完成任务时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆