I have an MQTT client that receives messages and should perform actions based on the message content. When the app is active - everything works as expected. However, when switched to a different app or the home screen, the actions do not happen, that is, the registered callback
mqtt.didReceiveMessage()
is not being executed. On the other hand, I can tell that the client stays connected and receives messages because (1) the broker's log does not show client disconnection and (2) once the app becomes active - the messages burst on the screen. Since the messages are qos0, they would have been dropped if the client was down. Another clue is from the CocoaMQTT source code, saying that the socket (I assume the one that maintains the connectivity) runs in background by default:
/// Enable backgounding socket if running on iOS platform. Default is true
///
/// - Note:
public var backgroundOnSocket: Bool
So, the messages seem to be stored somewhere in the system while the app is not active.
For this app, it is essential that the messages are parsed and the appropriate actions are taken at the time when the messages are received even if the app is not active.
What would be the (most optimal) approach to allow executing the didReceiveMessage() callback when the app is not active? It would be appreciated if someone could contrast different background app modes that could be appropriate in this case scenario.
XCode 12
Swift 5.6.1