I have set up a channel to receive messages from the receiver app:
import Foundation
import GoogleCast
class GcastChannel: GCKCastChannel {
var gcastDelegate: GcastDelegate?
override func didReceiveTextMessage(_ message: String) {
if let jsonData = message.data(using: .utf8) {
do {
let gcastEvent = try JSONDecoder().decode( GcastEvent.self, from: jsonData)
gcastDelegate?.didReceiveGcastEvent(event: gcastEvent)
} catch {
print("message decode error: \(error)")
}
}
}
}
And set up the session manager and listener in the main view controller:
func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKCastSession) {
castSession = session
castSession?.add(gcastChannel)
gcastChannel.gcastDelegate = self
}
It works perfectly when the app is in the foreground, but when the app is in the background, it does not respond to events anymore. In addition, when the app is put in the foreground again, the app is still unresponsive to the events being sent.
How do I get the app to respond to events while the app is in the background?