Ruby Server SDK

Installation

The Pusher Beams Ruby server SDK is available on RubyGems. Add this line to your application’s Gemfile:

Gemfile

1
gem 'pusher-push-notifications'

Usage

Configuring the SDK for Your Instance

Use your instance id and secret (you can get these from the dashboard) to create a PushNotifications instance:

Ruby

1
2
3
4
Pusher::PushNotifications.configure do |config|
  config.instance_id = 'YOUR_INSTANCE_ID_HERE'
  config.secret_key = 'YOUR_SECRET_KEY_HERE'
end

Publishing a Notification

Once you have created your PushNotifications instance you can publish a push notification to your registered & subscribed devices:

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
data = {
  apns: {
    aps: {
      alert: {
        title: 'Hello',
        body: 'Hello, world!'
      }
    }
  },
  fcm: {
    notification: {
      title: 'Hello',
      body: 'Hello, world!'
    }
  }
}

Pusher::PushNotifications.publish_to_interests(interests: ['hello'], payload: data)

Publish to Users

Ruby

1
2
users = ['cucas', 'jonathan', 'jordan', 'luis', 'luka', 'mina']
Pusher::PushNotifications.publish_to_users(users: users, payload: data)

Generate Token

Ruby

1
Pusher::PushNotifications.generate_token(user: 'Elmo')

Delete User

Ruby

1
Pusher::PushNotifications.delete_user(user: 'Elmo')

Reference

Class: PushNotifications

Arguments

  • instance_id(string): The unique identifier for your Push notifications instance. This can be found in the dashboard under "Credentials".
  • secret_key(string): The secret key your server will use to access your Beams instance. This can be found in the dashboard under "Credentials".

publish_to_interests(interests, payload)

Publish a new push notification to Pusher Beams with the given payload.

Arguments

  • interests: List of interests to send the push notification to, ranging from 1 to 100 per publish request. See Interests.
  • payload: Map containing the body of the push notification publish request. See publish API reference.

Returns
String that contains publish_id: See publish API reference

publish_to_users(users, publishRequest)

Publish the given publishRequest to specified users.

Arguments

  • users: Array of ids of users to send the push notification to, ranging from 1 to 1000 per publish request. See Authenticated Users.
  • publishRequest: Map containing the body of the push notification publish request. See publish API reference.

Returns
String that contains publishId: See publish API reference

generate_token(userId)

Generate a Beams auth token to allow a user to associate their device with their user ID. The token is valid for 24 hours.

Arguments

  • userId: ID of the user you would like to generate a Beams auth token for.

delete_user(userId)

Remove the given user (and all of their devices) from Beams. This user will no longer receive any notifications and all state stored about their devices will be deleted.

Arguments

  • userId: ID of the user you would like to remove from Beams.