I have a function with notification with the time interval trigger, which shows the notification once a minute
func addNotificationWithTimeIntervalTrigger(){
callApi()
let content = UNMutableNotificationContent()
print("PresentTitle",AppDelegate.titleNotification,"\(Date())")
content.title = AppDelegate.titleNotification
//content.subtitle = subTitle
//content.body = body
content.badge = 1
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats:true)
let reguest = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(reguest) { (error) in}
}
But it shows only one, the old value once a minute, although the data in API has already changed.
To see the new value, i need to press the button again, but I do not want that, i want to know how to rewrite this func that this (timeInterval: 60, repeats:true) includes callApi() or smth like that.
You can use NStimer for this .
var timer = NSTimer.scheduledTimerWithTimeInterval(60.0,
target: self,
selector: Selector("addNotificationWithTimeIntervalTrigger"),
userInfo: nil,
repeats: true)
Related
I have this code below to test how local notification works every hour. But im not getting anything.
Also, is there a way to send different local notifications messages in different times? and im just calling the LocalNotificationHour() in viewDidLoad()
I just started learning swift, so im sorry in advance.
--
#objc func LocalNotificationHour() {
let user = UNUserNotificationCenter.current()
user.requestAuthorization(options: [.alert,.sound]) { (granted, error) in}
let content = UNMutableNotificationContent()
content.title = "Local Notification"
content.body = "This is a test."
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let uuid = UUID().uuidString
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
user.add(request) { (error) in print("Error")}
}
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
This basically takes todays date and sets the time to 1am. Using: UNCalendarNotificationTrigger(dateMatching: you are telling the notification to trigger at 1am today and then repeat at the same time each day.
To trigger a notification based on a time interval you should use the UNTimeIntervalNotificationTrigger.
// Fire in 60 minutes (60 seconds times 60)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (60*60), repeats: false)
You can schedule your notification for every minute by adding following code:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (success, error) in
if error == nil, !success {
print("Error = \(error!.localizedDescription)")
} else {
let content = UNMutableNotificationContent()
content.title = "Local Notification"
content.body = "This is a test."
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let uuid = UUID().uuidString
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
I've created a function which will send the user a notification at a specific hour and minute with specified text. The problem is, when I call the function twice (trying to send two notifications), it only runs the second notification. Essentially, I need to be able to call the function multiple times to get multiple notifications at different times. My guess is calling it a second time overwrites the prior. Anyway I can fix/accomplish this? Here's my code:
func notification(hour: Int, minute: Int, text: String){
let content = UNMutableNotificationContent()
content.title = "Example"
content.body = text
content.sound = UNNotificationSound.default()
var date = DateComponents()
date.hour = hour
date.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "testIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
notification(hour: 12, minute: 5, text: "It is 12:05.")
notification(hour: 12, minute: 10, text: "It is 12:10.")
}
It's because the same identifier
let request = UNNotificationRequest(identifier: "testIdentifier", content: content, trigger: trigger)
so change it for every scheduled notification
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 trying to make an alarm app by setting new local notifications every X seconds when a Set button is pressed and stopping when the user taps a Stop button.
using Timer(), i am currently able to set the local notification but it doesn't repeat as I would like. it only repeats when i go into the app and back to the previous screen via the Home button. also, it only repeats once by doing that.
is there any way to have the user press the Set button once and have the notifications keep firing no matter if you're in the app/outside the app and only stopping when the user presses the Stop button?
my code as follows:
class LocalNotificationViewController: UIViewController {
var timer = Timer()
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(self.setLocalNotification)), userInfo: nil, repeats: true)
}
#objc func setLocalNotification() {
// timeEntered = time.text!
// let hrMin = timeEntered.components(separatedBy: ":");
let content = UNMutableNotificationContent()
content.title = "Test Local Notification"
content.subtitle = "Testing..."
content.body = "Testing..."
content.badge = 1
content.sound = UNNotificationSound(named: "alarm.aiff");
trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false);
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
var timeEntered: String = "";
var trigger: UNTimeIntervalNotificationTrigger? = nil;
#IBOutlet var time: UITextField!;
#IBAction func setNotification(_ sender: Any) {
runTimer()
}
#IBAction func stopRepeat(_ sender: Any) {
timer.invalidate()
}
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
}