sendToDevice method
This endpoint is used to send send-to-device events to a set of client devices.
eventType
The type of event to send.
txnId
The transaction ID for this event. Clients should generate an
ID unique across requests with the same access token; it will be
used by the server to ensure idempotency of requests.
messages
The messages to send. A map from user ID, to a map from
device ID to message body. The device ID may also be *
,
meaning all known devices for the user.
Implementation
Future<void> sendToDevice(
String eventType,
String txnId,
Map<String, Map<String, Map<String, Object?>>> messages,
) async {
final requestUri = Uri(
path:
'_matrix/client/v3/sendToDevice/${Uri.encodeComponent(eventType)}/${Uri.encodeComponent(txnId)}',
);
final request = Request('PUT', baseUri!.resolveUri(requestUri));
request.headers['authorization'] = 'Bearer ${bearerToken!}';
request.headers['content-type'] = 'application/json';
request.bodyBytes = utf8.encode(
jsonEncode({
'messages': messages
.map((k, v) => MapEntry(k, v.map((k, v) => MapEntry(k, v)))),
}),
);
final response = await httpClient.send(request);
final responseBody = await response.stream.toBytes();
if (response.statusCode != 200) unexpectedResponse(response, responseBody);
final responseString = utf8.decode(responseBody);
final json = jsonDecode(responseString);
return ignore(json);
}