iphone XMPP App run background - iphone

I created a chat application using XMPP framework..when I quit the app(enter background mode) I want to receive the chat message..and also need to display the icon badge...How can I do this?

You can indeed run a XMPP Framework-based app in the background in iOS4 by calling it a VoIP app. (However, Apple will reject it from the App Store unless it also truly does VoIP).
You need to set the VoIP flag in your app's (appname)-info.plist file, and then in
(void)xmppStream:(XMPPStream *)sender socketWillConnect:(AsyncSocket *)socket
You'll need to set the socket stream flags to include kCFStreamNetworkServiceTypeVoIP:
CFReadStreamSetProperty([socket getCFReadStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
CFWriteStreamSetProperty([socket getCFWriteStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
Then, your app will be woken up briefly when a new XMPP message arrives. In your normal
(void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
handler, you would want to create a local notification for the message if you are backgrounded (you can keep track of background state via UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification). The local notification handler can set the application badge number, etc (just like you would for a push notification).
EDIT
Newer versions of the XMPP Framework (specifically, GCDAsyncSocket) now support a call to make this easier, so you can just have:
- (void)xmppStream:(XMPPStream *)sender socketWillConnect:(GCDAsyncSocket *)socket
{
// Tell the socket to stay around if the app goes to the background (only works on apps with the VoIP background flag set)
[socket performBlock:^{
[socket enableBackgroundingOnSocket];
}];
}

There are a limited number of programs that can run in the background without limit, these being VOIP programs, those that play music, and those that track the user's location. If you're not doing any of these legitimately then you're limited to ten minutes of background operation. Note that Apple will reject apps that try silly tricks like playing 'empty' sounds to keep the app live.
You can find info on running tasks in the background here:
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Your other option I would guess is to have the chat program operate by contacting a server, and to have that server queue responses when a user is offline then deliver them when they next log on. Not being a net programmer myself I don't know how feasible this is, but I think it's the only way to do this indefinitely if you're only offering text chat. The better option would be to make your application VOIP enabled using the guide above.
EDIT As of the release of iOS 5.0, it is also possible to get apps registered as Newsstand applications to download information while backgrounded, plus a bunch of other funky features that are also Newsstand only.
-Ash

In the latest XMPP Framework you don't need to modify framework files.
Just do this:
1. Add this to your connect method
#if !TARGET_IPHONE_SIMULATOR
{
self.xmppStream.enableBackgroundingOnSocket = YES;
}
#endif
2. Add voip key to your info plist file:

Related

XMPP framework - Location based app connected in background

I developed and app that is currently set as "Location based" so that it can run in the background indefenitely. It's not a trick i need it to do stuff when the user moves.
After some search I learned that this app will only execute code on the event of a location change, no timer whatsoever will be able to execute any code after 10 mins.
My app, while in the background, stays connected to XMPP for 50mins or so, it then disconnects. I would like the app to never disconnect, I looked at the framework but I can't find the place where to fix this behaviour. Do you know if it's possible to stay connected "indefinetly"? if so what settings do I have to change in the xmppframework.
thanks
[EDIT]
After 50mins the user is still connected to XMPP but it's disconnected of the XMPPRooms it was conected.
On another run it was connected for 3 hours, the time the iPhone is connected doesnt seem to be consistent.
To make sure your application can do send and receive keep-alive messages, it needs to be a voip app and use a voip socket. From Apple's Documentation:
Add the UIBackgroundModes key to your app’s Info.plist file. Set the value of this key to an array that includes the voip value.
Configure one of the app’s sockets for VoIP usage.
Before moving to the background, call the setKeepAliveTimeout:handler: method to install a handler to be executed periodically. Your app can use this handler to maintain its service connection.
As for the socket:
NSInputStream and NSOutputStream: For Cocoa streams, use the setProperty:forKey: method to add the NSStreamNetworkServiceType property to the stream. The value of this property should be set to NSStreamNetworkServiceTypeVoIP.

Disabling notifications from Bluetooth 4.0 devices

My app connects to a Bluetooth sensor then starts updating the UI based on the notifications sent from said device. I am having a problem with IOS automatically generating a large amount of notification pop-ups when the app is minimized, I think this is due to the frequency at which the sensor is sending data. So I am trying to figure out how to keep the user from being bombarded when they minimize the app. I am trying to tell the device to stop sending data, but I suspect that delegate method never gets called.
I have tried adding
[application cancelAllLocalNotifications]
to both
- (void)applicationDidEnterBackground:(UIApplication *)application
and
- (void)applicationWillResignActive:(UIApplication *)application
but still seem to have an issue, any ideas.
Thanks
You can use CBPerpheral::setNotifyValue:forCharacteristic: to start or stop getting notification from the said peripheral.
Another option is to use session backgrounding. For that you need to add the bluetooth-central backgrounding mode to the app's plist file. After that the app is going to receive the bluetooth communication events both in foreground and background and no notifications will be generated by iOS. If your app decides it needs a notfification, it can simply generate a local notification (tutorial).

Xmpp Gets disconnected After iphone goes idle for more than 10 mins [duplicate]

The XMPPFramework for iPhone is powerful. I have it up and running, but how do you keep the connection alive while in the background for more than 10 minutes? I would appreciate some more documentation/how-tos on how to accomplish this.
So the use case is simple and common: Joe is online and in the iPhone chat app. He leave the chat app and goes to Safari, plays a game, streams a movie, and does other stuff for 3 hours (or more). Joe wants to keep receiving messages during that time.
The example app allows Joe to receive local notifications in the background, but for only 10 minutes it seems. Here's the relevant code (I think). Thanks!
To help out other folks, to even get to this point, you need to the xmppstream property enableBackgroundingSocket to YES (it's done for you in the iphoneXMPP example project which you should copy) and in the appname-info.plist (i.e., iosChat-info-plist) file you need to add a new key/value pair. You should right click somewhere and "add row". You should choose for the key "required background modes" and then type in "voip". Xcode will detect that you mean "App provides Voice over IP services" after you press enter. This gets you 10 minutes of keeping the chat app open in the background (I think). But we want indefinitely, and I suspect that the answer lies in the method below. Am I just supposed to "reconnect" within this method or something,e.g., [self connect] (I have a connect method)?
- (void)applicationDidEnterBackground:(UIApplication *)application{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
if ([application respondsToSelector:#selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:600 handler:^{
DDLogVerbose(#"KeepAliveHandler");
// Do other keep alive stuff here.
}];
}}
10 minutes is the approximate time iOS allows you to stay connected. You can
also look at [app beginBackgroundTaskWithExpirationHandler], which will allow you
to request more time to finish a task. In order to remain connected 100%, you will
need to either add a voip, audio or location tag to info.plist (that is UIBackgroundModes).
Backgrounding will not work unless you add one of those tags. In addition, adding a tag
will allow you to remain connected, but the actual tag must be valid if you're submitting to the App Store. Apple will reject the app if there's not a real use-case.
To remain connected longer without a tag, you will need to resort to using some type of server, which maintains the connection and then uses push notifications to deliver messages.

iPhone background notifications

I'm making an iPhone app. I can receive UDP messages using AsyncUdpSocket. I want the app running in background, and when receive a message, an UIAlertView is displaying to the user, and he can enter in the app, or ignore the alert.
Is it possible to detect a message when the app is running in background.
Do I need something to execute my code in this method?
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
I saw lot of tutorials, with timer, but I don't need timer to wake up my app. Also I read that's it's possible while playing music, tracking position or using VOIP.
Do I need to play a fake song to keep my app running? or to do something like that?
This app is for security, for example if someone is touching or moving your motorbike/computer/whatever else, your iPhone can alert you and prevents from stealing.
I read other threads similar but didn't find an answer.
Thank you guys for giving me tips, or any help /sample.
You can't run in the background on a non-jailbroken phone without being in one of those three categories of app, and Apple’s really unlikely to approve your app if you use that facility for another purpose. UDP probably isn’t the best solution for this anyway—if your phone leaves the network that the other device (whatever it is) is on, it won’t receive the notification at all, whether or not it’s in the foreground. You’re probably a lot better off using the push notification API.
This seems like a perfect case to use Apple Push Notifications (APN). You app can register to receive the notifications and the phone will alert the user with any combination of badging, messages, or sounds. Sounds like you already have a server that is sending the UDP messages, so incorporating APN should be minimal. Especially if you use a third-party to send the push notification, such as Urban Airship. (I am not affiliated with them, but have used them on a large commercial project.)
By definition, local events are not triggered by receiving a message.

iPhone Dev: Can you have a program running in background respond to SMS in iOS4+?

Can you have a program running in background respond to SMS? I basically want my program to sit in the background so the will be iOS4+ and when someone text messages you, the app can then do something with that text.
Can this be done?
I have seen that the question has been asked before, but it was before the iphone could have background apps.
No backgrounding is very limited on IOS. There is only a handful of stuff you can do. You can always send push notifications to get an alert while the app is not running in the foreground.
From Apple:
OS 4 delivers seven new multitasking services that allow your apps to
perform tasks in the background while
preserving battery life and
performance. These multitasking
services include:
Background audio - Allows your app to
play audio continuously. So customers
can listen to your app while they surf
the web, play games, and more. Voice
over IP - Your VoIP apps can now be
even better. Users can now receive
VoIP calls and have conversations
while using another app. Your users
can even receive calls when their
phones are locked in their pocket.
Background location - Navigation apps
can now continue to guide users who
are listening to their iPods, or using
other apps. iOS 4 also provides a new
and battery-efficient way to monitor
location when users move between cell
towers. This is a great way for your
social networking apps to keep track
of users and their friends' locations.
Push notifications - Receive alerts
from your remote servers even when
your app isn't running.
Local notifications - Your app can now
alert users of scheduled events and
alarms in the background, no servers
required.
Task finishing - If your app is in
mid-task when your customer leaves it,
the app can now keep running to finish
the task.
Fast app switching - All developers
should take advantage of fast app
switching, which allows users to leave
your app and come right back to where
they were when they left - no more
having to reload the app.
Nope. The only way for your application to be "called" when it's not already open is by sending a push notification.