Vapid::Client
HTTP client for sending VAPID-authenticated push notifications.
Usage
client = Vapid::Client.new
key_pair = Vapid::KeyPair.generate
response = client.send_notification(
endpoint: "https://fcm.googleapis.com/fcm/send/...",
audience: "https://fcm.googleapis.com",
subject: "mailto:admin@example.com",
private_key: key_pair,
payload: {"title" => "Hello"}.to_json,
ttl: 3600
)
Constants
DEFAULT_TTL = 2419200
Default TTL (Time To Live) in seconds
DEFAULT_URGENCY = "normal"
Default urgency level
VALID_URGENCIES = ["very-low", "low", "normal", "high"] of ::String
Valid urgency levels per Web Push protocol
Constructors
new(timeout : Time::Span = 30.seconds)
Creates a new VAPID client
Parameters
timeout: HTTP request timeout (default: 30 seconds)
Instance methods
Extracts the audience (origin) from a push endpoint URL
Example
audience = client.extract_audience("https://fcm.googleapis.com/fcm/send/abc123")
# => "https://fcm.googleapis.com"
send_notification(endpoint : String, audience : String | Nil = nil, subject : String | Nil = nil, private_key : KeyPair | Nil = nil, payload : String | Nil = nil, user_public_key : String | Nil = nil, user_auth : String | Nil = nil, ttl : Int32 = DEFAULT_TTL, urgency : String = DEFAULT_URGENCY, topic : String | Nil = nil, headers : RequestHeaders | Nil = nil) : HTTP::Client::Response
Sends a push notification with VAPID authentication
Parameters
endpoint: The push service endpoint URLaudience: The origin of the push service (extracted from endpoint if not provided)subject: Contact information (mailto: or https: URL)private_key: KeyPair for VAPID authenticationpayload: The notification payload (JSON string or nil for trigger notification)user_public_key: The subscriber's p256dh key (base64url, required if payload provided)user_auth: The subscriber's auth secret (base64url, required if payload provided)ttl: Time To Live in seconds (default: 28 days)urgency: Urgency level: very-low, low, normal, or high (default: normal)topic: Topic for message replacement
Returns the HTTP response