ApplePushNotifications.Client (apple_push_notifications v0.3.0)

Copy Markdown View Source

HTTP/2 client for APNs API.

APNs requires HTTP/2 connections. This client handles:

  • Token-based authentication (JWT in Authorization header)
  • HTTP/2 protocol negotiation
  • APNs-specific error handling

Summary

Functions

Get an access token for use in manual requests.

Send a push notification to a device.

Send a push notification using the cached token.

Validate a device token format.

Functions

get_token(opts \\ [])

@spec get_token(keyword()) :: {:ok, String.t()} | {:error, term()}

Get an access token for use in manual requests.

push(device_token, notification, opts \\ [])

@spec push(String.t(), map(), keyword()) :: {:ok, map()} | {:error, term()}

Send a push notification to a device.

Parameters

  • device_token: The hex-encoded device token (64 characters)
  • notification: Map containing:
    • aps: Alert dictionary with alert, badge, sound, etc.
    • Custom payload keys
  • opts: Per-call configuration overrides

Examples

ApplePushNotifications.Client.push(
  "a1b2c3d4e5f6...",
  %{
    aps: %{
      alert: %{title: "Hello", body: "World"},
      badge: 1,
      sound: "default"
    }
  }
)

push_cached(device_token, notification)

@spec push_cached(String.t(), map()) :: {:ok, map()} | {:error, term()}

Send a push notification using the cached token.

This is the preferred method for production use as it uses the TokenCache to avoid regenerating JWTs on every request.

valid_token?(token)

@spec valid_token?(String.t()) :: boolean()

Validate a device token format.

Device tokens should be 64 hex characters.