How to run code at a certain time using LocalNotification iOS? - 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).

Related

Silent notifications in Background Mode on force-quit apps

My problem is: I just can't make silent notifications work when a user has force-quit(swiped away) the app!
I guess the following is a fact: A silent push (with content-available:1) will NOT trigger application(_:didReceiveRemoteNotification:fetchCompletionHandler:) nor any other method (it will NOT launch the app) if the application was force-quit (swiped away) by the user! Can anyone prove this wrong?
I have made sure I've enabled Background Mode: Remote Notifications.
But what if non-silent notifications don't work for me? I need silent ones, I need to be able to run some checks before I show it! What If I want to check if the right user is logged on to my application after I receive a notification from remote server? (since I can't guarantee that when he logged out he successfully let the server know about it, so I assume the server doesn't know for sure)
What would be the right approach to take in my situation?
There are many questions about similar things, but not many people involved, I wonder why? I don't believe that I have such a rare case. Maybe my basic approach to solving this kind of problem is wrong? It doesn't seem to be a problem on Android platform at all!
I am using FCM as central point of sending out notifications, so if you say that PushKit can solve my problems, too bad that FCM doesn't support VoIP certificates. But, I wonder, can PushKit really solve this? Or Apple just designed it this way that when a user force-quit an app, it means that this app must shut up altogether with its ability to push remote notifications?!
I don't consider this a duplicate of Firebase silent notification does not start up a closed iOS app because what I am asking here is what would be the solution if you want to check if the user to whom the notification is addressed for corresponds to the user logged in to the application? It can be considered duplicate if it turns out that there is absolutely no solution for this on iOS platform.
You ask:
Or [has] Apple just designed it this way that when a user force-quit an app, it means that this app must shut up altogether with its ability to push remote notifications?
Yes, this is how it is designed. App Programming Guide for iOS: Understanding When Your App Gets Launched into the Background says:
In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system. When password protection is enabled on the device, the system does not launch an app in the background before the user first unlocks the device.
Is not possible. when the app is in background or suspended modes, you will have 30 sec to do some stuff. But if user kill app manually func didReceiveRemoteNotification will never called.
Upd:
When an iOS device receives a silent notification, the system wakes your app in the background and calls the application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method of its app delegate. Your app has 30 seconds of wall-clock time to perform any tasks and call the provided completion handler. For more information, see Handling Notifications and Notification-Related Actions.

Swift - Is there a way to wake up my app at a certain time?

I'm currently trying to make an alarm clock app, and I have a problem, because I need to wake up the app if it's closed when the alarm time is up, and then do some things inside my app, before sending a notification. However, I'm not quite sure how to do this.
I've been thinking of scheduling a local notification to wake up the app, but as I understand it, the user has to interact with it for that to happen, which isn't what I want. I've been looking at background fetch, but if I'm correct, you can't explicitly tell the app when to do a background fetch. Are there any other ways of doing this?
You probably need a silent notification.
You can find a good description here.

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.

Background Capablity on iOS

What are the requirements and how would I go about implementing the Application Push Notification Service to trigger methods when the application is in the background, or when the phone is closed. Would this even be possible?
Thank you
No thats not possible.
When the app is in the foreground it will receive the push notification directly and can do whatever it wants to in response to that.
However when its not in the foreground the notification is displayed to the user (if they haven't disabled them) and/or displayed in the notification center (if they haven't disabled that). Your app will be brought to the foreground to execute if and only if the user selects the notification.
The application does not receive the notification directly if its not in the foreground.
Read the Apple Docs:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1
It boils down to lots of setup, and then implementing application:didReceiveRemoteNotification. You can read an in-depth example here. Part two goes into the actual application:didReceiveRemoteNotification implementation.

Resources