You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
notification=ActionWebPush::Notification.new(title: "Hello World",body: "This is a push notification",endpoint: "https://fcm.googleapis.com/fcm/send/...",p256dh_key: "BNbN3OiAT...",auth_key: "k2i6t8hBmF...",data: {url: "/messages/123"},icon: "/icon.png",badge: "/badge.png",urgency: "high")
Parameters
Parameter
Type
Required
Description
title
String
Yes
Notification title
body
String
Yes
Notification body
endpoint
String
Yes
Push service endpoint
p256dh_key
String
Yes
P256DH public key from subscription
auth_key
String
Yes
Auth secret from subscription
data
Hash
No
Custom data payload
icon
String
No
Notification icon URL
badge
String
No
Badge icon URL
urgency
String
No
Urgency level (very-low, low, normal, high)
Delivery Methods
Synchronous Delivery
# Deliver immediatelynotification.deliver_now# Alias for deliver_nownotification.deliver
Asynchronous Delivery
# Deliver via background jobnotification.deliver_later# With optionsnotification.deliver_later(wait: 5.minutes,wait_until: 1.hour.from_now,queue: :push_notifications,priority: 10)
Notification Methods
Instance Methods
Method
Returns
Description
deliver(connection: nil)
Boolean
Deliver notification synchronously
deliver_now(connection: nil)
Boolean
Alias for deliver
deliver_later(options = {})
Job
Deliver via background job
to_params
Hash
Convert to parameter hash
to_json(*args)
String
Convert to JSON
Attributes
Attribute
Type
Description
title
String
Notification title
body
String
Notification body
data
Hash
Custom data
endpoint
String
Push endpoint
p256dh_key
String
P256DH key
auth_key
String
Auth key
options
Hash
Additional options
Batch Delivery
ActionWebPush::BatchDelivery
Efficiently deliver multiple notifications.
notifications=[notification1,notification2,notification3]# Basic batch deliveryActionWebPush::BatchDelivery.deliver(notifications)# With custom pool and batch sizebatch=ActionWebPush::BatchDelivery.new(notifications,pool: custom_pool,batch_size: 50)batch.deliver_all
Methods
Method
Parameters
Returns
Description
new(notifications, pool: nil, batch_size: nil)
Array, Pool, Integer
BatchDelivery
Create new batch
deliver_all
None
Void
Deliver all notifications
self.deliver(notifications, **options)
Array, Hash
Void
Class method for quick delivery
Base Classes
ActionWebPush::Base
Base class for notification senders with ActionMailer-like interface.
classNotificationSender < ActionWebPush::Basedefaultfrom_subscription: ->{current_subscription}defwelcome(user)web_push(title: "Welcome!",body: "Thanks for joining",to: user.push_subscriptions)endend# UsageNotificationSender.welcome(user).deliver_later
Class Methods
Method
Parameters
Returns
Description
default(params)
Hash
Void
Set default parameters
web_push(params)
Hash
Notification
Create notification
Instance Methods
Method
Parameters
Returns
Description
web_push(params)
Hash
Notification
Create notification
Rate Limiting
ActionWebPush::RateLimiter
Prevent abuse with configurable rate limits.
rate_limiter=ActionWebPush::RateLimiter.new(limits: {endpoint: {max_requests: 100,window: 3600},user: {max_requests: 1000,window: 3600}})# Check rate limitbeginrate_limiter.check_rate_limit!(:endpoint,endpoint_url,user_id)rescueActionWebPush::RateLimitExceeded=>e# Handle rate limit exceededend# Check without raisingifrate_limiter.within_rate_limit?(:endpoint,endpoint_url,user_id)# Proceed with notificationend# Get rate limit infoinfo=rate_limiter.rate_limit_info(:endpoint,endpoint_url,user_id)# => { limit: 100, remaining: 95, window: 3600, reset_at: Time }
# ConfigureActionWebPush.configuredo |config|
config.vapid_subject="mailto:admin@example.com"config.vapid_public_key=ENV['VAPID_PUBLIC_KEY']config.vapid_private_key=ENV['VAPID_PRIVATE_KEY']end# Send notificationnotification=ActionWebPush::Notification.new(title: "New Message",body: "You have a new message",endpoint: subscription.endpoint,p256dh_key: subscription.p256dh_key,auth_key: subscription.auth_key,data: {message_id: 123})notification.deliver_later
beginnotification.deliverrescueActionWebPush::ExpiredSubscriptionError# Remove expired subscriptionsubscription.destroyrescueActionWebPush::RateLimitExceeded=>e# Retry later or queue for later deliverynotification.deliver_later(wait: 1.hour)rescueActionWebPush::DeliveryError=>e# Log and handle delivery failureRails.logger.error"Push delivery failed: #{e.message}"end
Monitoring
# Subscribe to all push notifications eventsActiveSupport::Notifications.subscribe("action_web_push.notification_delivery")do |name,start,finish,id,payload|
duration=(finish - start) * 1000ifpayload[:success]Rails.logger.info"Push delivered in #{duration.round(2)}ms to #{payload[:endpoint]}"elseRails.logger.warn"Push failed to #{payload[:endpoint]}: #{payload[:response_code]}"endend# Custom metrics collectionActiveSupport::Notifications.subscribe("action_web_push.rate_limit_exceeded")do |name,start,finish,id,payload|
Metrics.increment("push.rate_limit_exceeded",tags: {resource_type: payload[:resource_type]})end