How can I set up local notifications in iOS with different repeating periods (weekly, monthly, yearly) and ensure they work correctly?

I’m trying to implement a feature in my iOS app where I need to schedule local notifications that repeat at different intervals “weekly“, “monthly“, or “yearly“, starting from a specified date. I’m using the UNCalendarNotificationTrigger with the repeats parameter set to true, but I’m not sure if it handles different repeating periods as expected.

Here’s the function I’ve written:

func scheduleNotifications(startDate: Date?, remindersDayBefore: Int16, periodType: PeriodType) {

    // Calculate the reminder date based on the start date and reminders day before
    let calendar = Calendar.current
    var reminderDate = calendar.date(byAdding: .day, value: 0, to: startDate)!

    let currentDate = Date()

    // If the reminder date is in the past, calculate the next occurrence
    if reminderDate < currentDate {
        switch periodType {
        case .weekly:
            reminderDate = calendar.date(byAdding: .day, value: 0, to: reminderDate)!
        case .monthly:
            reminderDate = calendar.date(byAdding: .month, value: 0, to: reminderDate)!
        case .yearly:
            reminderDate = calendar.date(byAdding: .year, value: 0, to: reminderDate)!
            return
        }
    }

    // Extract date components for the trigger
    var reminderComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: reminderDate)

    // Create the notification content
    let content = UNMutableNotificationContent()
    switch remindersDayBefore {
case 0:
    content.title = "Reminder: Event today"
    content.body = "This is a reminder that your event is today."
case 1:
    content.title = "Reminder: Event tomorrow"
    content.body = "This is a reminder that your event is tomorrow."
default:
    content.title = "Reminder: Event in (remindersDayBefore) days"
    content.body = "This is a reminder that your event is scheduled in (remindersDayBefore) days."
}

    content.sound = .default

    // Create the calendar notification trigger
    let trigger = UNCalendarNotificationTrigger(dateMatching: reminderComponents, repeats: true)

    // Create the notification request
    let notificationRequest = UNNotificationRequest(identifier: subscription.id.uuidString, content: content, trigger: trigger)

    // Add the notification request
    UNUserNotificationCenter.current().add(notificationRequest) { (error) in
        if let error = error {
            print("Error adding notification: (error)")
        } else {
            print("Notification scheduled successfully.")
        }
    }
}

My Questions:

  1. Will this setup correctly handle notifications that repeat weekly,
    monthly, or yearly? Specifically, I’m concerned about the
    UNCalendarNotificationTrigger and whether it will continue to work
    correctly for these different intervals after the initial trigger.

  2. How can I test these notifications to make sure they work as
    expected for future dates and different repeating periods? I’ve had
    trouble testing on a real device and the iOS simulator is tied to
    the macOS system date.

Thanks in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật