Keep program running in background ios - ios

I am working on an application which allows to send messages to another user.
If I press Home Button app goes first in background and then in suspended state so I don't receive any message unless I bring app back to the foreground.
How can I fix this to receive messages even if app is in background or suspended state? Solution is to avoid app to go suspended?
Some ideas please
Thanks and regards

For that you have to use Push notifications as they will wake your app when a notification is received (in your case a message is sent to your app). Do you have this implemented?
Another Dirty way would be to keep a silent Music file playing when in background state thus the app nevel sleeps (unless terminated by user). Nut keep in mind that this trick wont let you publish your app to the app store!
Hope this helps!

Use Voip to wake the app upon receiving push notification then handle it and pull from server as you want
migrate-to-ios-voip-push-notifications

Related

Execute code when app in background and a Local Notification fires

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.

Notify the app about remote push notification when the app is not running (terminated)

When app is not running (terminated NOT in background) and a remote push notifications is received, is there any way to inform the app about it so that the app can update something locally such as simple int counter?
I want to store something so that when the app is launched the next time, app knows that notification was received when app wasn't running and something needs to be done.
If user launches an app by tapping on a notification, obviously the app is notified about it through AppDelegate methods but these methods are never called if user launches an app by tapping on the app's icon.
To be aware of notification when user launches app by tapping on icon, i need some way to let app know that notification was received when app was in background.
There is no way you can achieve this with simple push notifications.
According to apple docs if the user has manually killed your application by swiping it out of memory, your app will never be started in the background to process data until after the user chooses to launch it again.
One solution to this problem is using VOIP push.
According to apple docs -
Your app is automatically relaunched if it’s not running when a VoIP
push is received.
But you need a strong reason for using it and apple may ask that before approving your app on the app store.
To read more about VOIP push please go through this doc - https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html
You can also be used the "Silent Push Notifications".Which are confirming that their is something available on the server, which you need to download to your app,The payload format of the Silent push notification is like
{ content-available:1 }
The Best part of the silent notification is that they do not notify the iPhone user
Here 1 is for their is something available to download from the server.
here below i have attached the apple's silent notification slide.
Your App is getting Refresh in the Background.

Back to iOS application automatically when it's suspending in background.

I have a question,
I work on a Swift 3 project with Xcode.
My goal is to send an alert sms, 20 secondes after receiving a local notification. when I'm in application, every things work well, but when my app is in background, it can do other tasks, like print some character, or play music, or send a text to a socket server, but it can't send the sms.
I think, when I receive an alert, if I could back to my application automatically, then I can send an alert sms, that's why I'm doing this stupide idea and looking for a way to know how I can wake up automatically my application and go back to the first View Controller Screen when I receive a Local Notification?
Maybe there is someone who knows a method to return to the iOS application automatically?
When your app is launched into the background—usually to handle some type of background event—the launch cycle changes slightly as given in the picture below. The main difference is that instead of your app being made active, it enters the background state to handle the event and may be suspended at some point after that. When launching into the background, the system still loads your app’s user interface files but it does not display the app’s window.
Reference - https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html
Bringing your app from background to foreground is not possible. And even if you are able to do so, App store will reject it.
Hope it helps!

iOS 10 Remote Notifications When App Not Running

When I receive a remote notification I updated a counter that I save to UserDefaults and I also show a local notification. Everything works as expected when the app is in the foreground, background, and suspended states Ref. When the app is in the Not Running state my counter is not updated nor is my local notification shown.
It is my understanding that I should be able to receive and process Remote Notifications while the app is completely off. A few articles online claim that when a Remote Notification arrives while in the Not Running state that the application:didFinishLaunchingWithOptions: should be called followed by application: didReceiveRemoteNotification:fetchCompletionHandler: but in my case it is not.
Is it possible to receive remote notifications while in Not Running state?
If your app is a VoIP app and you are using VoIP pushes through PushKit then a push notification will launch your app from the terminated state in order to deliver the notification. If you are using standard push notifications then your app will not receive the notification if it is terminated. You can include an alert text with a 'silent' notification that will be displayed to the user in the event that your app is terminated in order to prompt them to launch your app.
First of all, it sounds like you have a silent notification set up. Once you add alert data to your push notification (information like the title, body etc.. you can find more on that here), it'll start to display on the lock screen.
Second, it's not possible for your application to launch from a push notification, silent or otherwise, if it's in the Not Running state. The documentation on this is actually incorrect, as it states that the application will only not be launched if the user has quit the app. However, this actually also applies for any circumstance under the not-running state, for example if your app has never been launched since installation/rebooting, or if it was quit due to memory (a fairly common occurrence - iOS purges apps which haven't been run recently as required).
did you check this mark when app is background?

Silent Push notification when app is killed

I want to update status/flag without calling web service, so i implemented silent push but if app is killed then silent push will not updating my status/flag. Please tell me the other way to handle this situation.
If your app is killed, there is nothing you can do about that situation. Push will come through, but it won't automatically start the app and if its a silent push, well then you have zero chance of getting that app to start back up.
You can do one last call to a web service just before the app gets terminated - but you have seconds to do it, so not ideal.

Resources