首页 文章

从日期开始重复本地通知

提问于
浏览
0

我想从日期开始重复本地通知 . 例如:

StartDate: 25 June 2018

Todays Date: 21 June 2018

我被困在这里 . 下面的代码正在运行,但它从今天开始发送本地通知,而不是2018年6月25日 .

请查看我的本地通知功能:

func scheduleDosageLocalNotification(date: Date) {

        reminder.dosageIdentifier = "Dosage_Day"

        var calendar = Calendar.current
        calendar.timeZone = TimeZone.current

        let notificationContent = UNMutableNotificationContent()
        // Configure Notification Content
        notificationContent.title = "DOSAGE REMINDER"
        notificationContent.body = "Remember to take your TEST tablet dialy."

        // Set Category Identifier
        notificationContent.categoryIdentifier = Notification.Category.First
        var components = calendar.dateComponents([.hour, .minute], from: date)

        components.hour = 08
        components.minute = 00


        let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
       // let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval!, repeats: true)

        // Create Notification Request
        let identifier = "Dosage_Day"

        let notificationRequest = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: notificationTrigger)

        // Add Request to User Notification Center
        UNUserNotificationCenter.current().add(notificationRequest) { (error) in
            if let error = error {
                print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
            }

            Utilities.saveContextForAppInfo()
        }

    }

它应该每天重复,但从6月25日开始 . Thanks in Advance!!

2 回答

  • 1

    试试这个,

    let notification = UNMutableNotificationContent()
    notification.subtitle = ""
    notification.sound = UNNotificationSound.default()
    
    
    notification.userInfo =  userInfo
    notification.title = Title
    notification.body = Message
    
    let timeStr = time
    let splitTime:[String] = timeStr.components(separatedBy: ":")
    var dayComponent = DateComponents()
    dayComponent.weekday = day as? Int //[1 to 7 get randomly]
    dayComponent.hour = Int(splitTime[0])
    dayComponent.minute = Int(splitTime[1])
    
    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dayComponent, repeats: true)
    let lnMessageId:String = message
    let dayRequest = UNNotificationRequest(identifier: lnMessageId , content: notification, trigger: notificationTrigger)
    UNUserNotificationCenter.current().add(dayRequest, withCompletionHandler: {(_ error: Error?) -> Void in
    if error == nil
    {
    //print("success")
    }
    else
    {
    //print("UNUserNotificationCenter Error : \(String(describing: error?.localizedDescription))")
    }
    })
    

    如果您想在设备中测试通知,

    假设下次通知于2018年6月25日晚上7点到达,您将设备设置中的日期修改为下午6点59分和2018年6月25日,或者您将设备设置中的日期修改为2018年下午7点1分和2018年6月25日

    预定通知将在那时到达,

    它只是样本,它将在每个特定的工作日重复通知 . 如果您想重复通知,您应该设置任何订单,例如每天,每周,每周一等 . 否则,您应该使用唯一ID注册特定日期(无日订单日期)的多个通知 .

    参考这个 - How to show multiple local notifications?

    如果您希望每天重复通知,但希望跳过第一次出现的通知 . 我不认为这是可能的 . 还有一个类似的问题

    Scheduling local notifications to repeat daily from tomorrow in Swift

    祝好运!

  • 1

    尝试下面的代码行:

    func scheduleDosageLocalNotification(date: Date) {
    
        reminder.dosageIdentifier = "Dosage_Day"
    
        var calendar = Calendar.current
        calendar.timeZone = TimeZone.current
    
        let notificationContent = UNMutableNotificationContent()
        // Configure Notification Content
        notificationContent.title = "DOSAGE REMINDER"
        notificationContent.body = "Remember to take your TEST tablet dialy."
    
        // Set Category Identifier
        notificationContent.categoryIdentifier = Notification.Category.First
        var components = calendar.dateComponents([.day,.month,.year,.hour, .minute], from: date!)//calendar.dateComponents([.hour, .minute], from: date)
    
        components.hour = 08
        components.minute = 00
    
    
        let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
       // let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval!, repeats: true)
    
        // Create Notification Request
        let identifier = "Dosage_Day"
    
        let notificationRequest = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: notificationTrigger)
    
        // Add Request to User Notification Center
        UNUserNotificationCenter.current().add(notificationRequest) { (error) in
            if let error = error {
                print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
            }
    
            Utilities.saveContextForAppInfo()
        }
    
    }
    

    我改变了

    var components = calendar.dateComponents([.hour, .minute], from: date)
    

    var components = calendar.dateComponents([.day,.month,.year,.hour, .minute], from: date!)
    

    Updated answer :

    func scheduleDosageLocalNotification(date: Date) {
    reminder.dosageIdentifier = "Dosage_Day"
    var calendar = Calendar.current
    calendar.timeZone = TimeZone.current
    let notificationContent = UNMutableNotificationContent()
    // Configure Notification Content
    notificationContent.title = "DOSAGE REMINDER"
    notificationContent.body = "Remember to take your TEST tablet dialy."
    // Set Category Identifier
    notificationContent.categoryIdentifier = Notification.Category.First
    var components = calendar.dateComponents([.day, .month, .year, .hour, .minute], from: date!) //calendar.dateComponents([.hour, .minute], from: date)
    components.hour = 08
    components.minute = 00
    var notification: UILocalNotification = UILocalNotification()
    notification.category = "Dosage_Day"
    notification.alertBody = "Local notification"
    notification.fireDate = newDate
    notification.repeatInterval = .day
    print(notification)
    print(notification.fireDate)
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
    Utilities.saveContextForAppInfo()
    

    }}

    快乐编码....

相关问题