iphone XMPP App run background

后端 未结 3 523
陌清茗
陌清茗 2020-11-30 05:05

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...H

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 06:04

    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];
        }];
    }
    

提交回复
热议问题