I have an iPhone chat app using XMPP with an EJABBERD server that is setup to function correctly if the app goes into the background (by either hitting the home button or from lack of activity and then the screen goes black) and then continues to function correctly when brought to the foreground.
The app works fine on an iPad when going into the background and back to the foreground, but when I run the app on an iPhone 4S, about an hour after the app has gone into the background, the app goes Offline from the XMPP server. Then when the iPhone 4S goes back into the foreground, the app is no longer connected to the XMPP server. In contrast, on the iPad the app has been running in the background for over a day and is still Online with the XMPP server.
I saw this SO post, but since the app has been running in the background on my iPad for over a day, and the app on the iPad is still Online with the XMPP server, I don't think that is the problem.
1) Is this because the iPhone 4S goes into Airplane mode after some time?
2) If the answer to 1) is yes, is there a way for the app to programmatically stop the iPhone 4S from going into Airplane mode?
3) Or is something else going on?
Update 1 -
The interesting thing is that the iPad can go offline on the XMPP server, but when the app on the iPad is brought to the foreground, XMPP works fine. But if the app on the iPhone 4S goes offline on the XMPP server, then when the app on the iPhone 4S is brought to the foreground, XMPP no longer works. Also the iPad goes offline quickly on the XMPP server, after 20 minutes or so, whereas the iPhone 4S takes a few hours to go offline. Maybe a different method of going offline is occurring between the iPad and the iPhone 4S?
I've tried playing around DEFAULT_KEEPALIVE_INTERVAL in XMPPStream.h as mentioned in this SO post, without success so far. In XXMPStream.h I have the code:
#if TARGET_OS_IPHONE
#define MIN_KEEPALIVE_INTERVAL 20.0 // 20 Seconds
#define DEFAULT_KEEPALIVE_INTERVAL 120.0 // 2 Minutes
#else
#define MIN_KEEPALIVE_INTERVAL 10.0 // 10 Seconds
#define DEFAULT_KEEPALIVE_INTERVAL 300.0 // 5 Minutes
#endif
Update 2 -
The other strange thing is that while the app is in the background on the iPhone 4, if the app on the iPad wants to chat with the iPhone 4, the iPad sends a Push Notification to the iPhone 4. This Push Notification brings the app on the iPhone 4 to the foreground, where the iPhone 4 tries to reconnect to the EJABBERD server but the iPhone 4 is no longer able to reconnect.
Update 3 -
The iPad only has Wifi whereas the iPhone 4 has Wifi and 4G cellular connections. So I thought maybe that is the difference. So I put the iPhone 4 into Airplane mode, and then connected with the Wifi, so the iPhone 4 was only using Wifi. But after 2-3 hours the iPhone 4 went offline with the EJABBERD server again.
I finally found one solution to the problem via this SO post. Even though the iPhone 4 goes Offline from the EJABBERD server, when the app comes to the foreground, the app can reconnect to the EJABBERD server whereas previously the app couldn't reconnect.
Specifically I used the code
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
// it's better to move "dispatch_block_t expirationHandler"
// into your headerfile and initialize the code somewhere else
// i.e.
// - (void)applicationDidFinishLaunching:(UIApplication *)application {
//
// expirationHandler = ^{ ... } }
// because your app may crash if you initialize expirationHandler twice.
dispatch_block_t expirationHandler;
expirationHandler = ^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
};
bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// inform others to stop tasks, if you like
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyApplicationEntersBackground" object:self];
// do your background work here
});
}
Related
My BLE device remains connected with the application when I put my application in the background.
I want to disconnect BLE communication if my application will be in the background for more than x seconds. (Example : x = 60 seconds)
I have tried to use Timer but it gives me an event when I came back to the foreground. I want to disconnect the BLE when the application is in the background.
Please let me know if anyone has faced & solved this expectation.
UILocalNotification is not coming when i have supervise my iPad in single app mode. Same code is working fine when device is in normal mode when app is in background/killed or any other state.
But after Supervise device in single app mode with same app Notifications is not coming.
In my case app will never gone in background but may be device in sleep mode.
I am trying to get a notification on iPhone when the connection is lost from my Apple Watch.
My first approach was to check the session.paired==true which works fine when the application is on foreground. But I would like to receive that event when the app is on background.
Then I thought about the Background Modes of the iOS and that there is a mode for external-accessorythat will send a notification on the device when is connected or disconnected. Is there something similar for watchOS?
My other idea is again with background modes and Bluetooth. Can you detect the watchOS as bluetooth?
Did anyone solve this issue?
IOS 8 stops background call after 15 min.
I am using background service to send gps to service every 2 mins. app sending data properly for 15 min after that its stops sending data. this happens only in ipad mini, in rest of ipad its working fine, data going properly eben for 8 hours.
iOS 8 background service ends after 15 to 20 mins. I am testing in iPad mini, but its working fine in normal iPad, I had enabled background app refresh, and everything fine. but its not working after 20 min, but same code working fine in other device except iPad mini, I am using iPad mini cellular ? kindly help me out.
The usual cause for this is an app crash. I had it happen when my background refresh ran over the allowed 30 seconds. Then the app appears to still be running when you reactivate it, but in fact it's restarted.
Check the device logs and see if you've had crashes like this. Given the date, you probably already solved your problem.
Also remember that iOS calls for BG refresh when it wants, and it's very idiosyncratic.
I have developed an application where the user can select from a few custom notification sounds to sound. These notifications will either be shown in-app through a simple AVPlayer or through a UILocalNotification when the app is not in the foreground. The foreground alert ofcourse always works. The background alert did not work on one of my testers' iPhone 4S.
At first I thought it was a configuration problem on his phone but we couldn't find anything. When I started digging deeper into the problem I discovered he was running iOS 7.0.4. I tried to run the application on an emulator running iOS 7 instead of iOS 7.1 and I did not have any sound with a UILocalNotification neither. When I tried iOS 6.1 in my simulator there were no problems at all again. My hardware device running iOS 7.1 did not reproduce the problem either. The message and logo always show no matter what version, it's just the sound that's missing.
Then I thought the problem might be related by the fact that it wasn't running in the foreground thread of the app. performSelectorOnMainThread:withObject:waitUntilDone: did helped neither.
This is the relevant code:
+ (void)minimizedAppAlarm:(Settings *)settings;
{
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.alertBody = NSLocalizedString(#"WAKE_UP", #"Wake up!");
if (settings.playSound)
{
notification.soundName = #"somefile.mp3";
}
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
It seems it has to do with setting the badge number using the following call:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:number];
Currently I am not sure what exactly happens but I do a countdown from ten to zero when the app is minimized and perhaps setting the badge to zero just before the alarm it blocks.
I have been running my app without these lines of code and it's firing the background notifications in every version. Note that I have been testing this app on lower version in the simulator and I did need to do the following before I noticed any changes
Remove the app from the home screen
Reset contents and settings
Since the simulator does not have any settings for notifications I guess somehow 7.0 sets the notifications muted if I used setApplicationIconBadgeNumber. For now I can continue finishing the app without the countdown feature but I will continue testing so I know for sure I only need to disable iOS 7.0 or the problem exists on most versions if I start a version of the app that directly changes the badge number.