4

I want to set a repeat Local notification from date. For Example:

StartDate: 25 June 2018

Todays Date: 21 June 2018

I am stuck here. Below code is working but it is firing local notification from today not from 25 June 2018.

Please have a look to my Local Notification function:

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()
        }

    }

It should repeat daily but from 25th of June. Thanks in Advance!!

Keyur Tailor
  • 424
  • 3
  • 16
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34

2 Answers2

0

Try this,

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))")
}
})

If you want to test notification in your device,

Assume that next notification arrival at 25 June 2018 at 7.00 pm, you will modify the date in device settings to 6.59 pm and 25 June 2018 or you will modify the date in device settings to 7.01 pm and 25 June 2018

scheduled notification will arrive at that time,

it's just the sample, it will repeat the notification in every specific weekday. If you want to repeat the notification, you should set the anyone order like daily, weekly, every Monday etc.. otherwise, you should register the multiple notifications for the specific days(unorder days) with a unique id's.

refer this - How to show multiple local notifications?

If You want your notification to repeat daily but you want to skip the first occurrence. I don't think that's possible. there is a similar question

Scheduling local notifications to repeat daily from tomorrow in Swift

Good Luck!

Ramkumar Paulraj
  • 1,841
  • 2
  • 20
  • 40
0

Try below lines of code :

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()
    }

}

I have changed

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

to

 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()

} }

Happy Coding....

Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51