PHP Server SDK

This library requires a PHP version of 5.6 or greater

Installation

The Beams PHP Server SDK is available on Packagist here.

We recommend that you use Composer to install this SDK.


Using Composer

You can add this SDK to your project using composer, or by directly adding it to your composer.json:

$ composer require pusher/pusher-push-notifications

JSON

1
2
3
"require": {
    "pusher/pusher-push-notifications": "^1.0"
}

Reference

PushNotifications.__construct

Constructs a new Beams client instance using your instance id and secret key (you can get these from the dashboard)

Arguments

  • $options (array | required):
    • instanceId(string | required): The unique identifier for your Push notifications instance. This can be found in the dashboard under "Credentials".
    • secretKey(string | required): The secret key your server will use to access your Beams instance. This can be found in the dashboard under "Credentials".

Example

Php

1
2
3
4
$beamsClient = new \Pusher\PushNotifications\PushNotifications(array(
  "instanceId" => "YOUR_INSTANCE_ID_HERE",
  "secretKey" => "YOUR_SECRET_KEY_HERE",
));

.publishToInterests

Sends broadcast notifications to groups of subscribed devices using Device Interests

Arguments

  • $interests (Array<string> | required): Array of interests to send the push notification to, ranging from 1 to 100 per publish request. See Device Interests.
  • $publishBody: See publish API reference

Returns
An array containing the publish response body. See publish API reference

Example

Php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
include 'src/PushNotifications.php';
$publishResponse = $beamsClient->publishToInterests(
  array("hello", "donuts"),
  array(
    "fcm" => array(
      "notification" => array(
        "title" => "Hi!",
        "body" => "This is my first Push Notification!"
      )
    ),
    "apns" => array("aps" => array(
      "alert" => array(
        "title" => "Hi!",
        "body" => "This is my first Push Notification!"
      )
    ))
));

.publishToUsers

Securely send notifications to individual users of your application using Authenticated Users

Arguments

  • $userIds (Array<string> | required): Array of ids of users to send the push notification to, ranging from 1 to 1000 per publish request. See Authenticated Users.
  • $publishBody: See publish API reference

Returns
An array containing the publish response body. See publish API reference

Example

Php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
include 'src/PushNotifications.php';
$publishResponse = $beamsClient->publishToUsers(
  array("user-001", "user-002"),
  array(
    "fcm" => array(
      "notification" => array(
        "title" => "Hi!",
        "body" => "This is my first Push Notification!"
      )
    ),
    "apns" => array("aps" => array(
      "alert" => array(
        "title" => "Hi!",
        "body" => "This is my first Push Notification!"
      )
    ))
));

.generateToken

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 (string | required): Id of the user you would like to generate a Beams auth token for.

Returns
Beams token string.

Example

Php

1
2
3
$userId = "user-001";
$token = $beamsClient.generateToken($userId);
// Return $token to device

.deleteUser

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 (string | required): Id of the user you would like to remove from Beams.

Returns
Nothing.

Example

Php

$beamsClient.deleteUser("user-001");