百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

action_push_native

> 编程语言
开源

针对移动平台的 Rails 推送通知

472 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

针对移动平台的 Rails 推送通知

Action Push Native

Action Push Native is a Rails push notification gem for mobile platforms, supporting APNs (Apple) and FCM (Google).

Installation

bash
1. bundle add action_push_native
2. bin/rails g action_push_native:install
3. bin/rails action_push_native:install:migrations
4. bin/rails db:migrate

This will install the gem and run the necessary migrations to set up the database.

Configuration

The installation will create:

  • app/models/application_push_notification.rb
  • app/jobs/application_push_notification_job.rb
  • app/models/application_push_device.rb
  • config/push.yml

app/models/application_push_notification.rb:

ruby
class ApplicationPushNotification 
    encryption_key: 

    team_id: your_apple_team_id
    # Your identifier found on https://developer.apple.com/account/resources/identifiers/list
    topic: your.bundle.identifier

  google:
    # Your Firebase project service account credentials
    # See https://firebase.google.com/docs/cloud-messaging/auth-server
    encryption_key: 

    # Firebase project_id
    project_id: your_project_id

This file contains the configuration for the push notification services you want to use. The push notification services supported are apple (APNs) and google (FCM). If you're configuring more than one app, see the section Configuring multiple apps below.

Configuring multiple apps

You can send push notifications to multiple apps using different notification classes. Each notification class need to inherit from ApplicationPushNotification and set self.application, to a key set in push.yml for each supported platform. You can also (optionally) set a shared application option in push.yml. This acts as the base configuration for that platform, and its values will be merged (and overridden) with the matching app-specific configuration.

In the example below we are configuring two apps: calendar and email using respectively the CalendarPushNotification and EmailPushNotification notification classes.

…

Usage

Create and send a notification asynchronously to a device

ruby
device = ApplicationPushDevice.create! \
  name: "iPhone 16",
  token: "6c267f26b173cd9595ae2f6702b1ab560371a60e7c8a9e27419bd0fa4a42e58f",
  platform: "apple"

notification = ApplicationPushNotification.new \
  title: "Hello world!",
  body:  "Welcome to Action Push Native"

notification.deliver_later_to(device)

deliver_later_to supports also an array of devices:

ruby
notification.deliver_later_to([ device1, device2 ])

A notification can also be delivered synchronously using deliver_to:

ruby
notification.deliver_to(device)

It is recommended to send notifications asynchronously using deliver_later_to. This ensures error handling and retry logic are in place, and avoids blocking your application's execution.

Application data attributes

You can pass custom data to the application using the with_data method:

ruby
notification = ApplicationPushNotification
  .with_data({ badge: "1" })
  .new(title: "Welcome to Action Push Native")

Custom platform Payload

You can configure custom platform payload to be sent with the notification. This is useful when you need to send additional data that is specific to the platform you are using.

You can use with_apple for Apple and with_google for Google:

ruby
notification = ApplicationPushNotification
  .with_apple(aps: { category: "observable", "thread-id": "greeting"}, "apns-priority": "1")
  .with_google(data: { badge: 1 })
  .new(title: "Hello world!")

The platform payload takes precedence over the other fields, and you can use it to override the default behaviour:

ruby
notification = ApplicationPushNotification
  .with_google(android: { notification: { notification_count: nil } })
  .new(title: "Hello world!", body: "Welcome to Action Push Native", badge: 1)

This will unset the default notification_count (badge) field in the Google payload, while keeping title and body.

Silent Notifications

You can create a silent notification via the silent method:

ruby
notification = ApplicationPushNotification.silent.with_data(id: 1).new

This will create a silent notification for both Apple and Google platforms and sets an application data field of { id: 1 } for both platforms. Silent push notification must not contain any attribute which would trigger a visual notification on the device, such as title, body, badge, etc.

Linking a Device to a Record

A Device can be associated with any record in your application via the owner polymorphic association:

ruby
  user = User.find_by_email_address("[email protected]")

  ApplicationPushDevice.create! \
    name: "iPhone 16",
    token: "6c267f26b173cd9595ae2f6702b1ab560371a60e7c8a9e27419bd0fa4a42e58f",
    platform: "apple",
    owner: user

before_delivery callback

You can specify Active Record like callbacks for the delivery method. For example, you can modify or cancel the notification by specifying a custom before_delivery block. The callback has access to the notification object. You can also pass additional context data to the notification by adding extra arguments to the notification constructor:

ruby
  class CalendarPushNotification  error
    # Custom token error handling
  end
end

Structured errors

Every error descends from ActionPushNative::Error. Errors raised from a provider response carry the context of that response:

ruby
rescue ActionPushNative::TooManyRequestsError => error
  error.service          # :apns or :fcm
  error.status           # APNs: HTTP status (429); FCM: google.rpc status token ("RESOURCE_EXHAUSTED")
  error.reason           # APNs: reason token ("TooManyRequests"); FCM: ErrorInfo reason ("QUOTA_EXCEEDED")
  error.retry_after      # provider-mandated wait, in seconds, when the response carried Retry-After
  error.quota_violations # FCM only: google.rpc.QuotaFailure violations, when the response included them
end

Network errors keep the original exception as error.cause.

The delivery job's retry ladder honors retry_after as a floor: retries still back off exponentially, but never sooner than the provider asked.

Instrumentation

Each provider round trip, token refresh included, emits a push.action_push_native Active Support notification with service: and device_token: in the payload. When the provider errors, the payload also carries status:, reason:, retry_after:, and exception_object:.

ruby
ActiveSupport::Notifications.subscribe "push.action_push_native" do |event|
  StatsD.measure "push.#{event.payload[:service]}", event.duration
end

ActionPushNative::Notification attributes

Name Description
:title The title of the notification.
:body The body of the notification.
:badge The badge number to display on the app icon.
:thread_id The thread identifier for grouping notifications.
:sound The sound to play when the notification is received.
:high_priority Whether the notification should be sent with high priority (default: true).
:google_data The Google-specific payload for the notification.
:apple_data The Apple-specific payload for the notification. It can also be used to override APNs request headers, such as apns-push-type, apns-priority, etc.
:data The data payload for the notification, sent to all platforms.
** Any additional attributes passed to the constructor will be merged in the context hash.

Factory methods

Name Description
:with_apple Set the Apple-specific payload for the notification.
:with_google Set the Google-specific payload for the notification. It can also be used to override APNs request headers, such as apns-push-type, apns-priority, etc.
:with_data Set the data payload for the notification, sent to all platforms.
:silent Create a silent notification that does not trigger a visual alert on the device.

License

Action Push Native is licensed under MIT.

Issues· 11 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言