Java/Kotlin Server SDK

Installation

The Beams Java/Kotlin server SDK is available on Maven Central.

Maven

1
2
3
4
5
6
7
<dependencies>
    <dependency>
      <groupId>com.pusher</groupId>
      <artifactId>push-notifications-server-java</artifactId>
      <version>1.1.0</version>
    </dependency>
</dependencies>

Gradle

1
2
3
dependencies {
  compile 'com.pusher:push-notifications-server-java:1.1.0'
}

You can download a version of the .jar directly from Maven.

Reference

Class: PushNotifications

PushNotifications(instanceId, secretKey)

Construct a new Pusher Beams Client connected to your Beams instance.

Arguments

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

Example

  • Java
  • Kotlin
String instanceId = "YOUR_INSTANCE_ID_HERE";
String secretKey = "YOUR_SECRET_KEY_HERE";

PushNotifications beamsClient = new PushNotifications(instanceId, secretKey);

.publishToInterests

Sends broadcast notifications to groups of subscribed devices using Device Interests

Arguments

  • interests(List<String>): List of interests to send the push notification to, ranging from 1 to 100 per publish request. See Interests.
  • publishRequest(Map<String, Map>): Map containing the body of the push notification publish request. See publish API reference.

Returns
String that contains publishId: See publish API reference

Example

  • Java
  • Kotlin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
List<String> interests = Arrays.asList("donuts", "pizza");

Map<String, Map> publishRequest = new HashMap();

Map<String, String> alert = new HashMap();
alert.put("alert", "hi");
Map<String, Map> aps = new HashMap();
aps.put("aps", alert);
publishRequest.put("apns", aps);

Map<String, String> fcmNotification = new HashMap();
fcmNotification.put("title", "hello");
fcmNotification.put("body", "Hello world");
Map<String, Map> fcm = new HashMap();
fcm.put("notification", fcmNotification);
publishRequest.put("fcm", fcm);

beamsClient.publishToInterests(interests, publishRequest);

.publishToUsers

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

Arguments

  • userIds(List<String> | Min length=1, Max length=1000): List of ids of users to send the push notification to, ranging from 1 to 1000 per publish request. See Authenticated Users
  • publishRequest(Map<String, Map>): Map containing the body of the push notification publish request. See publish API reference.

Returns

Example

  • Java
  • Kotlin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
List<String> users = Arrays.asList("user-001", "user-002");

Map<String, Map> publishRequest = new HashMap();

Map<String, String> alert = new HashMap();
alert.put("alert", "hi");
Map<String, Map> aps = new HashMap();
aps.put("aps", alert);
publishRequest.put("apns", aps);

Map<String, String> fcmNotification = new HashMap();
fcmNotification.put("title", "hello");
fcmNotification.put("body", "Hello world");
Map<String, Map> fcm = new HashMap();
fcm.put("notification", fcmNotification);
publishRequest.put("fcm", fcm);

beamsClient.publishToUsers(users, publishRequest);

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

Returns

  • beamsToken(Map<String, Any>): Beams Token for the given user

Example

  • Java
  • Kotlin
String userId = "user-001";
Map<String, Object> token = beamsClient.generateToken(userId);