Reputation: 3819
I'm trying to test local notifications in iOS 10 by trying to trigger a daily notification.
I am using the following sample project: NotificationsUI-Demo
In the app is one of the following code:
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 of repeats
from false to true to repeat daily because according to the documentation describing the repeats
parameter:
Specify false to unschedule the notification after the trigger fires. Specifying true causes the notification to be rescheduled repeatedly.
Let's say I trigger a notification for today 4/10/2017 at 6:56pm. At 6:56pm, I see a local notification as expected.
When I try to manually change the date & time via Settings->Date & Time to 4/11/2017 at 6:55pm, once 6:56pm rolls around, I do not see a notification at all.
Can it not be tested this way?
I have not tried to actually wait the next day at the specified time to see if another notification pops up, but I'm curious as to why this way of testing does not work?
Thanks.
Upvotes: 4
Views: 2681
Reputation: 2026
You are calling repeats
on a Date
with a day and month. Therefore, this date will not occur again until next year. Thus, no notification will be sent the following day. The trigger for your notification must only contain an hour and minute value using DateComponents
to allow the notification to repeat every day:
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)
Upvotes: 9
Reputation: 1328
Try these 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)
}
}
Upvotes: 1