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
}
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 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.
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)
What I want that, I set the local notification for 7:00 am and as its fire immediately, notification comes continuously till the user not perform any action on notification or open the app.
below is code to send notification first time
let alarmNotification: UNMutableNotificationContent = UNMutableNotificationContent()
alarmNotification.title = "Demo"
alarmNotification.body = "Test"
alarmNotification.categoryIdentifier = "myDemoCategory"
let now = Date()
let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute], from: now)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let request = UNNotificationRequest(identifier: "TestNotification\(now)", content: alarmNotification, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
Please suggest for continuous send local notification.
Thanks
In your date component add seconds here
let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute], from: now)
set number of notifications you want in loop for every second by incrementing seconds Max is 64 in iOS.
when notification is tapped dismiss all notifications and reset for next time.
I want to repeat local notification every week, before iOS10 there is repeatInterval, but i am not able to find anything suitable to repeat notifications in iOS10.
TimeTrigger and calendarTrigger both have repeat as true or false, where can i apply repeat as weekly, daily, monthly.
Thanks.
Try this.
func scheduleNotification(at date: Date, body: String) {
let triggerWeekly = Calendar.current.dateComponents([.weekday,hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Dont Forget"
content.body = body
content.sound = UNNotificationSound.default()
//content.categoryIdentifier = "todoList"
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)")
}
}
}