How to open app inside didDetermineState Method - ios

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.

Related

What will happen to Scheduled notifications after the device Reboots?

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

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 show multiple local notifications

Background:
Im writing an application where a bot sends you messages. These messages can be received as local notification.
The Problem:
When the bot sends multiple notifications within a short span of time (1 second between each message), the notification center will only show one message. I will hear the notification sound every time I expect to, But i will still only see the first message.
Relevant code:
func postUserNotification(content: String, delay: TimeInterval, withDictionary dictionary: [String:String] = [:]) {
let notificationContent = UNMutableNotificationContent()
notificationContent.body = content
notificationContent.userInfo = dictionary
notificationContent.categoryIdentifier = "message"
let dateAfterDelay = Date(timeIntervalSinceNow: delay)
let dateComponents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateAfterDelay)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let identifier = "identifier" + "\(NotificationManager.incrementor)"
let localNotification = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(localNotification){ (error : Error?) in
if let theError = error {
print("the error is \(theError.localizedDescription)")
}
}
}
Nothing wrong with your code :
Like you wrote in your question, this is mentioned in the Apple Docs:
If you are sending multiple notifications to the same device or
computer within a short period of time, the push service will send only
the last one.
https://developer.apple.com/library/content/technotes/tn2265/_index.html#//apple_ref/doc/uid/DTS40010376-CH1-TNTAG23

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.

How to update the daily local notification message in iOS 10?

I'm trying to find out how to update the message in the local notifications when it is repeated daily.
Currently I have the following code in my AppDelegate:
func scheduler(at date: Date, numOfNotes: Int)
{
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
content.badge = numOfNotes as NSNumber
content.body = "REMINDER: " + String(numOfNotes) + " needs to be looked at!"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "reminderNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request) {(error) in
}
}
I am storing numOfNotes in UserDefaults. I have a UISwitch in my UITableViewCell, that upon being switched on calls the scheduler function like so:
func remindMeSwitch(_ remindMeSwitch: UISwitch)
{
numOfNotes = UserDefaults.standard.integer(forKey: "Notes")
let delegate = UIApplication.shared.delegate as? AppDelegate
delegate?.scheduler(at: time, numOfNotes: numOfNotes)
}
However, when setting the repeats parameter to true to have the notification repeat daily at the specified time, numOfNotes is only called once, which is when I toggle the UISwitch on.
How can I set the notification to alert daily but still be able to update the notification message as needed?
Thanks.
In general, you don't have any possibility to change Local Notifications.
Only one way - delete/cancel the old notification and create the new notification. But you can use a copy function.
For example, if you want to change the notification's content:
// create new content (based on old)
if let content = notificationRequest.content.mutableCopy() as? UNMutableNotificationContent {
// any changes
content.title = "your new content's title"
// create new notification
let request = UNNotificationRequest(identifier: notificationRequest.identifier, content: content, trigger: notificationRequest.trigger)
UNUserNotificationCenter.current().add(request)
}

Resources