Receive chat information in ios xmpp development app background Mode

Source: Internet
Author: User

Recently, xmppframwork is used to implement a chat application. When the application enters the background, it cannot receive the message;

When the application is switched to the backend, the network connection in the application still receives data and keeps it alive (like QQ iphone). The following is a step-by-step analysis.

There are three situations for the status of iOS apps:

1) if the iOS application is active, the client and the server receive and send messages after a persistent connection with the server.
2) If the iOS application exits and the persistent connection is disconnected, the server sends messages to the client through APNS.
3) if the iOS app has just been switched to the backend and has not been closed, the persistent connection is not closed, and the server still sends messages to the client through the persistent connection, however, iOS apps receive messages only when they are switched back to the front-end.


In the third case, you may have the following questions:

When the iOS app is switched to the background, the connection remains. Can the client receive messages from the server in the background and then notify the user with local notifications? What should I do if I can? Or should APNS be used to notify users once switched to the background?


Combine the Background information in the App Programming Guide
The Execution and Multitasking chapter and satckoverflow's answers to the xmpp support for background socket problems, and the following solutions are obtained:

1. because apple only supports a few types of application code that can be run in the background (such as audio, location, voip, etc.), you need to (appname)-info in the application. add a "Required
The key of background modes. Set the value to voip to specify the type.

2. ios xmppframwork already supports this feature. You only need to make the following settings when initializing xmppstream:

// Enable background mode (note that background socket is not supported on ios simulator) xmppStream. enableBackgroundingOnSocket = YES;


The following is a test to see how it works.

Add the following code in the-(void) xmppStream :( XMPPStream *) sender didReceiveMessage :( XMPPMessage *) message method:

// The program runs on the frontend, and the message is displayed normally if ([[UIApplication sharedApplication] applicationState] = UIApplicationStateActive) {} else {// if the program runs in the background, when a message is received, the UILocalNotification * localNotification = [[UILocalNotification alloc] init]; localNotification is displayed as the notification type. alertAction = @ "OK"; localNotification. alertBody = [NSString stringWithFormat: @ "From: % @ \ n % @", @ "test", @ "This is a test message"]; // notification subject localNotification. soundName = @ "crunch.wav"; // The localNotification of the notification sound. applicationIconBadgeNumber = 1; // Number of tags [[UIApplication sharedApplication] presentLocalNotificationNow: localNotification]; // send notification}

Real machine test: After logging on to the iphone, switch to the background status, and then send a message to the iphone using spark on the computer. The status bar displays the notification message, which is basically successful.


Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

Note:

When I checked the information online, I found a problem. If your application does not implement VoIP, if you follow the above method, there is a risk of Apple reject; then, when the application enters the background or exits, the server sends a message to the user based on the changes in the user status, and uses Apple apns to push new message notifications to the user; however, we know that Apple's apns is not guaranteed in terms of timeliness and reliability. That is to say, the notifications of new messages are sent to Apple, and Apple cannot guarantee that the notifications can be pushed to you successfully or in real time, so you can only check whether there are any third-party free push service providers.

Google finds a free push called Aurora push, which is under research and is being updated...


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.