distinguish whether app was launched through the UILocalNotification - ios

I am working on a alarm clock app and I've been stuck for the last couple of days. I was hoping you guys could help out.
My question is;
Is there any way to distinguish whether the app was opened through the UILocalNotification alertAction or just opened regularly?
I want to use the UILocalNotification to create a snooze feature, so if the app is opened by the user tapping on the action button on the LocalNotification, a snooze mechanism will launch (but obviously i don't want that to launch if the user regularly opens the app to set the time or something)
Thank you so much for your help!

Yes this is possible as described in the UILocalNotifaction class reference
In the
application:didFinishLaunchingWithOptions:
method the application delegate can
obtain the UILocalNotification object
from the passed-in options dictionary...
Therefore you can check the options dictionary to see if the notification is present. If it is then that's how the app was launched.

Related

How to run code at a certain time using LocalNotification iOS?

I am building an app that sends a notification at a certain time and I need it to run some code at that time when the app is closed. Currently I have no problem showing the notification even when the app is closed with UILocalNotification. How am I able to make the notification run a function when it fires (not when the user taps on the notification) even when the app is closed?
I'm using Xamarin.iOS if that helps, but even non-xamarin answers are welcome.
UNUserNotificationCenterDelegate doesn't offer a way for the backgrounded app to be told when a user received a notification. It only calls your backgrounded app if the user interacts with the notification (including if they simply dismiss it).
Now there may be other ways to achieve what you want. Check out PushKit_SilentPushNotification and let us know if it worked out for you. It claims to even work when your app is killed. (I found this via a quick search today, so there are likely other solutions out there too - no idea if Apple would approve or not but I'd certainly be concerned).

UILocalNotification in background mode

I had implemented the code regarding UILocalNotification in my app and so far it is working very well for every case except one case.The problem i am facing is when the user receives local notification and the app is in background, then user open the app by pressing app icon,in this case i am not getting any method to provide me notification data.
All other cases i had managed in DidReceiveLocalNotification and DidFinishLaunchingWithOptions.
Please someone tell me the proper way to sort this problem out.
please check this Link :
getting local notification while the app is in background

how to start my App in a particular time of the Day in Swift iOS

I have an App built on Swift, I want this App to start every day at some particular time. Logically its like Calendar notification, which gives notification in that particular window whatever we set.
Does is the same scenario is possible with an App in iOS Swift.
What you can probably do is to create a local notification, but this is not opening your app. A local notification is just a way to show a notification on your iPhone and then, if the user taps, it's opening your app.
See more here: https://www.codebeaulieu.com/49/How-to-add-local-notifications-to-your-app
I am not sure what you want to do, you cannot force your application upon the user without the user's consent. What you can do is schedule a local notification so the user knows when to open your app like jomafer proposed already. Also possible is to wake up the app to do stuff in the background:
https://developer.apple.com/library/prerelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
For example a silent push notification that will trigger some code, or a scheduled background download.
NONE OF THESE METHODS ARE 100% RELIABLE THOUGH!

Cancel Local Notifications after app is unused for a certain period of time

I am wondering if it's possible to do what the titles says. I have an application that has a refill reminder to refill your prescription drug via local notifications. I have seen that some apps (pill reminder apps mostly) push a notification if you have not taken your pill, or have not answered back to that notification, and was wondering if I can do the same if a user doesn't open/interact with the app after a certain period of time.
I have not began implementation but have thought about this thoroughly. What I am thinking of doing is having some sort of flag when the app is opened that removes that local notification and sets a new one once the app has gone in the background/inactive. The local notification would be set to three months from when the app has gone in the background/inactive. The question then becomes, how do I handle canceling all notifications after this notification has been received, regardless of whether the user opens the app at that notification or not?
If the user opens the app on that notification, I can have a check the method application:didReceiveLocalNotification and then handle the case where that local notification has been set and then use [[UIApplication sharedApplication] cancelAllLocalNotifications]
But if the user does not tap or open the app, how can I check and cancel all local notifications?
Sorry if this is a bit long or worded weirdly (sorry not good with words and explaining things). Let me know if you need more info or better explanation. Thanks in advance!
If I understand your question correctly, you want to know how to keep notifications from repeating when the user does not respond to a notification by opening your app.
You might consider configuring your local notification not to repeat. Instead, you might reschedule notification batches each time the application is launched.
Alternatively, if your application has a server-side component, you can use push notifications on iOS 7+ to wake your app, briefly. There is no equivalent to this behavior using UILocalNotification.

UILocalNotification handle when opened via App icon?

I'm working with UILocalNotifications for the first time. Mostly working with repeating notifications. Most all makes sense, except one thing.
Apple Documentation states several cases for handling local notifications when they fire.
First, a case for when the user "taps the notification" when outside of the App:
If the notification is an alert and the user taps the action button
(or, if the device is locked, drags open the action slider), the
application is launched. In the
application:didFinishLaunchingWithOptions: method the application
delegate can obtain the UILocalNotification object from the passed-in
options dictionary by using the
UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can
inspect the properties of the notification and, if the notification
includes custom data in its userInfo dictionary, it can access that
data and process it accordingly.
It also states a case for what happens when the user is inside the App:
If the application is foremost and visible when the system delivers
the notification, no alert is shown, no icon is badged, and no sound
is played. However, the application:didReceiveLocalNotification: is
called if the application delegate implements it. The
UILocalNotification instance is passed into this method, and the
delegate can check its properties or access any custom data from the
userInfo dictionary.
In both of those cases the developer can access the uilocalnotification and then decide what to do with it. However, in a third case - when the user, outside of the App, sees and ignores the notification and then later launches the App, no method is called that allows the application to know which notifications have previously fired?
At first I thought that this statement was describing that behavior, but now I am not sure:
On the other hand, if the local notification only badges the
application icon, and the user in response launches the application,
the application:didFinishLaunchingWithOptions: method is invoked, but
no UILocalNotification object is included in the options dictionary.
How can I handle the third case? How can I know which local notifications have fired? Do I need to iterate through my list and check all their times myself? Is there a better way to accomplish this?
You need to keep track of what is happening with your notifications. What I mean with this is that, because the notification has fired, and the user didn't launch the app because of a notification nor was your app running at the time of the notification, you need to check your sources to verify if a previously scheduled notification fired date has already passed.

Resources