What will happen to Scheduled notifications after the device Reboots? - ios

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

Related

How to schedule IOS UserNotification by excluding today

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.

Can a local push notification have variable content?

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)

UNNotificationRequest with repeat notification trigger option triggers at the start

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
}

How to open app inside didDetermineState Method

Today I am able to launch a local notification when the mobile gets into the region by using the didDetermineState just like the code bellow, even If the app is not running at all.
if (state == CLRegionState.inside){
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let content = UNMutableNotificationContent()
content.title = "Sonda"
content.subtitle = "Entrada"
content.body = "Hora: " + String(hour) + ":" + String(minutes)
content.badge = 1
content.sound = .default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
let requestIdentifier = "reqIdentifier"
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
//handle error
})....
Is it possible to launch the app itself within the same method without user interaction?
Thanks in advance,
Filipe
Sorry, but no, you cannot launch an iOS to the foreground programmatically. The design of iOS forbids this, as it always requires a user to gesture to allow an app to cone to the foreground. The closest you can come is sending a local notification as you have done. Tapping the local notification is the simplest and most user-friendly way to get that required user gesture.

How can I set the Local notification which fire every seconds after first set notification?

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.

Resources