messageHandler(_:)
Handles decrypted messages received from the paired accessory.
Declaration
func messageHandler(_ message: TransportMessage)Parameters
- message:
The decrypted message payload from the accessory.
Discussion
The system calls this method when your accessory sends data to the data provider extension. For Bluetooth transport, the accessory sends data through your transport extension using sendMessageToDataProvider(_:). For internet transport, the accessory routes data to the device through APNs using pushToken. In either case, the system delivers decrypted messages to this method regardless of the transport type.
Process notification responses
Parse the message to determine the response type (notification dismissal, action selection, or text input). Create a NotificationResponse instance and send it to the system using sendResponse(_:):
func messageHandler(_ message: AccessoryMessage) {
for payload in message.payloads {
let parsedResponse = parseResponse(payload.data)
let response = NotificationResponse(
sourceIdentifier: parsedResponse.sourceID,
notificationIdentifier: parsedResponse.notificationID,
actionIdentifier: parsedResponse.actionID,
userText: parsedResponse.userText
)
Task {
try await session?.sendResponse(response)
}
}
}