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)
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
In my app, a user will set date and time for reminder, and the phone needs to send local notification at that particular time even when the app is closed.
Able to send a notification at particular time using the below method. but it doesn't work when app is closed:
public func simpleAddNotification(hour: Int, minute: Int, identifier: String, title: String, body: String) {
// Initialize User Notification Center Object
let center = UNUserNotificationCenter.current()
// The content of the Notification
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
content.badge = 1
// The selected time to notify the user
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = hour
dateComponents.minute = minute
// The time/repeat trigger
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// Initializing the Notification Request object to add to the Notification Center
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
// Adding the notification to the center
center.add(request) { (error) in
if (error) != nil {
print(error!.localizedDescription)
}
}
}
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)
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 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
}