1

I'am trying to schedule a notification every specified date on each year for example every 1/6/yyyy and 15/6/yyyy i've done this code but it does not work

    var dateComponents = DateComponents()
        dateComponents.hour = 07
        dateComponents.minute = 24
        dateComponents.month=06;
        dateComponents.day=28;



        let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateComponents, repeats:true)
        let request     = UNNotificationRequest.init(identifier: UUID().uuidString, content: content, trigger: trigger)


        let center = UNUserNotificationCenter.current()
        center.add(request)
Moh'd Awad
  • 1,758
  • 1
  • 12
  • 22
  • What is not working? Is the notification added to the notification centre, but you never receive it? Or is it not repeating at the expected intervals? – Dávid Pásztor Jun 28 '17 at 14:36
  • It does not work at all the notification does not triggered – Moh'd Awad Jun 28 '17 at 16:31
  • You are definitely missing a notification content and category from your notification request. If you have not requested access to notifications and not set up notification categories, you also need to do those. See my answer. – Dávid Pásztor Jun 28 '17 at 16:37

1 Answers1

1

You need to add a notification content as well, otherwise your notification won't get added. You also need to add the specified notification category to your notification centre's categories.

If you have the notification centre request set up and the user grants the request, with below code you can send a notification at a specific date.

var dateComponents = DateComponents()
dateComponents.hour = 07
dateComponents.minute = 24
dateComponents.month=06;
dateComponents.day=28;
dateComponents.timeZone = TimeZone.current
let yourFireDate = Calendar.current.date(from: dateComponents)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
            "Your notification title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Your notification body", arguments: nil)
content.categoryIdentifier = "Your notification category"
content.sound = UNNotificationSound.default()
content.badge = 1

let dateComponents = Calendar.current.dateComponents(Set(arrayLiteral: Calendar.Component.month, Calendar.Component.day, Calendar.Component.hour,Calendar.Component.minute), from: yourFireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "Your notification identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
        if let error = error {
            //handle error
        } else {
            //notification set up successfully
        }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • i've tried your code and i've checked the if statement the notification has been set but not triggered . (for sure i changed the hour ,minute, date for date and time after 1 minute from now ) – Moh'd Awad Jun 29 '17 at 12:43
  • If you are using a simple `UNTimeIntervalNotificationTrigger`, do you receive the notification? If you check this and it works, we can make sure that the problem is coming from the trigger and not from somewhere else with your setup. Also, in the else condition of `UNUserNotificationCenter.current().add(request...` you should call `trigger.nextrTriggerDate()` to see when should the notification be delivered. – Dávid Pásztor Jun 29 '17 at 12:47
  • i noticed a weird thing , i've set the hour to 05 and minute to 55 and in the else block i've print the tigger.nextTriggerDate and this has been printed , it's add 7 hours to hour part it return Optional(2018-06-29 12:55:00 +0000) – Moh'd Awad Jun 29 '17 at 12:54
  • The issue is most probably cause by time zone difference. If you don't specify a time zone, it is by default GMT. See my updated answer. – Dávid Pásztor Jun 29 '17 at 13:22
  • same problem i set hour 6 minute 25 the result of print nextTriggerDate is : Optional(2017-06-29 13:25:00 +0000) – Moh'd Awad Jun 29 '17 at 13:25
  • even i've tried to add Calendar.Component.timeZone to the set and problem is same (note i 've changed the second dateComponent variable because it's already defined to be able to use it in the trigger parameter) let dateComponents1 = Calendar.current.dateComponents(Set(arrayLiteral: Calendar.Component.month, Calendar.Component.day, Calendar.Component.hour,Calendar.Component.minute,Calendar.Component.timeZone), from: yourFireDate!) let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents1, repeats: false) – Moh'd Awad Jun 29 '17 at 13:29
  • It is definitely caused by not setting something up with the DateComponents/Date object that is different on your device than the default. Try going thrugh the properties of `DateComponent` and see what might be different on your setup than the default. – Dávid Pásztor Jun 29 '17 at 13:35