i would like to send a notification to my iOS application users just a day following the registration of the notification. In other words, i want to exclude the day of the registration of the notification in the UNUserNotificationCenter (I want to send the notification everyday from tomorrow on).
var dateInfo = DateComponents()
dateInfo.hour = 12
dateInfo.minute = 30
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "daily notification:", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Hello !Get up,", arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber;
content.categoryIdentifier = "Daily notification"
// Deliver the notification every day at 12:30 am.
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)
let request = UNNotificationRequest.init(identifier: "midnoonHalfNotification", content: content, trigger: trigger)
Calculate the time interval between now (when the request is created) and 12:30 tomorrow and use a UNTimeIntervalNotificationTrigger as the trigger with the calculated interval instead of the calendar trigger.
Related
I was trying to find an exact answer for the question: "what will happen to my scheduled notifications after the device is rebooted?".
I've used the UNUserNotificationCenter to schedule all the notifications and they will be triggered repeatedly on each day based on the scheduled time.
here is my written code snippet, and it works while the device is on.
func scheduleNotification() {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "This is the title"
content.body = "The is the body"
content.categoryIdentifier = "identifier"
content.userInfo = ["info":"B"]
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.hour = 0
dateComponents.minute = 29
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.removeAllPendingNotificationRequests()
center.add(request)
}
In short, it should remain, although I could not find an official apple documentation, it worked for me. The timer should survive a reboot. As long as the device is on when the relevant time arrives and the app is still installed, The notification will be fired. Of course permission for sending notifications is required.
This question was asked before with no answer - UNUserNotificationCenter notifications after device reboot
I am scheduling daily repeating notifications by setting only hour, minutes and seconds:
func addNotification(fireDate: Date)
{
let notificationCenter = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Some title"
content.body = "Some message"
content.sound = .default
content.categoryIdentifier = "MyCategory"
let calendar = Calendar(identifier: .gregorian)
let triggerDate = calendar.dateComponents([.hour, .minute, .second], from: fireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
let requestID = UUID().uuidString
let notificationRequest = UNNotificationRequest(identifier: requestID, content: content, trigger: trigger)
notificationCenter.add(notificationRequest) { error in
if let error = error
{
print(error.localizedDescription)
}
}
}
Now I would like to be able to cancel all notifications for current day, so I could receive notifications starting with tomorrow. I tried to subclass UNCalendarNotificationTrigger in order to change nextTriggerDate, but there is an error when adding a notification using my custom trigger class. Cannot find a way to achieve this, if I cancel a scheduled notification at certain time, I will not receive that notification anymore. Any ideas are appreciated.
I would like to send a push notification three times a day. Which should display various time-data in every notification. As example it should give the current time when it fires minus a constant. I know you can setup the fire-date constantly like this:
notification.repeatInterval = NSCalendar.Unit.hour
But how can i set the alertbody variable?
Thanks.
You can use UNMutableNotificationContent class and use title property to set the title text and body to set the notification body text. For e.g.
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",
arguments: nil)
// Configure the trigger for a 7am wakeup.
var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)
// Create the request object.
let request = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
I am setting up my local notification using the following code. The issue I am facing is that it triggers the "will present" delegate call back just after the execution of this code and don't wait for the interval. i.e. 2 minutes. then trigger again after 2 minutes as required. I am new to this.
let content = UNMutableNotificationContent()
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
let message = String(format: "No hit since %d minutes", notificationInterval!)
content.body = message
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(2 * 60), repeats: true)
let request = UNNotificationRequest(identifier: "liveSessionLocalNotification", content: content, trigger: trigger)
// Schedule the notification.
center.add(request) { (error) in
}
In my app, i want to add local notifications. The scenario will be that the user can select any time and days from Mon to Sun. For example, if the user selects Mon, Thur and Sat as days and time as 11:00 pm, so now the user should be notified at all the selected days and that particular time.
Code:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
// let test = UNCalendarNotificationTrigger()
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
I am using this code but this doesn't work according to what I need.
To get local notifications that repeat on a certain weekday at a certain time you can use a UNCalendarNotificationTrigger:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
// add notification for Mondays at 11:00 a.m.
var dateComponents = DateComponents()
dateComponents.weekday = 2
dateComponents.hour = 11
dateComponents.minute = 0
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
If you want notifications on Monday, Thursday and Saturday at 11:00 you need to add 3 separate requests. To be able to remove them you have to keep track of the identifiers.