如何在 Swift 中安排特定日期和时间间隔的本地通知?

问题描述 投票:0回答:1

我需要更新这个日程功能:

func scheduleNotifications(numberOfNotifications: Int) {
    // Create notification content
    let content = UNMutableNotificationContent()
    content.title = "Your Notification Title"
    content.body = "Your Notification Body"
    content.sound = .default
    //content.userInfo = ["url": "myApp://favorite"]
    
    // Define the time range for notifications
    let startDate = DateComponents(hour: 21, minute: 40) // Start time
    let endDate = DateComponents(hour: 22, minute: 40) // End time
    
    // Calculate interval between notifications
    let totalMinutes = (endDate.hour! - startDate.hour!) * 60 + (endDate.minute! - startDate.minute!)
    guard totalMinutes > 0 else {
        print("End time must be after start time")
        return
    }
    
    let interval = max(totalMinutes / numberOfNotifications, 1) // Ensure interval is at least 1 minute
    
    // Define the days of the week for notifications
    let daysOfWeek: [Int] = [1, 3] // Sunday, Tuesday
    
    // Create date components for start and end times
    var date = Date()
    let calendar = Calendar.current
    let currentDateComponents = calendar.dateComponents([.year, .month, .day], from: date)
    var startDateComponents = startDate
    var endDateComponents = endDate
    startDateComponents.year = currentDateComponents.year
    startDateComponents.month = currentDateComponents.month
    startDateComponents.day = currentDateComponents.day
    endDateComponents.year = currentDateComponents.year
    endDateComponents.month = currentDateComponents.month
    endDateComponents.day = currentDateComponents.day
    
    // Create notification triggers for each day of the week within the specified time range
    for day in daysOfWeek {
        date = calendar.date(from: currentDateComponents)!
        let weekday = calendar.component(.weekday, from: date)
        let daysToAdd = (day + 7 - weekday) % 7
        date = calendar.date(byAdding: .day, value: daysToAdd, to: date)!
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        let dayOfWeek = dateFormatter.string(from: date)
        print("Scheduling notification for \(dayOfWeek)")
        
        let triggerDate = calendar.date(bySettingHour: startDate.hour!, minute: startDate.minute!, second: 0, of: date)!
        let endDate = calendar.date(bySettingHour: endDate.hour!, minute: endDate.minute!, second: 0, of: date)!
        
        if triggerDate <= endDate {
            // Schedule notifications at regular intervals
            for i in 0..<numberOfNotifications {
                let triggerTimeInterval = TimeInterval(i * interval * 60)
                if triggerTimeInterval > 0 {
                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: triggerTimeInterval, repeats: false)
                    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
                    UNUserNotificationCenter.current().add(request)
                }
            }
        }
    }
}

我尝试将 daysOfWeek 数组添加到触发器变量中,但没有收到预期的通知。

我的目标是通过此功能:我想在我指定的日期的开始时间和结束时间之间发送多个通知。 (例如,在周三和周六的 14:00 到 17:00 之间发送 20 条通知。)此外,我还需要克服 iOS 中一次仅安排 64 条通知的限制,因为我需要允许用户最多安排 140 条通知每周通知。我该如何处理这个问题?

ios swift uilocalnotification localnotification
1个回答
0
投票

此外,我还需要克服 iOS 中一次只能安排 64 个通知的限制,因为我需要允许用户每周最多安排 140 个通知。

那是不可能的。连64个通知都没有承诺。

您要做的是安排接下来的几个通知,当用户再次启动您的应用程序时,您将安排更多通知。有关您可以使用的其他工具的更多讨论,请参阅Swift 中的祈祷时间通知。例如,推送通知、重大位置更改(如果适用)和后台应用程序刷新。

只要用户定期与您的应用进行交互,就应该不会太困难,尽管 3 小时内 20 条通知可能过多,并且您可能需要重新设计以避免这种用例。

如果用户很少与您的应用程序交互,则无法实现这些工作。用户需要通过定期启动您的应用程序来表明对您的应用程序的兴趣,否则系统将根据设计阻止您的应用程序通知用户。这是你的设计必须处理的事情。如果您的应用程序的接触程度太低,以至于用户预计不会经常启动它,您可以考虑将您的功能转移到小部件中。

© www.soinside.com 2019 - 2024. All rights reserved.