iosswiftnotificationsmute

Swift: Mute notifications for current day


in my app I send a daily notification to remind the user to visit the app. This notification is locally delivered every day at 1pm.

        func scheduleNotifications() -> Void {
        for notification in notifications {
            let content = UNMutableNotificationContent()
            content.title = notification.title
            let todaysDate = Date()
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd"
            let currentDay = dateFormatter.string(from: todaysDate)
            let currentDayInt = Int(currentDay) ?? 0
            var datComp = DateComponents()
            datComp.hour = 13
            datComp.minute = 00
            datComp.day = currentDayInt + 1
            let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true)
            let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request) { error in
                guard error == nil else { return }
                print("Scheduling notification with id: \(notification.id) on Day \(datComp.day ?? 00) at \(datComp.hour ?? 00) - \(datComp.minute ?? 00)")
            }
        }
    }

As you can see, I added the "current day + 1" lines because if the user opens the app before 1pm, there is no need to deliver the notification on this day. So every time the user opens the app, I use UNUserNotificationCenter.current().removeAllPendingNotificationRequests() to remove and reschedule the notification for the next day (by recalling the function above).

My issue: The notification should repeat every day, which it does, as long as the user opens the app. But if the user does not open the app on one day, there will be no notification on the following days.

Is there a way to mute notifications for the current day so that I don't have to use this "current day + 1"-thing? Or does anyone have a better idea?

Thank you guys.


Solution

  • I think you misunderstood how repeating notifications and your datComp works here.

    For example (let's use today's date: May 27)

    datComp.hour = 13
    datComp.minute = 00
    datComp.day = currentDayInt + 1    // in our example it's 28
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true)
    

    means that your notification will get triggered every month on 28th, all parameters your trigger knows is hour, minute, day and by repeating it, will be triggered on every 28th at 13:00.

    So the way your app works now is that you set up monthly notification starting from tomorrow, when you open the app you remove that monthly notification and reschedule it for day later. By opening every day it gives you impression that its daily notification but it's not, it's monthly. That's why if you don't open the app nothing shows up next the day, it will show up next month.

    You can check my similar explanation here: (top answer there, maybe it is better worded and easier to understand)

    Removing scheduled local notification

    and my solution for similar problem i had here:

    How to set up daily local notification for tasks but don't show it when user completes the task before