createPushTarget method

Future<Target> createPushTarget({
  1. required String targetId,
  2. required String identifier,
  3. String? providerId,
  4. String? bundleId,
  5. String? environment,
})

Use this endpoint to register a device for push notifications. Provide a target ID (custom or generated using ID.unique()), a device identifier (usually a device token), and optionally specify which provider should send notifications to this target. The target is automatically linked to the current session and includes device information like brand and model. A session holds one push target per provider, so if one already exists this endpoint updates and returns that target instead of creating a second one, and a device that rotates its token is never notified twice.

Implementation

Future<models.Target> createPushTarget({
  required String targetId,
  required String identifier,
  String? providerId,
  String? bundleId,
  String? environment,
}) async {
  final String apiPath = '/account/targets/push';

  final Map<String, dynamic> apiParams = {
    'targetId': targetId,
    'identifier': identifier,
    if (providerId != null) 'providerId': providerId,
    if (bundleId != null) 'bundleId': bundleId,
    if (environment != null) 'environment': environment,
  };

  final Map<String, String> apiHeaders = {
    'X-Appwrite-Project': client.config['project'] ?? '',
    'content-type': 'application/json',
    'accept': 'application/json',
  };

  final res = await client.call(
    HttpMethod.post,
    path: apiPath,
    params: apiParams,
    headers: apiHeaders,
  );

  return models.Target.fromMap(res.data);
}