I want to make an IOS app in which I want to run the app even the user has killed the app from the main thread. For example a Reminder or Alarm Clock app in which even it is not running but still we get the alerts. Can anyone tell me how to achieve this task in iOS.
You can't run if you have been killed. That is what it means to be killed. The user is in charge of what is running - not you.
But you can schedule a UILocalNotification so that the system will put up an alarm alert for you.
You can schedule local notifications (UILocalNotification) to popup in the future. The notifications can be repeating or one-shot.
If the user taps on the notification (or optional custom actions), the app will be restarted. If not, there is no way to keep your app running long term.
I have also read that an app can remain running in the background by playing a repeating silent audio file, but probably not Apple approved.
Related
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.
Here a very tricky situation: I'm writing an alarm app in swift. At the moment the alarm is fired the application will most likely be in background or killed or even worse the iPhone is locked. As soon as the alarm is fired the app should set another notification automatically while in background. I'm able to do this in AppDelegate only if the app is in foreground, but in background, I'm only able to set a notification with a nice attachment and sound, but no code is executed. I'm not able to call a method after a local notification was received in background with a locked device.
Help would be very appreciated. I'm not sure there is a solution. Thank you very much.
Reading didReceiveRemoteNotification it says that this method is called if your app is running in the foreground or background.
What you may find useful is
In addition, if you enabled the remote notifications background mode,
the system launches your app (or wakes it from the suspended state)
and puts it in the background state when a remote notification
arrives. However, the system does not automatically launch your app if
the user has force-quit it. In that situation, the user must relaunch
your app or restart the device before the system attempts to launch
your app automatically again.
So, I don't the think you can work around the killed scenario. But with background mode enabled you can at least wake the app from at most the suspended mode.
Read Apple's Background Execution article, especially the section Understanding When Your App Gets Launched into the Background. Where it says a background execution is when
A push notification arrives for an app and the payload of the notification contains the content-available key with a value of 1.
You may be able to do some or most of what you want in here.
I am working on application in which I want to run the background service after every 12 hours even the application is suspended or any state just like Facebook. And in that service, it will hit the webService and get the data. And when the data is changed after 12 hours, I will get the notification that this item is added, and when I tap that notification, i will move to the specific screen.
I have read on many forums that when the application is in background state, it will take only 3-4 minutes to terminate the app, and after that time, no event will occur.
But I have seen on Facebook, WhatsApp or any other social media app, even the app is in not running, or in suspended state, the app still gets the notification.
Please guide me what should I do.
This is not how it works.
The notifications you see from Facebook or WhatsApp apps are Push Notifications.
The app is not fetching information from the server. It's the server that "pushes" information to your user's phone.
Push notifications don't require your app to be active to work. And tapping on them will start your app and show the correct screen if handled correctly.
For running a task periodically, you can look into Background Fetch.
This works great, but iOS decides when to call it and can completely stop calling it (if the phone is on power saving or if your app is manually killed for example).
Keep in mind that in iOS, there is NO WAY to force the device to execute code if your app is suspended/killed. You might want to review your workflow.
I am building an app for a client that requires the ability for users to set an alarm time in the app, then be able to run the app in the background, then when the alarm time is hit display a notification.
I know Apple has some limitations on how long apps can run in the background, so what is the proper method for going about this?
I already know how to use local notifications, so showing a notification when the alarm is actually hit is not an issue. I just want to make sure I am able to enable the app to 'wake up' when the alarm time is hit and call the code for showing the local notification.
I don't believe that your app can wake itself up upon a notification, users wake your app up by interacting with the said notification.
However, you can keep your app running in the background by various methods so you don't have to worry about waking it up. One of these methods is to let the app pretend to be a music player and constantly play some sound clips(just make them silent to not disrupt the users), this way the OS won't terminate your app and will allow it to run in the background.
Doing so of course against Apple's guideline, is highly likely that your app will be rejected from the App Store unless you have very good reasons to convince the reviewers.
My company is right now developing an app that uses similar approach to keep on running, since its an in-house application we don't need Apple to review it.
I m writing ios app on 4.3 simulator and 3.2 xcode
The app every minute with timer NSTimer checks news on some websites and if there are news, the app will make alert message with sound "There are something new on ... website!"
But if i close app or it goes to sleep, timer stops on background. So, i have read many articles like
http://www.codeproject.com/Articles/124159/Hour-21-Building-Background-Aware-Applications
How keep NSTimer when application entering background?
and non of them helped me. my questions:
1) What technologies or tricks should i use to make such app? One good man told me that i need to use only Push Notifications.
2) If to use Push Notifications, do i have to get Developer Program $100/year from apple and stay my Mac book always turned on for creating server for Push Notifications?
thank you!
There are only a few approved uses for running in the background: music, navigation, VOIP. If your app doesn't do one of those things, it can only run in the background for up to 10 minutes, then it will be suspended by the OS.
Push notifications require a server. You either run it yourself, or you use a service like urbanairship.com. A push notification just shows up like an alert on the device. It doesn't make your app wake up, unless the user does it by responding to the alert.
Please read this:
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
If you haven't paid the $100, you can't run your app on a real device. I don't think the simulator properly simulates background states, so debugging background tasks is going to be pretty difficult.