CometChat delegates not called in the background after some time

I have implemented the CometChat delegates such as the CometChatMessageDelegate, when the app is opened and then the user presses the home button on the iPhone, after some time, when you send a message to this user, the push notification is received but somehow the delegates are not called. I have tried to put some file logging whenever the methods are fired but after checking the logs, none of them is called.

Also, what is the behavior of the CometChatConnectionDelegate? Is there a case where we need to explicitly start the CometChat service in the SDK?

Hi @jeraldo

we are looking into the issue right now and will update you on this. No there is no need i guess to start the CometChat Service in SDK explicitly. Still will check this scenario and update you.

Thanks

Hi @nishant.tiwari as per CometChat’s Engineering Team reply via email:

We have tried this by keeping the app for around 5-8 minutes in the background and tried to send a message the delegates were getting called. Please let us know if we have missed anything while replicating the issue at our end.

I have re-tested and the delegates are still not called.

  1. Install app and login to CometChat.
  2. Disconnect from the debugger or stop Xcode from running you app in the device.
  3. Open your app normally (not via Xcode).
  4. Press the home button of the iPhone.
  5. Wait for 8-10 minutes.
  6. Try to send a message to this device.
  7. The device will receive the push notification.
  8. Open the app (make sure that the app has not restarted or relaunched).
  9. The delegates were not called.

In my case I have a Singleton class that conforms to the CometChat delegates such as the message delegate. When the delegate methods are called, for example onTextMessageReceived, I send a notification via NotificationCenter and the view controllers that are observing the notifications.

But when the above steps happened, the delegates don’t get called at all. I cannot even see the delegates getting called when looking at the log files that are stored on the device.

func onTextMessageReceived(textMessage: TextMessage) {
    NotificationCenter.default.post(name: .newMessage, object: textMessage)
}

Hi @jeraldo

We went through the scenario it seems the builder gets deallocated when kept in background. In our SDK we do not maintain the states of end developer’s viewcontroller life cycle. We thought a work around for your scenario. The following way would help you achieve this

In your ViewController’s viewDidLoad() add the following observers

NotificationCenter.default.addObserver( self , selector: #selector (didBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil )

and add this method in the ViewController
@objc func didBecomeActive() {

//Refresh the screen once i.e initiate the builder again and refresh the table view

}

So whenever the app comes from background to foreground after 10 minutes this method gets called and the message is received and even the delegates will work properly even after coming from background to foreground. Do let me know if you still have any issue we would love to help you out.

Hi @nishant.tiwari. Thanks for the quick response and for the suggested work around.
I was referring to the delegate methods and not the builder however. Few things I noticed during my testing and observation:

  1. When the app goes into the background for a long time (around 8+ minutes) and goes back into the foreground, the CometChatConnectionDelegate's disconnected() is called and after a second the connected(). It seems that the connection is being stopped in the background.

Hi @jeraldo

Yes we understood your query that the delegates weren’t getting called it because your builder is getting deallocated while coming in the foreground you will get the event and at that event you will have to reinitialize the builder in order to get the delegates back in working.

Regarding the point you mentioned about the connection delegate will check this and will update you on this. However, a small question in AppDelegate’s didBecomeActive method have you done this CometChat.startservice ? Please let us know if you still have any doubts or issue will be happy to help you out.

Thanks,
Nishant

Hi @nishant.tiwari. Thanks again. Can you please specify which builder are you referring to? Is it the AppSettingsBuilder that is used to initialise CometChat? Regarding the start/stop service. I currently do not call these methods in my app.

Hi

The builder you are using on the screen which is coming from background to foreground. E.g If i am on a conversation list page and I go to background and come to foreground again the conversation list page gets open so i need to call the conversationBuilder init method in the event.
Regarding the start and stop service if you refer the sample app we have added this method in app delegate file in didBecomeActive method just to establish the connection again if the connection got broke.
do let me know if you still face any issue we would love to help you out.

Thanks,
Nishant

Thanks @nishant.tiwari. Will try to apply the startService on didBecomeActive and see how it goes.