The community forums are in BETA and closed to the public.

Unsubscribe from channel on "applicationDidEnterBackground" event IOS

I am using PubNub for an IOS app (IOS SDK 8.0). When I am trying to unsubscribe from a channel on "applicationDidEnterBackground" event, I get the following console output. It says the channel is unsubscribed, but when I check from admin console I am still on the channel. Can you give some advice on this ?

Here is the line I am using in "applicationDidEnterBackground":

PubNub.unsubscribeFromChannel(channelToReceive,{(list, err:PNError!)-> Void in if(err != nil){ println(err.localizedFailureReason) } else { println("Unsubscribed...") } })

Here is the PubNub console output after "applicationDidEnterBackground" event:

2014-11-08 15:21:14.933 Chat[297:60b] PubNub (0x155b05f0) TRYING TO UNSUBSCRIBE FROM CHANNELS: ( "PNChannel(0x1555e710) b905359780766" ) (STATE: 'connected') 2014-11-08 15:21:14.937 Chat[297:60b] PubNub (0x155b05f0) UNSUBSCRIBE FROM CHANNELS (STATE: 'connected') 2014-11-08 15:21:14.941 Chat[297:60b] PNMessagingChannel (0x1559d8a0) [CHANNEL::PNMessagingConnectionIdentifier] LEAVING SPECIFIC SET OF CHANNELS... (STATE: 10) 2014-11-08 15:21:14.945 Chat[297:60b] PubNub (0x155b05f0) WILL UNSUBSCRIBE FROM CHANNELS: ( "PNChannelPresence(0x155f7f20) b905359780766-pnpres", "PNChannel(0x1555e710) b905359780766" ) (STATE: 'connected') 2014-11-08 15:21:14.949 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier::WRITE] WRITE BUFFER CONTENT (STATE: 33683456) 2014-11-08 15:21:14.954 Chat[297:60b] PNMessagingChannel (0x1559d8a0) [CHANNEL::PNMessagingConnectionIdentifier] WILL START REQUEST PROCESSING: (STATE: 8) 2014-11-08 15:21:14.957 Chat[297:60b] PNMessagingChannel (0x1559d8a0) [CHANNEL::PNMessagingConnectionIdentifier] RECONNECTING BY REQUEST... (STATE: 8) 2014-11-08 15:21:14.989 Chat[297:60b] PNReachability (0x155bef40) PUBNUB SERVICES REACHABILITY CHANGED W/O EVENT CONNECTED? 1 | NETWORK ADDRESS: 2014-11-08 15:21:14.991 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier] TRYING TO RECONNECT... (STATE: 100792320) 2014-11-08 15:21:14.993 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier] DESTROYING STREAMS... (STATE: 100792328) 2014-11-08 15:21:14.995 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier] UNSCHEDULE REQUEST SENDING (70e3b8fb-810b-429e-afec-49c4124116fc) (STATE: 67237896) 2014-11-08 15:21:14.999 Chat[297:60b] PNMessagingChannel (0x1559d8a0) [CHANNEL::PNMessagingConnectionIdentifier] DID CANCEL REQUEST: (STATE: 0) 2014-11-08 15:21:15.028 Chat[297:60b] PNReachability (0x155bef40) PUBNUB SERVICES REACHABILITY CHANGED W/O EVENT CONNECTED? 1 | NETWORK ADDRESS: 2014-11-08 15:21:15.031 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier::READ] DESTROYING... (STATE: 129032) 2014-11-08 15:21:15.038 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier::READ] DESTROYED. (STATE: 126984) 2014-11-08 15:21:15.041 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier::WRITE] DESTROYING... (STATE: 126984) 2014-11-08 15:21:15.053 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier::WRITE] DESTROYED. (STATE: 122888) 2014-11-08 15:21:15.057 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier] READ AND WRITE STREAMS DESTROYED (STATE: 12582920) 2014-11-08 15:21:15.061 Chat[297:60b] PNConnection (0x1559fce0) [CONNECTION::PNMessagingConnectionIdentifier::WRITE] WRITE CANCELED (STATE: 12582920) (lldb)

Thanks in advance.

Hi Berker,

There are two options. First option is to issue a "LEAVE" event from the device. The second option is to turn on TCP FIN recognition on your PUB/SUB key.

Can you elaborate? I have the same problem but can't find a way to issue a leave request. It's probably simple but I can't figure it out :(

Actually, PubNub seems inactive once in background mode. Can't figure out a way to detect that a client has entered a background state ... I would imagined that it would have been pretty straight forward but I guess not.

PubNub would only be inactive because the app is inactive (not running) while in the background. Running an iOS in the background requires special permission from Apple. Once you have permission, just configure your app to run in the background and you can use PubNub in the background, as well.

This is also something that is very easily done in Android apps and PubNub runs in the background receiving realtime messages. See more about our Android Susbcribe-at-Boot sample app.

Login or Signup to post a comment