Why local notification is not firing for UNCalendarNotificationTrigger

Published

The local notification is supposed to fire at 25-9-2021. Here is the print object of fire time

▿ year: 2021 month: 9 day: 26 isLeapMonth: false

  • year : 2021
  • month : 9
  • day : 25
  • isLeapMonth : false
let content = UNMutableNotificationContent()
content.title = "Test title"
content.body = "sample test body"
var trigger:UNCalendarNotificationTrigger
let n = 1
let nextTriggerDate = Calendar.current.date(byAdding: .day, value: n, to: Date())!
let comps = Calendar.current.dateComponents([.year, .month, .day], from: nextTriggerDate)
trigger = UNCalendarNotificationTrigger(dateMatching: comps, repeats: false)
content.subtitle = "Sub title-Date-NonRepeat"
let request = UNNotificationRequest(identifier: "test.my.example", content: content, trigger: trigger)    
UNUserNotificationCenter.current().add(request) { Error in                        
    if let err = Error {
        print("Notification Error:(String(describing: err))")
    }
}

Again I added time with Date with below code changes

var comps = Calendar.current.dateComponents([.year, .month, .day], from: nextTriggerDate)
comps.day = 25
comps.hour = 12
comps.minute = 17
comps.second = 10

Here is PO of comps variable

var comps = Calendar.current.dateComponents([.year, .month, .day],
from: nextTriggerDate)
comps.day = 25
comps.hour = 12
comps.minute = 17
comps.second = 10

I have given just date to see if it fires and I have given just date and time to see if it fires and I’m doing the whole thing in main thread and it’s not working

Can someone make me understand what am I doing wrong here

Source: Ios Questions

Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq