Repeat daily local notifications in iOS 10 won't start when manually setting time and date?
I am trying to test local notifications in iOS 10 trying to trigger daily notification.
I am using the following example project: NotificationsUI-Demo
The app contains one of the following codes:
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Tutorial Reminder"
content.body = "Just a reminder to read your tutorial over at appcoda.com!"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "myCategory"
if let path = Bundle.main.path(forResource: "logo", ofType: "png") {
let url = URL(fileURLWithPath: path)
do {
let attachment = try UNNotificationAttachment(identifier: "logo", url: url, options: nil)
content.attachments = [attachment]
} catch {
print("The attachment was not loaded.")
}
}
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
I changed the value repeats
from false to true to repeat every day because according to the documentation describing the parameter repeats
:
Specify false to unblock the notification after the trigger fires. The indication of the true reasons is repeatedly redirected by the notification.
Let's say I trigger a notification for today 10/10/2017 at 18:56. At 6:56 pm I see a local notification as expected.
When I try to manually change the date and time using Settings-> Date and Time to 4/11/2017 at 6:55 pm, once at 6:56 pm collapses, I don't see the notification at all.
Can't it be tested this way?
I didn't try to wait for the next day the next day to see if another notification popped up, but I'm curious why this testing method doesn't work?
Thank.
source to share
You call repeats
in Date
with day and month. Therefore, by law, this date will never be repeated until next year, and therefore, the notification will not be sent the next day. The trigger for your notification should only contain the hour and minute value, using DateComponents
to repeat each time:
var date = DateComponents()
date.hour = 11
date.minute = 41
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let content = UNNotificationContent()
let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)
source to share
Try the following line of codes:
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Title"
notificationContent.body = "Body Message"
var date = DateComponents()
date.hour = 11
date.minute = 30
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let notificationRequest = UNNotificationRequest(identifier: "\(NSDate().timeIntervalSince1970)", content: notificationContent, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error
{
let errorString = String(format: NSLocalizedString("Unable to Add Notification Request %@, %@", comment: ""), error as CVarArg, error.localizedDescription)
print(errorString)
}
}
source to share