iOS XMPP Development app background mode receive chat messages [go]

Source: Internet
Author: User

Recently in the use of xmppframwork to implement a chat application, encountered a problem, the application into the background, will not receive the message;

How to get the app to be cut into the background, the network connection in the app still receives data and remains alive (like QQ iphone), which is analyzed step-by-step.

There are three scenarios for the status of iOS apps:

1) If the iOS app is active, just keep a long connection with the server, and the client and server send and receive messages through this connection.
2) If the iOS app is out of status and the long connection is disconnected, the server sends a message to the client via APNS push message implementation.
3) If the iOS app has just been cut into the background and has not been turned off, it is found that the long connection is not disconnected, the server sends a message to the client via a long connection, but the iOS app can only receive messages when it is re-cut to the foreground.

For the third case, there are the following questions:

When the iOS app has just been cut into the background, the connection remains, can the client receive a message from the server in the background, and then notify the user with local notifications? What should I do if I can? Or will it be necessary to notify the user via APNs once it has been cut into the background?

Combined with the section on background execution and multitasking in Apple's Official Document App Programming Guide and Satckoverflow's answers to XMPP support for the backend socket problem, The following workarounds are obtained:

1. Because Apple supports only a handful of our app's code can be run in the background (for example: Audio, location, VoIP, etc.), so in the application (appname)-info.plist file, you need to add a "Required Background modes "Key, the value is set to VoIP, to specify the type.

This support is already available in 2.ios Xmppframwork, as long as the xmppstream is initialized with the following settings:

[CPP]View Plaincopyprint?
  1. Allow background mode (note that background sockets are not supported on the iOS emulator)
  2. Xmppstream.enablebackgroundingonsocket = YES;

Here's a test to see how it works.

First in-(void) Xmppstream: (Xmppstream *) sender Didreceivemessage: (Xmppmessage *) in the message method add the following code:

[CPP]View Plaincopyprint?
  1. //program running in the foreground, the message normal display  
  2. if ([[UIApplication sharedapplication] applicationstate] = = uiapplicationstateactive)
  3. {
  4.  
  5. else{//if the program is running in the background, you receive a message with the notification type to display  
  6.     Uilocalnotification *localnotification = [[Uilocalnotification alloc] init]; 
  7.     Localnotification.alertaction = @ "OK" ; 
  8.     Localnotification.alertbody = [NSString stringwithformat:@ "from:%@\n\n%@" ,@ "test" ,@ "This is a test message" ];//Notification subject  
  9.     Localnotification.soundname = @ "crunch.wav" ;//Notification sound
  10.     Localnotification.applicationiconbadgenumber = 1;//number of marks  
  11. [[UIApplication sharedapplication] presentlocalnotificationnow:localnotification]; //Send notifications
  12. }

Real machine test, log in after the switch to the background state, and then on the computer with spark sent a message to the iphone, the status bar turned out the notification message, basically successfully implemented.

--------------------------------------------------------------------------------------------------------------- ----------------------------------

Additional notes:

Online search data, found a problem, if your application does not implement VoIP, if you do this method, there is the risk of Apple reject; then the alternative is when the application enters the background or has exited, the server will send a message to the user based on the change of user status, At the same time using the Apple APNs to push the new message to the user, but we know that Apple's APNs in the instant and reliability is not guaranteed, that is, the notification of new news to Apple, Apple does not guarantee that can successfully help you push, nor guarantee instant push, So just look for a third party free push service provider.

Google a bit, find a call Aurora push free push, is being researched, there are gains in the update ...

iOS XMPP Development app background mode receive chat messages [go]

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.